Make CGroups cleanup optional on whether they exist

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

Closes: #981
Approved by: baude
This commit is contained in:
Matthew Heon
2018-06-21 11:09:17 -04:00
committed by Atomic Bot
parent d2f981fd0b
commit c3602075ec
5 changed files with 36 additions and 7 deletions

View File

@ -167,6 +167,9 @@ type containerState struct {
// the path of the file on disk outside the container // the path of the file on disk outside the container
BindMounts map[string]string `json:"bindMounts,omitempty"` BindMounts map[string]string `json:"bindMounts,omitempty"`
// CgroupCreated indicates that the container has created a cgroup
CgroupCreated bool `json:"cgroupCreated,omitempty"`
// UserNSRoot is the directory used as root for the container when using // UserNSRoot is the directory used as root for the container when using
// user namespaces. // user namespaces.
UserNSRoot string `json:"userNSRoot,omitempty"` UserNSRoot string `json:"userNSRoot,omitempty"`

View File

@ -805,6 +805,8 @@ func (c *Container) Refresh(ctx context.Context) error {
return err return err
} }
logrus.Debugf("Resetting state of container %s", c.ID())
// We've finished unwinding the container back to its initial state // We've finished unwinding the container back to its initial state
// Now safe to refresh container state // Now safe to refresh container state
if err := resetState(c.state); err != nil { if err := resetState(c.state); err != nil {

View File

@ -329,15 +329,13 @@ func resetState(state *containerState) error {
state.Interfaces = nil state.Interfaces = nil
state.Routes = nil state.Routes = nil
state.BindMounts = make(map[string]string) state.BindMounts = make(map[string]string)
state.CgroupCreated = false
return nil return nil
} }
// Refresh refreshes the container's state after a restart // Refresh refreshes the container's state after a restart
func (c *Container) refresh() error { func (c *Container) refresh() error {
c.lock.Lock()
defer c.lock.Unlock()
if !c.valid { if !c.valid {
return errors.Wrapf(ErrCtrRemoved, "container %s is not valid - may have been removed", c.ID()) return errors.Wrapf(ErrCtrRemoved, "container %s is not valid - may have been removed", c.ID())
} }
@ -567,6 +565,7 @@ func (c *Container) init(ctx context.Context) error {
logrus.Debugf("Created container %s in OCI runtime", c.ID()) logrus.Debugf("Created container %s in OCI runtime", c.ID())
c.state.State = ContainerStateCreated c.state.State = ContainerStateCreated
c.state.CgroupCreated = true
if err := c.save(); err != nil { if err := c.save(); err != nil {
return err return err
@ -812,6 +811,11 @@ func (c *Container) prepare() (err error) {
// cleanupCgroup cleans up residual CGroups after container execution // cleanupCgroup cleans up residual CGroups after container execution
// This is a no-op for the systemd cgroup driver // This is a no-op for the systemd cgroup driver
func (c *Container) cleanupCgroups() error { func (c *Container) cleanupCgroups() error {
if !c.state.CgroupCreated {
logrus.Debugf("Cgroups are not present, ignoring...")
return nil
}
if c.runtime.config.CgroupManager == SystemdCgroupsManager { if c.runtime.config.CgroupManager == SystemdCgroupsManager {
return nil return nil
} }
@ -836,11 +840,22 @@ func (c *Container) cleanupCgroups() error {
return err return err
} }
c.state.CgroupCreated = false
if c.valid {
return c.save()
}
return nil return nil
} }
// cleanupNetwork unmounts and cleans up the container's network // cleanupNetwork unmounts and cleans up the container's network
func (c *Container) cleanupNetwork() error { func (c *Container) cleanupNetwork() error {
if c.state.NetNS == nil {
logrus.Debugf("Network is already cleaned up, skipping...")
return nil
}
// Stop the container's network namespace (if it has one) // Stop the container's network namespace (if it has one)
if err := c.runtime.teardownNetNS(c); err != nil { if err := c.runtime.teardownNetNS(c); err != nil {
logrus.Errorf("unable to cleanup network for container %s: %q", c.ID(), err) logrus.Errorf("unable to cleanup network for container %s: %q", c.ID(), err)
@ -850,13 +865,19 @@ func (c *Container) cleanupNetwork() error {
c.state.IPs = nil c.state.IPs = nil
c.state.Interfaces = nil c.state.Interfaces = nil
c.state.Routes = nil c.state.Routes = nil
if c.valid {
return c.save() return c.save()
}
return nil
} }
// cleanupStorage unmounts and cleans up the container's root filesystem // cleanupStorage unmounts and cleans up the container's root filesystem
func (c *Container) cleanupStorage() error { func (c *Container) cleanupStorage() error {
if !c.state.Mounted { if !c.state.Mounted {
// Already unmounted, do nothing // Already unmounted, do nothing
logrus.Debugf("Storage is already unmounted, skipping...")
return nil return nil
} }

View File

@ -127,9 +127,6 @@ func (p *Pod) save() error {
// Refresh a pod's state after restart // Refresh a pod's state after restart
func (p *Pod) refresh() error { func (p *Pod) refresh() error {
p.lock.Lock()
defer p.lock.Unlock()
if !p.valid { if !p.valid {
return ErrPodRemoved return ErrPodRemoved
} }

View File

@ -570,14 +570,20 @@ func (r *Runtime) refresh(alivePath string) error {
return errors.Wrapf(err, "error retrieving all pods from state") return errors.Wrapf(err, "error retrieving all pods from state")
} }
for _, ctr := range ctrs { for _, ctr := range ctrs {
ctr.lock.Lock()
if err := ctr.refresh(); err != nil { if err := ctr.refresh(); err != nil {
ctr.lock.Unlock()
return err return err
} }
ctr.lock.Unlock()
} }
for _, pod := range pods { for _, pod := range pods {
pod.lock.Lock()
if err := pod.refresh(); err != nil { if err := pod.refresh(); err != nil {
pod.lock.Unlock()
return err return err
} }
pod.lock.Unlock()
} }
// Create a file indicating the runtime is alive and ready // Create a file indicating the runtime is alive and ready