Skip to content

Commit

Permalink
make prune to remove dangling images only
Browse files Browse the repository at this point in the history
Signed-off-by: suyanhanx <[email protected]>
  • Loading branch information
suyanhanx committed Jan 27, 2023
1 parent d39e9c8 commit dcde698
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 22 deletions.
61 changes: 40 additions & 21 deletions cmd/nerdctl/image_prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main
import (
"context"
"fmt"
"github.com/containerd/nerdctl/pkg/imgutil"
"strings"

"github.com/containerd/containerd"
Expand Down Expand Up @@ -55,20 +56,21 @@ func imagePruneAction(cmd *cobra.Command, _ []string) error {
return err
}

if !all {
logrus.Warn("Currently, `nerdctl image prune` requires --all to be specified. Skip pruning.")
// NOP
return nil
}

force, err := cmd.Flags().GetBool("force")
if err != nil {
return err
}

if !force {
var confirm string
msg := "This will remove all images without at least one container associated to them."
var (
confirm string
msg string
)
if !all {
msg += "This will remove all dangling images."
} else {
msg += "This will remove all images without at least one container associated to them."
}
msg += "\nAre you sure you want to continue? [y/N] "

fmt.Fprintf(cmd.OutOrStdout(), "WARNING! %s", msg)
Expand All @@ -84,10 +86,10 @@ func imagePruneAction(cmd *cobra.Command, _ []string) error {
}
defer cancel()

return imagePrune(ctx, cmd, client)
return imagePrune(ctx, cmd, client, all)
}

func imagePrune(ctx context.Context, cmd *cobra.Command, client *containerd.Client) error {
func imagePrune(ctx context.Context, cmd *cobra.Command, client *containerd.Client, all bool) error {
var (
imageStore = client.ImageService()
contentStore = client.ContentStore()
Expand All @@ -97,22 +99,39 @@ func imagePrune(ctx context.Context, cmd *cobra.Command, client *containerd.Clie
if err != nil {
return err
}
containerList, err := containerStore.List(ctx)
if err != nil {
return err
}
usedImages := make(map[string]struct{})
for _, container := range containerList {
usedImages[container.Image] = struct{}{}
}

delOpts := []images.DeleteOpt{images.SynchronousDelete()}
removedImages := make(map[string][]digest.Digest)
for _, image := range imageList {
if _, ok := usedImages[image.Name]; ok {
continue
var filteredImages []images.Image

if all {
containerList, err := containerStore.List(ctx)
if err != nil {
return err
}
usedImages := make(map[string]struct{})
for _, container := range containerList {
usedImages[container.Image] = struct{}{}
}

for _, image := range imageList {
if _, ok := usedImages[image.Name]; ok {
continue
}

filteredImages = append(filteredImages, image)
}
} else {
for _, image := range imageList {
_, tag := imgutil.ParseRepoTag(image.Name)

if tag == "" {
filteredImages = append(filteredImages, image)
}
}
}

for _, image := range filteredImages {
digests, err := image.RootFS(ctx, contentStore, platforms.DefaultStrict())
if err != nil {
logrus.WithError(err).Warnf("failed to enumerate rootfs")
Expand Down
2 changes: 1 addition & 1 deletion cmd/nerdctl/system_prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func systemPruneAction(cmd *cobra.Command, args []string) error {
return err
}
}
if err := imagePrune(ctx, cmd, client); err != nil {
if err := imagePrune(ctx, cmd, client, true); err != nil {
return nil
}
prunedObjects, err := buildCachePrune(ctx, cmd, all, globalOptions.Namespace)
Expand Down

0 comments on commit dcde698

Please sign in to comment.