diff options
Diffstat (limited to 'libpod')
-rw-r--r-- | libpod/container.go | 7 | ||||
-rw-r--r-- | libpod/container_api.go | 59 | ||||
-rw-r--r-- | libpod/container_internal.go | 113 | ||||
-rw-r--r-- | libpod/image/image.go | 14 | ||||
-rw-r--r-- | libpod/oci.go | 16 | ||||
-rw-r--r-- | libpod/pod_api.go | 4 | ||||
-rw-r--r-- | libpod/runtime.go | 23 | ||||
-rw-r--r-- | libpod/runtime_ctr.go | 14 | ||||
-rw-r--r-- | libpod/runtime_pod_linux.go | 8 | ||||
-rw-r--r-- | libpod/stats.go | 7 | ||||
-rw-r--r-- | libpod/util.go | 25 |
11 files changed, 179 insertions, 111 deletions
diff --git a/libpod/container.go b/libpod/container.go index 0b1879208..55a0f3a2c 100644 --- a/libpod/container.go +++ b/libpod/container.go @@ -36,6 +36,9 @@ const ( ContainerStateStopped ContainerStatus = iota // ContainerStatePaused indicates that the container has been paused ContainerStatePaused ContainerStatus = iota + // ContainerStateExited indicates the the container has stopped and been + // cleaned up + ContainerStateExited ContainerStatus = iota ) // CgroupfsDefaultCgroupParent is the cgroup parent for CGroupFS in libpod @@ -354,9 +357,11 @@ func (t ContainerStatus) String() string { case ContainerStateRunning: return "running" case ContainerStateStopped: - return "exited" + return "stopped" case ContainerStatePaused: return "paused" + case ContainerStateExited: + return "exited" } return "bad state" } diff --git a/libpod/container_api.go b/libpod/container_api.go index fc2058de6..192ccd347 100644 --- a/libpod/container_api.go +++ b/libpod/container_api.go @@ -32,7 +32,8 @@ func (c *Container) Init(ctx context.Context) (err error) { } if !(c.state.State == ContainerStateConfigured || - c.state.State == ContainerStateStopped) { + c.state.State == ContainerStateStopped || + c.state.State == ContainerStateExited) { return errors.Wrapf(ErrCtrExists, "container %s has already been created in runtime", c.ID()) } @@ -50,7 +51,7 @@ func (c *Container) Init(ctx context.Context) (err error) { } defer func() { if err != nil { - if err2 := c.cleanup(); err2 != nil { + if err2 := c.cleanup(ctx); err2 != nil { logrus.Errorf("error cleaning up container %s: %v", c.ID(), err2) } } @@ -84,7 +85,8 @@ func (c *Container) Start(ctx context.Context) (err error) { // Container must be created or stopped to be started if !(c.state.State == ContainerStateConfigured || c.state.State == ContainerStateCreated || - c.state.State == ContainerStateStopped) { + c.state.State == ContainerStateStopped || + c.state.State == ContainerStateExited) { return errors.Wrapf(ErrCtrStateInvalid, "container %s must be in Created or Stopped state to be started", c.ID()) } @@ -102,7 +104,7 @@ func (c *Container) Start(ctx context.Context) (err error) { } defer func() { if err != nil { - if err2 := c.cleanup(); err2 != nil { + if err2 := c.cleanup(ctx); err2 != nil { logrus.Errorf("error cleaning up container %s: %v", c.ID(), err2) } } @@ -113,8 +115,9 @@ func (c *Container) Start(ctx context.Context) (err error) { if err := c.reinit(ctx); err != nil { return err } - } else if c.state.State == ContainerStateConfigured { - // Or initialize it for the first time if necessary + } else if c.state.State == ContainerStateConfigured || + c.state.State == ContainerStateExited { + // Or initialize it if necessary if err := c.init(ctx); err != nil { return err } @@ -147,7 +150,8 @@ func (c *Container) StartAndAttach(ctx context.Context, streams *AttachStreams, // Container must be created or stopped to be started if !(c.state.State == ContainerStateConfigured || c.state.State == ContainerStateCreated || - c.state.State == ContainerStateStopped) { + c.state.State == ContainerStateStopped || + c.state.State == ContainerStateExited) { return nil, errors.Wrapf(ErrCtrStateInvalid, "container %s must be in Created or Stopped state to be started", c.ID()) } @@ -165,7 +169,7 @@ func (c *Container) StartAndAttach(ctx context.Context, streams *AttachStreams, } defer func() { if err != nil { - if err2 := c.cleanup(); err2 != nil { + if err2 := c.cleanup(ctx); err2 != nil { logrus.Errorf("error cleaning up container %s: %v", c.ID(), err2) } } @@ -176,8 +180,9 @@ func (c *Container) StartAndAttach(ctx context.Context, streams *AttachStreams, if err := c.reinit(ctx); err != nil { return nil, err } - } else if c.state.State == ContainerStateConfigured { - // Or initialize it for the first time if necessary + } else if c.state.State == ContainerStateConfigured || + c.state.State == ContainerStateExited { + // Or initialize it if necessary if err := c.init(ctx); err != nil { return nil, err } @@ -202,26 +207,8 @@ func (c *Container) StartAndAttach(ctx context.Context, streams *AttachStreams, // Default stop timeout is 10 seconds, but can be overridden when the container // is created func (c *Container) Stop() error { - if !c.batched { - c.lock.Lock() - defer c.lock.Unlock() - - if err := c.syncContainer(); err != nil { - return err - } - } - - if c.state.State == ContainerStateConfigured || - c.state.State == ContainerStateUnknown || - c.state.State == ContainerStatePaused { - return errors.Wrapf(ErrCtrStateInvalid, "can only stop created, running, or stopped containers") - } - - if c.state.State == ContainerStateStopped { - return ErrCtrStopped - } - - return c.stop(c.config.StopTimeout) + // Stop with the container's given timeout + return c.StopWithTimeout(c.config.StopTimeout) } // StopWithTimeout is a version of Stop that allows a timeout to be specified @@ -243,7 +230,8 @@ func (c *Container) StopWithTimeout(timeout uint) error { return errors.Wrapf(ErrCtrStateInvalid, "can only stop created, running, or stopped containers") } - if c.state.State == ContainerStateStopped { + if c.state.State == ContainerStateStopped || + c.state.State == ContainerStateExited { return ErrCtrStopped } @@ -431,7 +419,8 @@ func (c *Container) Attach(streams *AttachStreams, keys string, resize <-chan re } if c.state.State != ContainerStateCreated && - c.state.State != ContainerStateRunning { + c.state.State != ContainerStateRunning && + c.state.State != ContainerStateExited { return errors.Wrapf(ErrCtrStateInvalid, "can only attach to created or running containers") } @@ -626,7 +615,7 @@ func (c *Container) WaitWithInterval(waitTimeout time.Duration) (int32, error) { // Cleanup unmounts all mount points in container and cleans up container storage // It also cleans up the network stack -func (c *Container) Cleanup() error { +func (c *Container) Cleanup(ctx context.Context) error { if !c.batched { c.lock.Lock() defer c.lock.Unlock() @@ -645,7 +634,7 @@ func (c *Container) Cleanup() error { return errors.Wrapf(ErrCtrStateInvalid, "container %s has active exec sessions, refusing to clean up", c.ID()) } - return c.cleanup() + return c.cleanup(ctx) } // Batch starts a batch operation on the given container @@ -800,7 +789,7 @@ func (c *Container) Refresh(ctx context.Context) error { // Fire cleanup code one more time unconditionally to ensure we are good // to refresh - if err := c.cleanup(); err != nil { + if err := c.cleanup(ctx); err != nil { return err } diff --git a/libpod/container_internal.go b/libpod/container_internal.go index c88794212..033426817 100644 --- a/libpod/container_internal.go +++ b/libpod/container_internal.go @@ -150,7 +150,8 @@ func (c *Container) syncContainer() error { // If runtime knows about the container, update its status in runtime // And then save back to disk if (c.state.State != ContainerStateUnknown) && - (c.state.State != ContainerStateConfigured) { + (c.state.State != ContainerStateConfigured) && + (c.state.State != ContainerStateExited) { oldState := c.state.State // TODO: optionally replace this with a stat for the exit file if err := c.runtime.ociRuntime.updateContainerStatus(c); err != nil { @@ -422,7 +423,7 @@ func (c *Container) isStopped() (bool, error) { if err != nil { return true, err } - return c.state.State == ContainerStateStopped, nil + return (c.state.State == ContainerStateStopped || c.state.State == ContainerStateExited), nil } // save container state to the database @@ -528,6 +529,8 @@ func (c *Container) init(ctx context.Context) error { logrus.Debugf("Created container %s in OCI runtime", c.ID()) + c.state.ExitCode = 0 + c.state.Exited = false c.state.State = ContainerStateCreated if err := c.save(); err != nil { @@ -537,11 +540,14 @@ func (c *Container) init(ctx context.Context) error { return c.completeNetworkSetup() } -// Reinitialize a container -// Deletes and recreates a container in the runtime -// Should only be done on ContainerStateStopped containers -func (c *Container) reinit(ctx context.Context) error { - logrus.Debugf("Recreating container %s in OCI runtime", c.ID()) +// Clean up a container in the OCI runtime. +// Deletes the container in the runtime, and resets its state to Exited. +// The container can be restarted cleanly after this. +func (c *Container) cleanupRuntime(ctx context.Context) error { + // If the container is not ContainerStateStopped, do nothing + if c.state.State != ContainerStateStopped { + return nil + } // If necessary, delete attach and ctl files if err := c.removeConmonFiles(); err != nil { @@ -552,19 +558,33 @@ func (c *Container) reinit(ctx context.Context) error { return err } - // Our state is now Configured, as we've removed ourself from - // the runtime - // Set and save now to make sure that, if the init() below fails - // we still have a valid state - c.state.State = ContainerStateConfigured - c.state.ExitCode = 0 - c.state.Exited = false - if err := c.save(); err != nil { - return err + // Our state is now Exited, as we've removed ourself from + // the runtime. + c.state.State = ContainerStateExited + + if c.valid { + if err := c.save(); err != nil { + return err + } } logrus.Debugf("Successfully cleaned up container %s", c.ID()) + return nil +} + +// Reinitialize a container. +// Deletes and recreates a container in the runtime. +// Should only be done on ContainerStateStopped containers. +// Not necessary for ContainerStateExited - the container has already been +// removed from the runtime, so init() can proceed freely. +func (c *Container) reinit(ctx context.Context) error { + logrus.Debugf("Recreating container %s in OCI runtime", c.ID()) + + if err := c.cleanupRuntime(ctx); err != nil { + return err + } + // Initialize the container again return c.init(ctx) } @@ -592,7 +612,7 @@ func (c *Container) initAndStart(ctx context.Context) (err error) { } defer func() { if err != nil { - if err2 := c.cleanup(); err2 != nil { + if err2 := c.cleanup(ctx); err2 != nil { logrus.Errorf("error cleaning up container %s: %v", c.ID(), err2) } } @@ -603,28 +623,11 @@ func (c *Container) initAndStart(ctx context.Context) (err error) { if c.state.State == ContainerStateStopped { logrus.Debugf("Recreating container %s in OCI runtime", c.ID()) - // If necessary, delete attach and ctl files - if err := c.removeConmonFiles(); err != nil { - return err - } - - // Delete the container in the runtime - if err := c.runtime.ociRuntime.deleteContainer(c); err != nil { - return errors.Wrapf(err, "error removing container %s from runtime", c.ID()) - } - - // Our state is now Configured, as we've removed ourself from - // the runtime - // Set and save now to make sure that, if the init() below fails - // we still have a valid state - c.state.State = ContainerStateConfigured - if err := c.save(); err != nil { + if err := c.reinit(ctx); err != nil { return err } - } - - // If we are ContainerStateConfigured we need to init() - if c.state.State == ContainerStateConfigured { + } else if c.state.State == ContainerStateConfigured || + c.state.State == ContainerStateExited { if err := c.init(ctx); err != nil { return err } @@ -705,7 +708,7 @@ func (c *Container) restartWithTimeout(ctx context.Context, timeout uint) (err e } defer func() { if err != nil { - if err2 := c.cleanup(); err2 != nil { + if err2 := c.cleanup(ctx); err2 != nil { logrus.Errorf("error cleaning up container %s: %v", c.ID(), err2) } } @@ -716,8 +719,9 @@ func (c *Container) restartWithTimeout(ctx context.Context, timeout uint) (err e if err := c.reinit(ctx); err != nil { return err } - } else if c.state.State == ContainerStateConfigured { - // Initialize the container if it has never been initialized + } else if c.state.State == ContainerStateConfigured || + c.state.State == ContainerStateExited { + // Initialize the container if err := c.init(ctx); err != nil { return err } @@ -826,7 +830,7 @@ func (c *Container) cleanupStorage() error { } // Unmount the a container and free its resources -func (c *Container) cleanup() error { +func (c *Container) cleanup(ctx context.Context) error { var lastError error logrus.Debugf("Cleaning up container %s", c.ID()) @@ -845,6 +849,15 @@ func (c *Container) cleanup() error { } } + // Remove the container from the runtime, if necessary + if err := c.cleanupRuntime(ctx); err != nil { + if lastError != nil { + logrus.Errorf("Error removing container %s from OCI runtime: %v", c.ID(), err) + } else { + lastError = err + } + } + return lastError } @@ -926,9 +939,6 @@ func (c *Container) makeBindMounts() error { if err != nil { return errors.Wrapf(err, "error creating resolv.conf for container %s", c.ID()) } - if err = label.Relabel(newResolv, c.config.MountLabel, false); err != nil { - return errors.Wrapf(err, "error relabeling %q for container %q", newResolv, c.ID) - } c.state.BindMounts["/etc/resolv.conf"] = newResolv // Make /etc/hosts @@ -940,9 +950,6 @@ func (c *Container) makeBindMounts() error { if err != nil { return errors.Wrapf(err, "error creating hosts file for container %s", c.ID()) } - if err = label.Relabel(newHosts, c.config.MountLabel, false); err != nil { - return errors.Wrapf(err, "error relabeling %q for container %q", newHosts, c.ID) - } c.state.BindMounts["/etc/hosts"] = newHosts // Make /etc/hostname @@ -952,9 +959,6 @@ func (c *Container) makeBindMounts() error { if err != nil { return errors.Wrapf(err, "error creating hostname file for container %s", c.ID()) } - if err = label.Relabel(hostnamePath, c.config.MountLabel, false); err != nil { - return errors.Wrapf(err, "error relabeling %q for container %q", hostnamePath, c.ID) - } c.state.BindMounts["/etc/hostname"] = hostnamePath } @@ -1286,7 +1290,7 @@ func (c *Container) setupOCIHooks(ctx context.Context, config *spec.Spec) (exten } } - var allHooks map[string][]spec.Hook + allHooks := make(map[string][]spec.Hook) for _, hDir := range c.runtime.config.HooksDir { manager, err := hooks.New(ctx, []string{hDir}, []string{"poststop"}, lang) if err != nil { @@ -1329,3 +1333,10 @@ func (c *Container) unmount(force bool) error { return nil } + +// getExcludedCGroups returns a string slice of cgroups we want to exclude +// because runc or other components are unaware of them. +func getExcludedCGroups() (excludes []string) { + excludes = []string{"rdma"} + return +} diff --git a/libpod/image/image.go b/libpod/image/image.go index 197a83dc1..f39b1d78d 100644 --- a/libpod/image/image.go +++ b/libpod/image/image.go @@ -744,6 +744,20 @@ func (i *Image) Labels(ctx context.Context) (map[string]string, error) { return imgInspect.Labels, nil } +// GetLabel Returns a case-insensitive match of a given label +func (i *Image) GetLabel(ctx context.Context, label string) (string, error) { + imageLabels, err := i.Labels(ctx) + if err != nil { + return "", err + } + for k, v := range imageLabels { + if strings.ToLower(k) == strings.ToLower(label) { + return v, nil + } + } + return "", nil +} + // Annotations returns the annotations of an image func (i *Image) Annotations(ctx context.Context) (map[string]string, error) { manifest, manifestType, err := i.Manifest(ctx) diff --git a/libpod/oci.go b/libpod/oci.go index 3838394cb..e5db06540 100644 --- a/libpod/oci.go +++ b/libpod/oci.go @@ -457,7 +457,7 @@ func (r *OCIRuntime) updateContainerStatus(ctr *Container) error { if err != nil { if strings.Contains(string(out), "does not exist") { ctr.removeConmonFiles() - ctr.state.State = ContainerStateConfigured + ctr.state.State = ContainerStateExited return nil } return errors.Wrapf(err, "error getting container %s state. stderr/out: %s", ctr.ID(), out) @@ -535,7 +535,7 @@ func (r *OCIRuntime) updateContainerStatus(ctr *Container) error { // Sets time the container was started, but does not save it. func (r *OCIRuntime) startContainer(ctr *Container) error { // TODO: streams should probably *not* be our STDIN/OUT/ERR - redirect to buffers? - if err := utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.path, "start", ctr.ID()); err != nil { + if err := utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, nil, r.path, "start", ctr.ID()); err != nil { return err } @@ -547,7 +547,7 @@ func (r *OCIRuntime) startContainer(ctr *Container) error { // killContainer sends the given signal to the given container func (r *OCIRuntime) killContainer(ctr *Container, signal uint) error { logrus.Debugf("Sending signal %d to container %s", signal, ctr.ID()) - if err := utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.path, "kill", ctr.ID(), fmt.Sprintf("%d", signal)); err != nil { + if err := utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, nil, r.path, "kill", ctr.ID(), fmt.Sprintf("%d", signal)); err != nil { return errors.Wrapf(err, "error sending signal to container %s", ctr.ID()) } @@ -605,7 +605,7 @@ func (r *OCIRuntime) stopContainer(ctr *Container, timeout uint) error { args = []string{"kill", "--all", ctr.ID(), "KILL"} } - if err := utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.path, args...); err != nil { + if err := utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, nil, r.path, args...); err != nil { // Again, check if the container is gone. If it is, exit cleanly. err := unix.Kill(ctr.state.PID, 0) if err == unix.ESRCH { @@ -631,12 +631,12 @@ func (r *OCIRuntime) deleteContainer(ctr *Container) error { // pauseContainer pauses the given container func (r *OCIRuntime) pauseContainer(ctr *Container) error { - return utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.path, "pause", ctr.ID()) + return utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, nil, r.path, "pause", ctr.ID()) } // unpauseContainer unpauses the given container func (r *OCIRuntime) unpauseContainer(ctr *Container) error { - return utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.path, "resume", ctr.ID()) + return utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, nil, r.path, "resume", ctr.ID()) } // execContainer executes a command in a running container @@ -740,7 +740,7 @@ func (r *OCIRuntime) execStopContainer(ctr *Container, timeout uint) error { // Stop using SIGTERM by default // Use SIGSTOP after a timeout logrus.Debugf("Killing all processes in container %s with SIGTERM", ctr.ID()) - if err := utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.path, "kill", "--all", ctr.ID(), "TERM"); err != nil { + if err := utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, nil, r.path, "kill", "--all", ctr.ID(), "TERM"); err != nil { return errors.Wrapf(err, "error sending SIGTERM to container %s processes", ctr.ID()) } @@ -755,7 +755,7 @@ func (r *OCIRuntime) execStopContainer(ctr *Container, timeout uint) error { // Send SIGKILL logrus.Debugf("Killing all processes in container %s with SIGKILL", ctr.ID()) - if err := utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.path, "kill", "--all", ctr.ID(), "KILL"); err != nil { + if err := utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, nil, r.path, "kill", "--all", ctr.ID(), "KILL"); err != nil { return errors.Wrapf(err, "error sending SIGKILL to container %s processes", ctr.ID()) } diff --git a/libpod/pod_api.go b/libpod/pod_api.go index 0c518da0d..3d5512e8c 100644 --- a/libpod/pod_api.go +++ b/libpod/pod_api.go @@ -77,7 +77,7 @@ func (p *Pod) Start(ctx context.Context) (map[string]error, error) { // containers. The container ID is mapped to the error encountered. The error is // set to ErrCtrExists // If both error and the map are nil, all containers were stopped without error -func (p *Pod) Stop(cleanup bool) (map[string]error, error) { +func (p *Pod) Stop(ctx context.Context, cleanup bool) (map[string]error, error) { p.lock.Lock() defer p.lock.Unlock() @@ -118,7 +118,7 @@ func (p *Pod) Stop(cleanup bool) (map[string]error, error) { } if cleanup { - if err := ctr.cleanup(); err != nil { + if err := ctr.cleanup(ctx); err != nil { ctrErrors[ctr.ID()] = err } } diff --git a/libpod/runtime.go b/libpod/runtime.go index fbd4c7529..985af2849 100644 --- a/libpod/runtime.go +++ b/libpod/runtime.go @@ -261,6 +261,25 @@ func getDefaultTmpDir() (string, error) { return filepath.Join(rootlessRuntimeDir, "libpod", "tmp"), nil } +// SetXdgRuntimeDir ensures the XDG_RUNTIME_DIR env variable is set +// containers/image uses XDG_RUNTIME_DIR to locate the auth file. +func SetXdgRuntimeDir(val string) error { + if !rootless.IsRootless() { + return nil + } + if val == "" { + var err error + val, err = GetRootlessRuntimeDir() + if err != nil { + return err + } + } + if err := os.Setenv("XDG_RUNTIME_DIR", val); err != nil { + return errors.Wrapf(err, "cannot set XDG_RUNTIME_DIR") + } + return nil +} + // NewRuntime creates a new container runtime // Options can be passed to override the default configuration for the runtime func NewRuntime(options ...RuntimeOption) (runtime *Runtime, err error) { @@ -297,7 +316,7 @@ func NewRuntime(options ...RuntimeOption) (runtime *Runtime, err error) { // containers/image uses XDG_RUNTIME_DIR to locate the auth file. // So make sure the env variable is set. - err = os.Setenv("XDG_RUNTIME_DIR", runtimeDir) + err = SetXdgRuntimeDir(runtimeDir) if err != nil { return nil, errors.Wrapf(err, "cannot set XDG_RUNTIME_DIR") } @@ -395,7 +414,7 @@ func makeRuntime(runtime *Runtime) (err error) { } if !foundRuntime { return errors.Wrapf(ErrInvalidArg, - "could not find a working runc binary (configured options: %v)", + "could not find a working binary (configured options: %v)", runtime.config.RuntimePath) } diff --git a/libpod/runtime_ctr.go b/libpod/runtime_ctr.go index 6c487e367..4256a84a0 100644 --- a/libpod/runtime_ctr.go +++ b/libpod/runtime_ctr.go @@ -262,7 +262,8 @@ func (r *Runtime) removeContainer(ctx context.Context, c *Container, force bool) } } else if !(c.state.State == ContainerStateConfigured || c.state.State == ContainerStateCreated || - c.state.State == ContainerStateStopped) { + c.state.State == ContainerStateStopped || + c.state.State == ContainerStateExited) { return errors.Wrapf(ErrCtrStateInvalid, "cannot remove container %s as it is %s - running or paused containers cannot be removed", c.ID(), c.state.State.String()) } @@ -311,7 +312,7 @@ func (r *Runtime) removeContainer(ctx context.Context, c *Container, force bool) c.valid = false // Clean up network namespace, cgroups, mounts - if err := c.cleanup(); err != nil { + if err := c.cleanup(ctx); err != nil { if cleanupErr == nil { cleanupErr = err } else { @@ -332,10 +333,11 @@ func (r *Runtime) removeContainer(ctx context.Context, c *Container, force bool) label.ReleaseLabel(c.ProcessLabel()) r.reserveLabels() } - // Delete the container - // Only do this if we're not ContainerStateConfigured - if we are, - // we haven't been created in the runtime yet - if c.state.State != ContainerStateConfigured { + // Delete the container. + // Not needed in Configured and Exited states, where the container + // doesn't exist in the runtime + if c.state.State != ContainerStateConfigured && + c.state.State != ContainerStateExited { if err := c.delete(ctx); err != nil { if cleanupErr == nil { cleanupErr = err diff --git a/libpod/runtime_pod_linux.go b/libpod/runtime_pod_linux.go index dd57007e0..eb3d471dd 100644 --- a/libpod/runtime_pod_linux.go +++ b/libpod/runtime_pod_linux.go @@ -222,7 +222,7 @@ func (r *Runtime) removePod(ctx context.Context, p *Pod, removeCtrs, force bool) // As we have guaranteed their dependencies are in the pod for _, ctr := range ctrs { // Clean up network namespace, cgroups, mounts - if err := ctr.cleanup(); err != nil { + if err := ctr.cleanup(ctx); err != nil { return err } @@ -233,7 +233,8 @@ func (r *Runtime) removePod(ctx context.Context, p *Pod, removeCtrs, force bool) // Delete the container from runtime (only if we are not // ContainerStateConfigured) - if ctr.state.State != ContainerStateConfigured { + if ctr.state.State != ContainerStateConfigured && + ctr.state.State != ContainerStateExited { if err := ctr.delete(ctx); err != nil { return err } @@ -264,7 +265,8 @@ func (r *Runtime) removePod(ctx context.Context, p *Pod, removeCtrs, force bool) } case CgroupfsCgroupsManager: // Delete the cgroupfs cgroup - cgroup, err := cgroups.Load(cgroups.V1, cgroups.StaticPath(p.state.CgroupPath)) + v1CGroups := GetV1CGroups(getExcludedCGroups()) + cgroup, err := cgroups.Load(v1CGroups, cgroups.StaticPath(p.state.CgroupPath)) if err != nil && err != cgroups.ErrCgroupDeleted { return err } else if err == nil { diff --git a/libpod/stats.go b/libpod/stats.go index 9d5efd993..c58a46135 100644 --- a/libpod/stats.go +++ b/libpod/stats.go @@ -33,13 +33,14 @@ func (c *Container) GetContainerStats(previousStats *ContainerStats) (*Container if err != nil { return nil, err } - - cgroup, err := cgroups.Load(cgroups.V1, cgroups.StaticPath(cgroupPath)) + v1CGroups := GetV1CGroups(getExcludedCGroups()) + cgroup, err := cgroups.Load(v1CGroups, cgroups.StaticPath(cgroupPath)) if err != nil { return stats, errors.Wrapf(err, "unable to load cgroup at %s", cgroupPath) } - cgroupStats, err := cgroup.Stat() + // Ubuntu does not have swap memory in cgroups because swap is often not enabled. + cgroupStats, err := cgroup.Stat(cgroups.IgnoreNotExist) if err != nil { return stats, errors.Wrapf(err, "unable to obtain cgroup stats") } diff --git a/libpod/util.go b/libpod/util.go index 17325f6e4..3b51e4fcc 100644 --- a/libpod/util.go +++ b/libpod/util.go @@ -9,8 +9,10 @@ import ( "strings" "time" + "github.com/containerd/cgroups" "github.com/containers/image/signature" "github.com/containers/image/types" + "github.com/containers/libpod/pkg/util" spec "github.com/opencontainers/runtime-spec/specs-go" "github.com/pkg/errors" ) @@ -160,3 +162,26 @@ func validPodNSOption(p *Pod, ctrPod string) error { } return nil } + +// GetV1CGroups gets the V1 cgroup subsystems and then "filters" +// out any subsystems that are provided by the caller. Passing nil +// for excludes will return the subsystems unfiltered. +//func GetV1CGroups(excludes []string) ([]cgroups.Subsystem, error) { +func GetV1CGroups(excludes []string) cgroups.Hierarchy { + return func() ([]cgroups.Subsystem, error) { + var filtered []cgroups.Subsystem + + subSystem, err := cgroups.V1() + if err != nil { + return nil, err + } + for _, s := range subSystem { + // If the name of the subsystem is not in the list of excludes, then + // add it as a keeper. + if !util.StringInSlice(string(s.Name()), excludes) { + filtered = append(filtered, s) + } + } + return filtered, nil + } +} |