Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Release-1.26] Allow executors to define containerd and docker behavior #9252

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions pkg/agent/containerd/config_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package containerd

import (
"context"
"os"

"github.com/containerd/containerd"
Expand Down Expand Up @@ -36,9 +35,9 @@ func getContainerdArgs(cfg *config.Node) []string {
return args
}

// setupContainerdConfig generates the containerd.toml, using a template combined with various
// SetupContainerdConfig generates the containerd.toml, using a template combined with various
// runtime configurations and registry mirror settings provided by the administrator.
func setupContainerdConfig(ctx context.Context, cfg *config.Node) error {
func SetupContainerdConfig(cfg *config.Node) error {
isRunningInUserNS := userns.RunningInUserNS()
_, _, controllers := cgroups.CheckCgroups()
// "/sys/fs/cgroup" is namespaced
Expand Down
6 changes: 2 additions & 4 deletions pkg/agent/containerd/config_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
package containerd

import (
"context"

"github.com/containerd/containerd"
"github.com/k3s-io/k3s/pkg/agent/templates"
"github.com/k3s-io/k3s/pkg/daemons/config"
Expand All @@ -23,9 +21,9 @@ func getContainerdArgs(cfg *config.Node) []string {
return args
}

// setupContainerdConfig generates the containerd.toml, using a template combined with various
// SetupContainerdConfig generates the containerd.toml, using a template combined with various
// runtime configurations and registry mirror settings provided by the administrator.
func setupContainerdConfig(ctx context.Context, cfg *config.Node) error {
func SetupContainerdConfig(cfg *config.Node) error {
if cfg.SELinux {
logrus.Warn("SELinux isn't supported on windows")
}
Expand Down
10 changes: 3 additions & 7 deletions pkg/agent/containerd/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ var (
// Run configures and starts containerd as a child process. Once it is up, images are preloaded
// or pulled from files found in the agent images directory.
func Run(ctx context.Context, cfg *config.Node) error {
if err := setupContainerdConfig(ctx, cfg); err != nil {
return err
}

args := getContainerdArgs(cfg)
stdOut := io.Writer(os.Stdout)
stdErr := io.Writer(os.Stderr)
Expand Down Expand Up @@ -109,14 +105,14 @@ func Run(ctx context.Context, cfg *config.Node) error {
return err
}

return preloadImages(ctx, cfg)
return PreloadImages(ctx, cfg)
}

// preloadImages reads the contents of the agent images directory, and attempts to
// PreloadImages reads the contents of the agent images directory, and attempts to
// import into containerd any files found there. Supported compressed types are decompressed, and
// any .txt files are processed as a list of images that should be pre-pulled from remote registries.
// If configured, imported images are retagged as being pulled from additional registries.
func preloadImages(ctx context.Context, cfg *config.Node) error {
func PreloadImages(ctx context.Context, cfg *config.Node) error {
fileInfo, err := os.Stat(cfg.Images)
if os.IsNotExist(err) {
return nil
Expand Down
22 changes: 5 additions & 17 deletions pkg/agent/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (

systemd "github.com/coreos/go-systemd/daemon"
"github.com/k3s-io/k3s/pkg/agent/config"
"github.com/k3s-io/k3s/pkg/agent/containerd"
"github.com/k3s-io/k3s/pkg/agent/cridockerd"
"github.com/k3s-io/k3s/pkg/agent/flannel"
"github.com/k3s-io/k3s/pkg/agent/netpol"
"github.com/k3s-io/k3s/pkg/agent/proxy"
Expand Down Expand Up @@ -132,14 +130,11 @@ func run(ctx context.Context, cfg cmds.Agent, proxy proxy.Proxy) error {
}
}

if nodeConfig.Docker {
if err := cridockerd.Run(ctx, nodeConfig); err != nil {
return err
}
} else if nodeConfig.ContainerRuntimeEndpoint == "" {
if err := containerd.Run(ctx, nodeConfig); err != nil {
return err
}
notifySocket := os.Getenv("NOTIFY_SOCKET")
os.Unsetenv("NOTIFY_SOCKET")

if err := setupTunnelAndRunAgent(ctx, nodeConfig, cfg, proxy); err != nil {
return err
}

// the agent runtime is ready to host workloads when containerd is up and the airgap
Expand All @@ -150,13 +145,6 @@ func run(ctx context.Context, cfg cmds.Agent, proxy proxy.Proxy) error {
close(cfg.AgentReady)
}

notifySocket := os.Getenv("NOTIFY_SOCKET")
os.Unsetenv("NOTIFY_SOCKET")

if err := setupTunnelAndRunAgent(ctx, nodeConfig, cfg, proxy); err != nil {
return err
}

if err := util.WaitForAPIServerReady(ctx, nodeConfig.AgentConfig.KubeConfigKubelet, util.DefaultAPIServerReadyTimeout); err != nil {
return errors.Wrap(err, "failed to wait for apiserver ready")
}
Expand Down
23 changes: 23 additions & 0 deletions pkg/daemons/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/k3s-io/k3s/pkg/agent/config"
"github.com/k3s-io/k3s/pkg/agent/containerd"
"github.com/k3s-io/k3s/pkg/agent/proxy"
daemonconfig "github.com/k3s-io/k3s/pkg/daemons/config"
"github.com/k3s-io/k3s/pkg/daemons/executor"
Expand All @@ -21,6 +22,17 @@ func Agent(ctx context.Context, nodeConfig *daemonconfig.Node, proxy proxy.Proxy

logs.InitLogs()
defer logs.FlushLogs()

if nodeConfig.Docker {
if err := startDocker(ctx, nodeConfig); err != nil {
return err
}
} else if nodeConfig.ContainerRuntimeEndpoint == "" {
if err := startContainerd(ctx, nodeConfig); err != nil {
return err
}
}

if err := startKubelet(ctx, &nodeConfig.AgentConfig); err != nil {
return err
}
Expand Down Expand Up @@ -52,6 +64,17 @@ func startKubelet(ctx context.Context, cfg *daemonconfig.Agent) error {
return executor.Kubelet(ctx, args)
}

func startContainerd(ctx context.Context, cfg *daemonconfig.Node) error {
if err := containerd.SetupContainerdConfig(cfg); err != nil {
return err
}
return executor.Containerd(ctx, cfg)
}

func startDocker(ctx context.Context, cfg *daemonconfig.Node) error {
return executor.Docker(ctx, cfg)
}

// ImageCredProvAvailable checks to see if the kubelet image credential provider bin dir and config
// files exist and are of the correct types. This is exported so that it may be used by downstream projects.
func ImageCredProvAvailable(cfg *daemonconfig.Agent) bool {
Expand Down
10 changes: 10 additions & 0 deletions pkg/daemons/executor/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"strconv"
"time"

"github.com/k3s-io/k3s/pkg/agent/containerd"
"github.com/k3s-io/k3s/pkg/agent/cridockerd"
"github.com/k3s-io/k3s/pkg/cli/cmds"
daemonconfig "github.com/k3s-io/k3s/pkg/daemons/config"
"github.com/k3s-io/k3s/pkg/util"
Expand Down Expand Up @@ -217,6 +219,14 @@ func (e *Embedded) CurrentETCDOptions() (InitialOptions, error) {
return InitialOptions{}, nil
}

func (e *Embedded) Containerd(ctx context.Context, cfg *daemonconfig.Node) error {
return containerd.Run(ctx, cfg)
}

func (e *Embedded) Docker(ctx context.Context, cfg *daemonconfig.Node) error {
return cridockerd.Run(ctx, cfg)
}

// waitForUntaintedNode watches nodes, waiting to find one not tainted as
// uninitialized by the external cloud provider.
func waitForUntaintedNode(ctx context.Context, kubeConfig string) error {
Expand Down
10 changes: 10 additions & 0 deletions pkg/daemons/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type Executor interface {
CurrentETCDOptions() (InitialOptions, error)
ETCD(ctx context.Context, args ETCDConfig, extraArgs []string) error
CloudControllerManager(ctx context.Context, ccmRBACReady <-chan struct{}, args []string) error
Containerd(ctx context.Context, node *daemonconfig.Node) error
Docker(ctx context.Context, node *daemonconfig.Node) error
}

type ETCDConfig struct {
Expand Down Expand Up @@ -169,3 +171,11 @@ func ETCD(ctx context.Context, args ETCDConfig, extraArgs []string) error {
func CloudControllerManager(ctx context.Context, ccmRBACReady <-chan struct{}, args []string) error {
return executor.CloudControllerManager(ctx, ccmRBACReady, args)
}

func Containerd(ctx context.Context, config *daemonconfig.Node) error {
return executor.Containerd(ctx, config)
}

func Docker(ctx context.Context, config *daemonconfig.Node) error {
return executor.Docker(ctx, config)
}
Loading