From 04e0687da14070ea3ff208687d4f749d5dc319a0 Mon Sep 17 00:00:00 2001 From: Matthew Heon Date: Wed, 10 Jan 2018 16:21:54 -0500 Subject: Add ability to set CGroup Parent via API Signed-off-by: Matthew Heon --- libpod/container.go | 3 +-- libpod/container_top.go | 2 +- libpod/options.go | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) (limited to 'libpod') diff --git a/libpod/container.go b/libpod/container.go index 79fd5d42c..fbc01be0a 100644 --- a/libpod/container.go +++ b/libpod/container.go @@ -83,7 +83,6 @@ type Container struct { // TODO Add readonly support // TODO add SHM size support // TODO add shared namespace support -// TODO add cgroup parent support // containerRuntimeInfo contains the current state of the container // It is stored on disk in a tmpfs and recreated on reboot @@ -1228,7 +1227,7 @@ func (c *Container) cleanupStorage() error { // CGroupPath returns a cgroups "path" for a given container. func (c *Container) CGroupPath() cgroups.Path { - return cgroups.StaticPath(filepath.Join(CgroupParent, fmt.Sprintf("libpod-conmon-%s", c.ID()))) + return cgroups.StaticPath(filepath.Join(c.config.CgroupParent, fmt.Sprintf("libpod-conmon-%s", c.ID()))) } // copyHostFileToRundir copies the provided file to the runtimedir diff --git a/libpod/container_top.go b/libpod/container_top.go index 020773488..0eb1d0c41 100644 --- a/libpod/container_top.go +++ b/libpod/container_top.go @@ -25,7 +25,7 @@ func (c *Container) GetContainerPids() ([]string, error) { // Gets the pids for a container without locking. should only be called from a func where // locking has already been established. func (c *Container) getContainerPids() ([]string, error) { - taskFile := filepath.Join("/sys/fs/cgroup/pids", CgroupParent, fmt.Sprintf("libpod-conmon-%s", c.ID()), c.ID(), "tasks") + taskFile := filepath.Join("/sys/fs/cgroup/pids", c.config.CgroupParent, fmt.Sprintf("libpod-conmon-%s", c.ID()), c.ID(), "tasks") logrus.Debug("reading pids from ", taskFile) content, err := ioutil.ReadFile(taskFile) if err != nil { diff --git a/libpod/options.go b/libpod/options.go index 199bf9ee9..75ad7acfb 100644 --- a/libpod/options.go +++ b/libpod/options.go @@ -465,6 +465,23 @@ func WithNetNS(portMappings []ocicni.PortMapping) CtrCreateOption { } } +// WithCgroupParent sets the Cgroup Parent of the new container +func WithCgroupParent(parent string) CtrCreateOption { + return func(ctr *Container) error { + if ctr.valid { + return ErrCtrFinalized + } + + if parent == "" { + return errors.Wrapf(ErrInvalidArg, "cgroup parent cannot be empty") + } + + ctr.config.CgroupParent = parent + + return nil + } +} + // Pod Creation Options // WithPodName sets the name of the pod -- cgit v1.2.3-54-g00ecf