forked from cri-o/cri-o
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Kpod rm removes a container from the system Signed-off-by: Ryan Cole <rcyoalne@gmail.com> Signed-off-by: Daniel J Walsh <dwalsh@redhat.com> Signed-off-by: umohnani8 <umohnani@redhat.com>
- Loading branch information
Showing
14 changed files
with
303 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/kubernetes-incubator/cri-o/libkpod" | ||
"github.com/pkg/errors" | ||
"github.com/urfave/cli" | ||
) | ||
|
||
var ( | ||
rmFlags = []cli.Flag{ | ||
cli.BoolFlag{ | ||
Name: "force, f", | ||
Usage: "Force removal of a running container. The default is false", | ||
}, | ||
} | ||
rmDescription = "Remove one or more containers" | ||
rmCommand = cli.Command{ | ||
Name: "rm", | ||
Usage: fmt.Sprintf(`kpod rm will remove one or more containers from the host. The container name or ID can be used. | ||
This does not remove images. Running containers will not be removed without the -f option.`), | ||
Description: rmDescription, | ||
Flags: rmFlags, | ||
Action: rmCmd, | ||
ArgsUsage: "", | ||
} | ||
) | ||
|
||
// saveCmd saves the image to either docker-archive or oci | ||
func rmCmd(c *cli.Context) error { | ||
args := c.Args() | ||
if len(args) == 0 { | ||
return errors.Errorf("specify one or more containers to remove") | ||
} | ||
|
||
config, err := getConfig(c) | ||
if err != nil { | ||
return errors.Wrapf(err, "could not get config") | ||
} | ||
server, err := libkpod.New(config) | ||
if err != nil { | ||
return errors.Wrapf(err, "could not get container server") | ||
} | ||
defer server.Shutdown() | ||
err = server.Update() | ||
if err != nil { | ||
return errors.Wrapf(err, "could not update list of containers") | ||
} | ||
force := c.Bool("force") | ||
|
||
for _, container := range c.Args() { | ||
id, err2 := server.Remove(container, force) | ||
if err2 != nil { | ||
if err == nil { | ||
err = err2 | ||
} else { | ||
err = errors.Wrapf(err, "%v. Stop the container before attempting removal or use -f\n", err2) | ||
} | ||
} else { | ||
fmt.Println(id) | ||
} | ||
} | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
% kpod(1) kpod-rm - Remove one or more containers | ||
% Ryan Cole | ||
# kpod-rm "1" "August 2017" "kpod" | ||
|
||
## NAME | ||
kpod rm - Remove one or more containers | ||
|
||
## SYNOPSIS | ||
**kpod** **rm** [*options* [...]] container | ||
|
||
## DESCRIPTION | ||
Kpod rm will remove one or more containers from the host. The container name or ID can be used. This does not remove images. Running containers will not be removed without the -f option | ||
|
||
## OPTIONS | ||
|
||
**--force, f** | ||
|
||
Force the removal of a running container | ||
|
||
|
||
## EXAMPLE | ||
|
||
kpod rm mywebserver | ||
|
||
kpod rm -f 860a4b23 | ||
|
||
## SEE ALSO | ||
kpod(1), kpod-rmi(1) | ||
|
||
## HISTORY | ||
August 2017, Originally compiled by Ryan Cole <rycole@redhat.com> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package libkpod | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/kubernetes-incubator/cri-o/oci" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// Remove removes a container | ||
func (c *ContainerServer) Remove(container string, force bool) (string, error) { | ||
ctr, err := c.LookupContainer(container) | ||
if err != nil { | ||
return "", err | ||
} | ||
ctrID := ctr.ID() | ||
|
||
cState := c.runtime.ContainerStatus(ctr) | ||
if cState.Status == oci.ContainerStateCreated || cState.Status == oci.ContainerStateRunning { | ||
if force { | ||
_, err = c.ContainerStop(container, -1) | ||
if err != nil { | ||
return "", errors.Wrapf(err, "unable to stop container %s", ctrID) | ||
} | ||
} else { | ||
return "", errors.Errorf("cannot remove running container %s", ctrID) | ||
} | ||
} | ||
|
||
if err := c.runtime.DeleteContainer(ctr); err != nil { | ||
return "", errors.Wrapf(err, "failed to delete container %s", ctrID) | ||
} | ||
if err := os.Remove(filepath.Join(c.Config().RuntimeConfig.ContainerExitsDir, ctrID)); err != nil && !os.IsNotExist(err) { | ||
return "", errors.Wrapf(err, "failed to remove container exit file %s", ctrID) | ||
} | ||
c.RemoveContainer(ctr) | ||
|
||
if err := c.storageRuntimeServer.DeleteContainer(ctrID); err != nil { | ||
return "", errors.Wrapf(err, "failed to delete storage for container %s", ctrID) | ||
} | ||
|
||
c.ReleaseContainerName(ctr.Name()) | ||
|
||
if err := c.ctrIDIndex.Delete(ctrID); err != nil { | ||
return "", err | ||
} | ||
return ctrID, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package libkpod | ||
|
||
import ( | ||
"github.com/kubernetes-incubator/cri-o/oci" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// ContainerStop stops a running container with a grace period (i.e., timeout). | ||
func (c *ContainerServer) ContainerStop(container string, timeout int64) (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.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()) | ||
} | ||
} | ||
|
||
c.ContainerStateToDisk(ctr) | ||
|
||
return ctr.ID(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,19 @@ | ||
package server | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/kubernetes-incubator/cri-o/oci" | ||
"github.com/sirupsen/logrus" | ||
"golang.org/x/net/context" | ||
pb "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime" | ||
) | ||
|
||
// StopContainer stops a running container with a grace period (i.e., timeout). | ||
func (s *Server) StopContainer(ctx context.Context, req *pb.StopContainerRequest) (*pb.StopContainerResponse, error) { | ||
logrus.Debugf("StopContainerRequest %+v", req) | ||
c, err := s.GetContainerFromRequest(req.ContainerId) | ||
_, err := s.ContainerServer.ContainerStop(req.ContainerId, req.Timeout) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cStatus := s.Runtime().ContainerStatus(c) | ||
if cStatus.Status != oci.ContainerStateStopped { | ||
if err := s.Runtime().StopContainer(c, req.Timeout); err != nil { | ||
return nil, fmt.Errorf("failed to stop container %s: %v", c.ID(), err) | ||
} | ||
if err := s.StorageRuntimeServer().StopContainer(c.ID()); err != nil { | ||
return nil, fmt.Errorf("failed to unmount container %s: %v", c.ID(), err) | ||
} | ||
} | ||
|
||
s.ContainerStateToDisk(c) | ||
|
||
resp := &pb.StopContainerResponse{} | ||
logrus.Debugf("StopContainerResponse %s: %+v", c.ID(), resp) | ||
logrus.Debugf("StopContainerResponse %s: %+v", req.ContainerId, resp) | ||
return resp, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.