Skip to content

Commit

Permalink
Allow parallel image removal
Browse files Browse the repository at this point in the history
Allow removing images in parallel goroutines.

Signed-off-by: Sascha Grunert <[email protected]>
  • Loading branch information
saschagrunert committed Oct 16, 2024
1 parent 7abeb52 commit c0e4174
Showing 1 changed file with 30 additions and 35 deletions.
65 changes: 30 additions & 35 deletions cmd/crictl/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"golang.org/x/term"
errorUtils "k8s.io/apimachinery/pkg/util/errors"
internalapi "k8s.io/cri-api/pkg/apis"
pb "k8s.io/cri-api/pkg/apis/runtime/v1"
)
Expand Down Expand Up @@ -466,50 +467,44 @@ var removeImageCommand = &cli.Command{
return cli.ShowSubcommandHelp(cliCtx)
}

errored := false
funcs := []func() error{}
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)
errored = true
continue
}
if status.Image == nil {
logrus.Errorf("no such image %s", id)
errored = true
continue
}

if err := RemoveImage(imageClient, id); err != nil {
// 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
funcs = append(funcs, func() error {
status, err := ImageStatus(imageClient, id, false)
if err != nil {
return fmt.Errorf("image status request for %q failed: %w", id, err)
}
continue
}
if len(status.Image.RepoTags) == 0 {
// RepoTags is nil when pulling image by repoDigest,
// so print deleted using that instead.
for _, repoDigest := range status.Image.RepoDigests {
fmt.Printf("Deleted: %s\n", repoDigest)
if status.Image == nil {
return fmt.Errorf("no such image %s", id)
}
continue
}
for _, repoTag := range status.Image.RepoTags {
fmt.Printf("Deleted: %s\n", repoTag)
}
}

if errored {
return errors.New("unable to remove the image(s)")
if err := RemoveImage(imageClient, id); err != nil {
// We ignore further errors on prune because there might be
// races
if !prune {
return fmt.Errorf("error of removing image %q: %w", id, err)
}
return nil
}
if len(status.Image.RepoTags) == 0 {
// RepoTags is nil when pulling image by repoDigest,
// so print deleted using that instead.
for _, repoDigest := range status.Image.RepoDigests {
fmt.Printf("Deleted: %s\n", repoDigest)
}
return nil
}
for _, repoTag := range status.Image.RepoTags {
fmt.Printf("Deleted: %s\n", repoTag)
}
return nil
})
}

return nil
return errorUtils.AggregateGoroutines(funcs...)
},
}

Expand Down

0 comments on commit c0e4174

Please sign in to comment.