Remove parent cgroup we create with cgroupfs

Signed-off-by: Matthew Heon <matthew.heon@gmail.com>

Closes: #507
Approved by: baude
This commit is contained in:
Matthew Heon 2018-05-09 16:33:31 -04:00 committed by Atomic Bot
parent b70f6cc04a
commit c4c5c1a3e1
2 changed files with 40 additions and 0 deletions

View File

@ -14,6 +14,7 @@ import (
"syscall"
"time"
"github.com/containerd/cgroups"
"github.com/containers/storage"
"github.com/containers/storage/pkg/archive"
"github.com/containers/storage/pkg/chrootarchive"
@ -721,6 +722,30 @@ func (c *Container) prepare() (err error) {
return nil
}
// cleanupCgroup cleans up residual CGroups after container execution
// This is a no-op for the systemd cgroup driver
func (c *Container) cleanupCgroups() error {
if c.runtime.config.CgroupManager == SystemdCgroupsManager {
return nil
}
// Remove the base path of the container's cgroups
path := filepath.Join(c.config.CgroupParent, fmt.Sprintf("libpod-%s", c.ID()))
logrus.Debugf("Removing CGroup %s", path)
cgroup, err := cgroups.Load(cgroups.V1, cgroups.StaticPath(path))
if err != nil {
return err
}
if err := cgroup.Delete(); err != nil {
return err
}
return nil
}
// cleanupNetwork unmounts and cleans up the container's network
func (c *Container) cleanupNetwork() error {
// Stop the container's network namespace (if it has one)
@ -764,11 +789,21 @@ func (c *Container) cleanupStorage() error {
func (c *Container) cleanup() error {
var lastError error
logrus.Debugf("Cleaning up container %s", c.ID())
// Clean up network namespace, if present
if err := c.cleanupNetwork(); err != nil {
lastError = nil
}
if err := c.cleanupCgroups(); err != nil {
if lastError != nil {
logrus.Errorf("Error cleaning up container %s CGroups: %v", c.ID(), err)
} else {
lastError = err
}
}
// Unmount storage
if err := c.cleanupStorage(); err != nil {
if lastError != nil {

View File

@ -219,6 +219,11 @@ func (r *Runtime) removeContainer(c *Container, force bool) error {
return errors.Wrapf(ErrCtrExists, "container %s has dependent containers which must be removed before it: %s", c.ID(), depsStr)
}
// Tear down the container's cgroups (if they exist)
if err := c.cleanupCgroups(); err != nil {
return err
}
// Stop the container's network namespace (if it has one)
if err := r.teardownNetNS(c); err != nil {
return err