Skip to content

Commit

Permalink
Merge pull request #523 from openSUSE/image-all-remove
Browse files Browse the repository at this point in the history
Add `--all, -a` flag to image removal (`rmi`)
  • Loading branch information
k8s-ci-robot authored Sep 5, 2019
2 parents a1e9cf7 + c961c1b commit 418de71
Showing 1 changed file with 47 additions and 16 deletions.
63 changes: 47 additions & 16 deletions cmd/crictl/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,39 +285,70 @@ var imageStatusCommand = cli.Command{
}

var removeImageCommand = cli.Command{
Name: "rmi",
Usage: "Remove one or more images",
ArgsUsage: "IMAGE-ID [IMAGE-ID...]",
Action: func(context *cli.Context) error {
if context.NArg() == 0 {
return cli.ShowSubcommandHelp(context)
}
imageClient, conn, err := getImageClient(context)
Name: "rmi",
Usage: "Remove one or more images",
ArgsUsage: "IMAGE-ID [IMAGE-ID...]",
SkipArgReorder: true,
UseShortOptionHandling: true,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "all, a",
Usage: "Remove all images",
},
},
Action: func(ctx *cli.Context) error {
imageClient, conn, err := getImageClient(ctx)
if err != nil {
return err
}
defer closeConnection(context, conn)
defer closeConnection(ctx, conn)

for i := 0; i < context.NArg(); i++ {
id := context.Args().Get(i)
ids := ctx.Args()
if ctx.Bool("all") {
r, err := imageClient.ListImages(context.Background(),
&pb.ListImagesRequest{})
if err != nil {
return err
}
ids = nil
for _, img := range r.GetImages() {
ids = append(ids, img.GetId())
}
}

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

var verbose = false
status, err := ImageStatus(imageClient, id, verbose)
errored := false
for _, id := range ids {
status, err := ImageStatus(imageClient, id, false)
if err != nil {
return fmt.Errorf("image status request for %q failed: %v", id, err)
logrus.Errorf("image status request for %q failed: %v", id, err)
errored = true
continue
}
if status.Image == nil {
return fmt.Errorf("no such image %s", id)
logrus.Errorf("no such image %s", id)
errored = true
continue
}

_, err = RemoveImage(imageClient, id)
if err != nil {
return fmt.Errorf("error of removing image %q: %v", id, err)
logrus.Errorf("error of removing image %q: %v", id, err)
errored = true
continue
}
for _, repoTag := range status.Image.RepoTags {
fmt.Printf("Deleted: %s\n", repoTag)
}
}

if errored {
return fmt.Errorf("unable to remove the image(s)")
}

return nil
},
}
Expand Down

0 comments on commit 418de71

Please sign in to comment.