Skip to content

Commit

Permalink
Merge pull request #555 from openSUSE/prune
Browse files Browse the repository at this point in the history
Add rmi --prune command
  • Loading branch information
k8s-ci-robot authored Nov 12, 2019
2 parents 7397819 + 25e2dc5 commit c78dd47
Showing 1 changed file with 60 additions and 8 deletions.
68 changes: 60 additions & 8 deletions cmd/crictl/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@ var removeImageCommand = cli.Command{
Name: "all, a",
Usage: "Remove all images",
},
cli.BoolFlag{
Name: "prune, p",
Usage: "Remove all unused images",
},
},
Action: func(ctx *cli.Context) error {
imageClient, conn, err := getImageClient(ctx)
Expand All @@ -304,25 +308,69 @@ var removeImageCommand = cli.Command{
}
defer closeConnection(ctx, conn)

ids := ctx.Args()
if ctx.Bool("all") {
ids := map[string]bool{}
for _, id := range ctx.Args() {
logrus.Debugf("User specified image to be removed: %v", id)
ids[id] = true
}

all := ctx.Bool("all")
prune := ctx.Bool("prune")

// Add all available images to the ID selector
if all || prune {
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())
logrus.Debugf("Adding image to be removed: %v", img.GetId())
ids[img.GetId()] = true
}
}

// On prune, remove images which are in use from the ID selector
if prune {
runtimeClient, conn, err := getRuntimeClient(ctx)
if err != nil {
return err
}
defer closeConnection(ctx, conn)

// Container images
c, err := runtimeClient.ListContainers(
context.Background(), &pb.ListContainersRequest{},
)
if err != nil {
return err
}
for _, container := range c.GetContainers() {
img := container.GetImage().Image
imageStatus, err := ImageStatus(imageClient, img, false)
if err != nil {
logrus.Errorf(
"image status request for %q failed: %v",
img, err,
)
continue
}
id := imageStatus.GetImage().GetId()
logrus.Debugf("Excluding in use container image: %v", id)
ids[id] = false
}
}

if len(ids) == 0 {
return cli.ShowSubcommandHelp(ctx)
logrus.Info("No images to remove")
return nil
}

errored := false
for _, id := range ids {
for id, remove := range ids {
if !remove {
continue
}
status, err := ImageStatus(imageClient, id, false)
if err != nil {
logrus.Errorf("image status request for %q failed: %v", id, err)
Expand All @@ -337,8 +385,12 @@ var removeImageCommand = cli.Command{

_, err = RemoveImage(imageClient, id)
if err != nil {
logrus.Errorf("error of removing image %q: %v", id, err)
errored = true
// We ignore further errors on prune because there might be
// races
if !prune {
logrus.Errorf("error of removing image %q: %v", id, err)
errored = true
}
continue
}
for _, repoTag := range status.Image.RepoTags {
Expand Down

0 comments on commit c78dd47

Please sign in to comment.