Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow parallel image removal #1639

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading