diff --git a/cmd/crictl/image.go b/cmd/crictl/image.go index 6dff03196b..24ab90ee68 100644 --- a/cmd/crictl/image.go +++ b/cmd/crictl/image.go @@ -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" ) @@ -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...) }, }