Browse code

use containerd/cgroups to detect cgroups v2

libcontainer does not guarantee a stable API, and is not intended
for external consumers.

this patch replaces some uses of libcontainer/cgroups with
containerd/cgroups.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2020/11/09 23:00:32
Showing 5 changed files
... ...
@@ -5,11 +5,11 @@ package main
5 5
 import (
6 6
 	"os/exec"
7 7
 
8
+	"github.com/containerd/cgroups"
8 9
 	"github.com/docker/docker/daemon/config"
9 10
 	"github.com/docker/docker/opts"
10 11
 	"github.com/docker/docker/rootless"
11 12
 	units "github.com/docker/go-units"
12
-	"github.com/opencontainers/runc/libcontainer/cgroups"
13 13
 	"github.com/pkg/errors"
14 14
 	"github.com/spf13/pflag"
15 15
 )
... ...
@@ -66,7 +66,7 @@ func installConfigFlags(conf *config.Config, flags *pflag.FlagSet) error {
66 66
 	// Note that defaultUserlandProxyPath and honorXDG are configured according to the value of rootless.RunningWithRootlessKit, not the value of --rootless.
67 67
 	flags.BoolVar(&conf.Rootless, "rootless", rootless.RunningWithRootlessKit(), "Enable rootless mode; typically used with RootlessKit")
68 68
 	defaultCgroupNamespaceMode := "host"
69
-	if cgroups.IsCgroup2UnifiedMode() {
69
+	if cgroups.Mode() == cgroups.Unified {
70 70
 		defaultCgroupNamespaceMode = "private"
71 71
 	}
72 72
 	flags.StringVar(&conf.CgroupNamespaceMode, "default-cgroupns-mode", defaultCgroupNamespaceMode, `Default mode for containers cgroup namespace ("host" | "private")`)
... ...
@@ -16,6 +16,7 @@ import (
16 16
 	"strings"
17 17
 	"time"
18 18
 
19
+	"github.com/containerd/cgroups"
19 20
 	statsV1 "github.com/containerd/cgroups/stats/v1"
20 21
 	statsV2 "github.com/containerd/cgroups/v2/stats"
21 22
 	"github.com/containerd/containerd/sys"
... ...
@@ -43,7 +44,6 @@ import (
43 43
 	"github.com/docker/libnetwork/options"
44 44
 	lntypes "github.com/docker/libnetwork/types"
45 45
 	"github.com/moby/sys/mount"
46
-	"github.com/opencontainers/runc/libcontainer/cgroups"
47 46
 	specs "github.com/opencontainers/runtime-spec/specs-go"
48 47
 	"github.com/opencontainers/selinux/go-selinux/label"
49 48
 	"github.com/pkg/errors"
... ...
@@ -362,11 +362,11 @@ func (daemon *Daemon) adaptContainerSettings(hostConfig *containertypes.HostConf
362 362
 	if hostConfig.CgroupnsMode.IsEmpty() {
363 363
 		// for cgroup v2: unshare cgroupns even for privileged containers
364 364
 		// https://github.com/containers/libpod/pull/4374#issuecomment-549776387
365
-		if hostConfig.Privileged && !cgroups.IsCgroup2UnifiedMode() {
365
+		if hostConfig.Privileged && cgroups.Mode() != cgroups.Unified {
366 366
 			hostConfig.CgroupnsMode = containertypes.CgroupnsMode("host")
367 367
 		} else {
368 368
 			m := "host"
369
-			if cgroups.IsCgroup2UnifiedMode() {
369
+			if cgroups.Mode() == cgroups.Unified {
370 370
 				m = "private"
371 371
 			}
372 372
 			if daemon.configStore != nil {
... ...
@@ -637,7 +637,7 @@ func UsingSystemd(config *config.Config) bool {
637 637
 		return true
638 638
 	}
639 639
 	// On cgroup v2 hosts, default to systemd driver
640
-	if getCD(config) == "" && cgroups.IsCgroup2UnifiedMode() && IsRunningSystemd() {
640
+	if getCD(config) == "" && cgroups.Mode() == cgroups.Unified && IsRunningSystemd() {
641 641
 		return true
642 642
 	}
643 643
 	return false
... ...
@@ -758,7 +758,7 @@ func verifyDaemonSettings(conf *config.Config) error {
758 758
 		}
759 759
 	}
760 760
 
761
-	if conf.Rootless && UsingSystemd(conf) && !cgroups.IsCgroup2UnifiedMode() {
761
+	if conf.Rootless && UsingSystemd(conf) && cgroups.Mode() != cgroups.Unified {
762 762
 		return fmt.Errorf("exec-opt native.cgroupdriver=systemd requires cgroup v2 for rootless mode")
763 763
 	}
764 764
 
... ...
@@ -11,6 +11,7 @@ import (
11 11
 	"strconv"
12 12
 	"strings"
13 13
 
14
+	cdcgroups "github.com/containerd/cgroups"
14 15
 	"github.com/containerd/containerd/containers"
15 16
 	coci "github.com/containerd/containerd/oci"
16 17
 	"github.com/containerd/containerd/sys"
... ...
@@ -89,7 +90,7 @@ func WithRootless(daemon *Daemon) coci.SpecOpts {
89 89
 	return func(_ context.Context, _ coci.Client, _ *containers.Container, s *coci.Spec) error {
90 90
 		var v2Controllers []string
91 91
 		if daemon.getCgroupDriver() == cgroupSystemdDriver {
92
-			if !cgroups.IsCgroup2UnifiedMode() {
92
+			if cdcgroups.Mode() != cdcgroups.Unified {
93 93
 				return errors.New("rootless systemd driver doesn't support cgroup v1")
94 94
 			}
95 95
 			rootlesskitParentEUID := os.Getenv("ROOTLESSKIT_PARENT_EUID")
... ...
@@ -814,7 +815,7 @@ func WithCgroups(daemon *Daemon, c *container.Container) coci.SpecOpts {
814 814
 			return nil
815 815
 		}
816 816
 
817
-		if cgroups.IsCgroup2UnifiedMode() {
817
+		if cdcgroups.Mode() == cdcgroups.Unified {
818 818
 			return errors.New("daemon-scoped cpu-rt-period and cpu-rt-runtime are not implemented for cgroup v2")
819 819
 		}
820 820
 
... ...
@@ -3,9 +3,9 @@
3 3
 package daemon // import "github.com/docker/docker/daemon"
4 4
 
5 5
 import (
6
+	"github.com/containerd/cgroups"
6 7
 	"github.com/docker/docker/container"
7 8
 	"github.com/docker/docker/errdefs"
8
-	"github.com/opencontainers/runc/libcontainer/cgroups"
9 9
 	"github.com/pkg/errors"
10 10
 	"github.com/sirupsen/logrus"
11 11
 )
... ...
@@ -27,7 +27,7 @@ func (daemon *Daemon) getLibcontainerdCreateOptions(container *container.Contain
27 27
 		rt.Shim = defaultV2ShimConfig(daemon.configStore, p)
28 28
 	}
29 29
 	if rt.Shim.Binary == linuxShimV1 {
30
-		if cgroups.IsCgroup2UnifiedMode() {
30
+		if cgroups.Mode() == cgroups.Unified {
31 31
 			return "", nil, errdefs.InvalidParameter(errors.Errorf("runtime %q is not supported while cgroups v2 (unified hierarchy) is being used", container.HostConfig.Runtime))
32 32
 		}
33 33
 		logrus.Warnf("Configured runtime %q is deprecated and will be removed in the next release", container.HostConfig.Runtime)
... ...
@@ -8,6 +8,7 @@ import (
8 8
 	"strings"
9 9
 	"sync"
10 10
 
11
+	cdcgroups "github.com/containerd/cgroups"
11 12
 	"github.com/opencontainers/runc/libcontainer/cgroups"
12 13
 	"github.com/sirupsen/logrus"
13 14
 	"golang.org/x/sys/unix"
... ...
@@ -56,7 +57,7 @@ func New(quiet bool, options ...Opt) *SysInfo {
56 56
 	for _, o := range options {
57 57
 		o(&opts)
58 58
 	}
59
-	if cgroups.IsCgroup2UnifiedMode() {
59
+	if cdcgroups.Mode() == cdcgroups.Unified {
60 60
 		return newV2(quiet, &opts)
61 61
 	}
62 62