Add kpod pause
and kpod unpause
Implement the ability to pause and unpause running containers. Signed-off-by: Daniel J Walsh <dwalsh@redhat.com> Signed-off-by: TomSweeneyRedHat <tsweeney@redhat.com>
This commit is contained in:
parent
45747cc5d0
commit
9db7cf1370
13 changed files with 444 additions and 11 deletions
46
libkpod/pause.go
Normal file
46
libkpod/pause.go
Normal file
|
@ -0,0 +1,46 @@
|
|||
package libkpod
|
||||
|
||||
import (
|
||||
"github.com/kubernetes-incubator/cri-o/oci"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// ContainerPause pauses a running container.
|
||||
func (c *ContainerServer) ContainerPause(container string) (string, error) {
|
||||
ctr, err := c.LookupContainer(container)
|
||||
if err != nil {
|
||||
return "", errors.Wrapf(err, "failed to find container %s", container)
|
||||
}
|
||||
|
||||
cStatus := c.runtime.ContainerStatus(ctr)
|
||||
if cStatus.Status != oci.ContainerStatePaused {
|
||||
if err := c.runtime.PauseContainer(ctr); err != nil {
|
||||
return "", errors.Wrapf(err, "failed to pause container %s", ctr.ID())
|
||||
}
|
||||
c.ContainerStateToDisk(ctr)
|
||||
} else {
|
||||
return "", errors.Wrapf(err, "container %s is already paused", ctr.ID())
|
||||
}
|
||||
|
||||
return ctr.ID(), nil
|
||||
}
|
||||
|
||||
// ContainerUnpause unpauses a running container with a grace period (i.e., timeout).
|
||||
func (c *ContainerServer) ContainerUnpause(container string) (string, error) {
|
||||
ctr, err := c.LookupContainer(container)
|
||||
if err != nil {
|
||||
return "", errors.Wrapf(err, "failed to find container %s", container)
|
||||
}
|
||||
|
||||
cStatus := c.runtime.ContainerStatus(ctr)
|
||||
if cStatus.Status == oci.ContainerStatePaused {
|
||||
if err := c.runtime.UnpauseContainer(ctr); err != nil {
|
||||
return "", errors.Wrapf(err, "failed to unpause container %s", ctr.ID())
|
||||
}
|
||||
c.ContainerStateToDisk(ctr)
|
||||
} else {
|
||||
return "", errors.Wrapf(err, "the container %s is not paused", ctr.ID())
|
||||
}
|
||||
|
||||
return ctr.ID(), nil
|
||||
}
|
|
@ -16,8 +16,11 @@ func (c *ContainerServer) Remove(container string, force bool) (string, error) {
|
|||
}
|
||||
ctrID := ctr.ID()
|
||||
|
||||
cState := c.runtime.ContainerStatus(ctr)
|
||||
if cState.Status == oci.ContainerStateCreated || cState.Status == oci.ContainerStateRunning {
|
||||
cStatus := c.runtime.ContainerStatus(ctr)
|
||||
switch cStatus.Status {
|
||||
case oci.ContainerStatePaused:
|
||||
return "", errors.Errorf("cannot remove paused container %s", ctrID)
|
||||
case oci.ContainerStateCreated, oci.ContainerStateRunning:
|
||||
if force {
|
||||
_, err = c.ContainerStop(container, -1)
|
||||
if err != nil {
|
||||
|
|
|
@ -11,18 +11,25 @@ func (c *ContainerServer) ContainerStop(container string, timeout int64) (string
|
|||
if err != nil {
|
||||
return "", errors.Wrapf(err, "failed to find container %s", container)
|
||||
}
|
||||
ctrID := ctr.ID()
|
||||
|
||||
cStatus := c.runtime.ContainerStatus(ctr)
|
||||
if cStatus.Status != oci.ContainerStateStopped {
|
||||
if err := c.runtime.StopContainer(ctr, timeout); err != nil {
|
||||
return "", errors.Wrapf(err, "failed to stop container %s", ctr.ID())
|
||||
}
|
||||
if err := c.storageRuntimeServer.StopContainer(ctr.ID()); err != nil {
|
||||
return "", errors.Wrapf(err, "failed to unmount container %s", ctr.ID())
|
||||
switch cStatus.Status {
|
||||
|
||||
case oci.ContainerStatePaused:
|
||||
return "", errors.Errorf("cannot stop paused container %s", ctrID)
|
||||
default:
|
||||
if cStatus.Status != oci.ContainerStateStopped {
|
||||
if err := c.runtime.StopContainer(ctr, timeout); err != nil {
|
||||
return "", errors.Wrapf(err, "failed to stop container %s", ctrID)
|
||||
}
|
||||
if err := c.storageRuntimeServer.StopContainer(ctrID); err != nil {
|
||||
return "", errors.Wrapf(err, "failed to unmount container %s", ctrID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
c.ContainerStateToDisk(ctr)
|
||||
|
||||
return ctr.ID(), nil
|
||||
return ctrID, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue