diff --git a/server/config.go b/server/config.go index dec0cfc57..4c7595d25 100644 --- a/server/config.go +++ b/server/config.go @@ -25,6 +25,8 @@ type Config struct { Subreaper bool `toml:"subreaper"` // OOMScore adjust the containerd's oom score OOMScore int `toml:"oom_score"` + // Cgroup specifies cgroup information for the containerd daemon process + Cgroup CgroupConfig `toml:"cgroup"` md toml.MetaData } @@ -46,6 +48,10 @@ type MetricsConfig struct { Address string `toml:"address"` } +type CgroupConfig struct { + Path string `toml:"path"` +} + // Decode unmarshals a plugin specific configuration by plugin id func (c *Config) Decode(id string, v interface{}) (interface{}, error) { data, ok := c.Plugins[id] diff --git a/server/server_linux.go b/server/server_linux.go index 34d35d012..324a1b05d 100644 --- a/server/server_linux.go +++ b/server/server_linux.go @@ -4,8 +4,10 @@ import ( "context" "os" + "github.com/containerd/cgroups" "github.com/containerd/containerd/log" "github.com/containerd/containerd/sys" + specs "github.com/opencontainers/runtime-spec/specs-go" ) const ( @@ -35,5 +37,21 @@ func apply(ctx context.Context, config *Config) error { return err } } + if config.Cgroup.Path != "" { + cg, err := cgroups.Load(cgroups.V1, cgroups.StaticPath(config.Cgroup.Path)) + if err != nil { + if err != cgroups.ErrCgroupDeleted { + return err + } + if cg, err = cgroups.New(cgroups.V1, cgroups.StaticPath(config.Cgroup.Path), &specs.LinuxResources{}); err != nil { + return err + } + } + if err := cg.Add(cgroups.Process{ + Pid: os.Getpid(), + }); err != nil { + return err + } + } return nil }