diff --git a/go.mod b/go.mod index d17de6e5e9..c54d9cc5e6 100644 --- a/go.mod +++ b/go.mod @@ -64,6 +64,7 @@ require ( github.com/NYTimes/gziphandler v1.1.1 // indirect github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect github.com/bronze1man/goStrongswanVici v0.0.0-20190828090544-27d02f80ba40 // indirect + github.com/containerd/cgroups v0.0.0-20200710171044-318312a37340 github.com/containerd/containerd v1.4.1 github.com/containerd/cri v1.11.1-0.20200820101445-b0cc07999aa5 github.com/coreos/flannel v0.12.0 diff --git a/pkg/agent/run.go b/pkg/agent/run.go index ee6e7864cc..0f2532149f 100644 --- a/pkg/agent/run.go +++ b/pkg/agent/run.go @@ -3,12 +3,15 @@ package agent import ( "context" "errors" + "fmt" "io/ioutil" "os" "path/filepath" "strings" "time" + "github.com/containerd/cgroups" + cgroupsv2 "github.com/containerd/cgroups/v2" systemd "github.com/coreos/go-systemd/daemon" "github.com/rancher/k3s/pkg/agent/config" "github.com/rancher/k3s/pkg/agent/containerd" @@ -170,6 +173,13 @@ func Run(ctx context.Context, cfg cmds.Agent) error { } func validate() error { + if cgroups.Mode() == cgroups.Unified { + return validateCgroupsV2() + } + return validateCgroupsV1() +} + +func validateCgroupsV1() error { cgroups, err := ioutil.ReadFile("/proc/self/cgroup") if err != nil { return err @@ -188,6 +198,27 @@ func validate() error { return nil } +func validateCgroupsV2() error { + manager, err := cgroupsv2.LoadManager("/sys/fs/cgroup", "/") + if err != nil { + return err + } + controllers, err := manager.RootControllers() + if err != nil { + return err + } + m := make(map[string]struct{}) + for _, controller := range controllers { + m[controller] = struct{}{} + } + for _, controller := range []string{"cpu", "cpuset", "memory"} { + if _, ok := m[controller]; !ok { + return fmt.Errorf("faild to find %s cgroup (v2)", controller) + } + } + return nil +} + func configureNode(ctx context.Context, agentConfig *daemonconfig.Agent, nodes v1.NodeInterface) error { count := 0 for { diff --git a/vendor/modules.txt b/vendor/modules.txt index 96616b4153..afc0eac394 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -166,6 +166,7 @@ github.com/cilium/ebpf/internal/unix # github.com/container-storage-interface/spec v1.2.0 github.com/container-storage-interface/spec/lib/go/csi # github.com/containerd/cgroups v0.0.0-20200710171044-318312a37340 => github.com/containerd/cgroups v0.0.0-20200531161412-0dbf7f05ba59 +## explicit github.com/containerd/cgroups github.com/containerd/cgroups/stats/v1 github.com/containerd/cgroups/v2