diff --git a/pkg/skaffold/build/local/types.go b/pkg/skaffold/build/local/types.go index 31ea4ab8e78..c20ebc0f7f9 100644 --- a/pkg/skaffold/build/local/types.go +++ b/pkg/skaffold/build/local/types.go @@ -38,6 +38,7 @@ type Builder struct { localCluster bool pushImages bool prune bool + pruneChildren bool skipTests bool kubeContext string builtImages []string @@ -81,6 +82,7 @@ func NewBuilder(runCtx *runcontext.RunContext) (*Builder, error) { pushImages: pushImages, skipTests: runCtx.Opts.SkipTests, prune: runCtx.Opts.Prune(), + pruneChildren: !runCtx.Opts.NoPruneChildren, insecureRegistries: runCtx.InsecureRegistries, }, nil } @@ -101,5 +103,5 @@ func (b *Builder) Labels() map[string]string { // Prune uses the docker API client to remove all images built with Skaffold func (b *Builder) Prune(ctx context.Context, out io.Writer) error { - return docker.Prune(ctx, out, b.builtImages, b.localDocker) + return b.localDocker.Prune(ctx, out, b.builtImages, b.pruneChildren) } diff --git a/pkg/skaffold/docker/image.go b/pkg/skaffold/docker/image.go index ed16310fa22..1eb2cda6fd7 100644 --- a/pkg/skaffold/docker/image.go +++ b/pkg/skaffold/docker/image.go @@ -55,6 +55,7 @@ type LocalDaemon interface { RepoDigest(ctx context.Context, ref string) (string, error) ImageList(ctx context.Context, options types.ImageListOptions) ([]types.ImageSummary, error) ImageExists(ctx context.Context, ref string) bool + Prune(ctx context.Context, out io.Writer, images []string, pruneChildren bool) error } type localDaemon struct { @@ -410,3 +411,25 @@ func EvaluateBuildArgs(args map[string]*string) (map[string]*string, error) { return evaluated, nil } + +func (l *localDaemon) Prune(ctx context.Context, out io.Writer, images []string, pruneChildren bool) error { + for _, id := range images { + resp, err := l.ImageRemove(ctx, id, types.ImageRemoveOptions{ + Force: true, + PruneChildren: pruneChildren, + }) + if err != nil { + return errors.Wrap(err, "pruning images") + } + for _, r := range resp { + if r.Deleted != "" { + fmt.Fprintf(out, "deleted image %s\n", r.Deleted) + } + if r.Untagged != "" { + fmt.Fprintf(out, "untagged image %s\n", r.Untagged) + } + } + } + + return nil +} diff --git a/pkg/skaffold/docker/image_util.go b/pkg/skaffold/docker/image_util.go index 33d299fc947..ce553b20e03 100644 --- a/pkg/skaffold/docker/image_util.go +++ b/pkg/skaffold/docker/image_util.go @@ -18,48 +18,13 @@ package docker import ( "context" - "fmt" - "io" "strings" - "github.com/GoogleContainerTools/skaffold/pkg/skaffold/config" - "github.com/docker/docker/api/types" v1 "github.com/google/go-containerregistry/pkg/v1" "github.com/pkg/errors" "github.com/sirupsen/logrus" ) -var ( - opts = &config.SkaffoldOptions{} -) - -func Prune(ctx context.Context, out io.Writer, images []string, client LocalDaemon) error { - pruneChildren := true - - if opts.NoPruneChildren { - pruneChildren = false - } - - for _, id := range images { - resp, err := client.ImageRemove(ctx, id, types.ImageRemoveOptions{ - Force: true, - PruneChildren: pruneChildren, - }) - if err != nil { - return errors.Wrap(err, "pruning images") - } - for _, r := range resp { - if r.Deleted != "" { - fmt.Fprintf(out, "deleted image %s\n", r.Deleted) - } - if r.Untagged != "" { - fmt.Fprintf(out, "untagged image %s\n", r.Untagged) - } - } - } - return nil -} - func RetrieveWorkingDir(tagged string, insecureRegistries map[string]bool) (string, error) { var cf *v1.ConfigFile var err error