Skip to content

Commit

Permalink
Add -a (remove all) option to rmp
Browse files Browse the repository at this point in the history
It is now possible to remove all pod sandboxes, with or without
forcing. This means a `crictl rmp -af` allows the user to cleanly wipe
the containers storage via the runtime.

Beside this, we now try to remove further and do not abort on a single
failure.

Signed-off-by: Sascha Grunert <[email protected]>
  • Loading branch information
saschagrunert committed Aug 29, 2019
1 parent de7ab55 commit 2346b4f
Showing 1 changed file with 44 additions and 12 deletions.
56 changes: 44 additions & 12 deletions cmd/crictl/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,47 +103,79 @@ var stopPodCommand = cli.Command{
}

var removePodCommand = cli.Command{
Name: "rmp",
Usage: "Remove one or more pods",
ArgsUsage: "POD-ID [POD-ID...]",
Name: "rmp",
Usage: "Remove one or more pods",
ArgsUsage: "POD-ID [POD-ID...]",
SkipArgReorder: true,
UseShortOptionHandling: true,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "force, f",
Usage: "Force removal of the pod sandbox, disregarding if running",
},
cli.BoolFlag{
Name: "all, a",
Usage: "Remove all pods",
},
},
Action: func(ctx *cli.Context) error {
if ctx.NArg() == 0 {
return cli.ShowSubcommandHelp(ctx)
}
runtimeClient, runtimeConn, err := getRuntimeClient(ctx)
if err != nil {
return err
}
defer closeConnection(ctx, runtimeConn)
for i := 0; i < ctx.NArg(); i++ {
id := ctx.Args().Get(i)

ids := ctx.Args()
if ctx.Bool("all") {
r, err := runtimeClient.ListPodSandbox(context.Background(),
&pb.ListPodSandboxRequest{})
if err != nil {
return err
}
ids = nil
for _, sb := range r.GetItems() {
ids = append(ids, sb.GetId())
}
}

if len(ids) == 0 {
return cli.ShowSubcommandHelp(ctx)
}

errored := false
for _, id := range ids {
resp, err := runtimeClient.PodSandboxStatus(context.Background(),
&pb.PodSandboxStatusRequest{PodSandboxId: id})
if err != nil {
return err
logrus.Error(err)
errored = true
continue
}
if resp.Status.State == pb.PodSandboxState_SANDBOX_READY {
if ctx.Bool("force") {
if err := StopPodSandbox(runtimeClient, id); err != nil {
return fmt.Errorf("stopping the pod sandbox %q failed: %v", id, err)
logrus.Errorf("stopping the pod sandbox %q failed: %v", id, err)
errored = true
continue
}
} else {
return fmt.Errorf("pod sandbox %q is running, please stop it first", id)
logrus.Errorf("pod sandbox %q is running, please stop it first", id)
errored = true
continue
}
}

err = RemovePodSandbox(runtimeClient, id)
if err != nil {
return fmt.Errorf("removing the pod sandbox %q failed: %v", id, err)
logrus.Errorf("removing the pod sandbox %q failed: %v", id, err)
errored = true
continue
}
}

if errored {
return fmt.Errorf("unable to remove sandbox(es)")
}
return nil
},
}
Expand Down

0 comments on commit 2346b4f

Please sign in to comment.