github.com/containerd/nerdctl/v2@v2.0.0-beta.5.0.20240520001846-b5758f54fa28/pkg/cmd/image/prune.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package image
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	"github.com/containerd/containerd"
    24  	"github.com/containerd/containerd/images"
    25  	"github.com/containerd/log"
    26  	"github.com/containerd/nerdctl/v2/pkg/api/types"
    27  	"github.com/containerd/nerdctl/v2/pkg/imgutil"
    28  	"github.com/containerd/platforms"
    29  	"github.com/opencontainers/go-digest"
    30  )
    31  
    32  // Prune will remove all dangling images. If all is specified, will also remove all images not referenced by any container.
    33  func Prune(ctx context.Context, client *containerd.Client, options types.ImagePruneOptions) error {
    34  	var (
    35  		imageStore     = client.ImageService()
    36  		contentStore   = client.ContentStore()
    37  		containerStore = client.ContainerService()
    38  	)
    39  
    40  	imageList, err := imageStore.List(ctx)
    41  	if err != nil {
    42  		return err
    43  	}
    44  
    45  	var filteredImages []images.Image
    46  
    47  	if options.All {
    48  		containerList, err := containerStore.List(ctx)
    49  		if err != nil {
    50  			return err
    51  		}
    52  		usedImages := make(map[string]struct{})
    53  		for _, container := range containerList {
    54  			usedImages[container.Image] = struct{}{}
    55  		}
    56  
    57  		for _, image := range imageList {
    58  			if _, ok := usedImages[image.Name]; ok {
    59  				continue
    60  			}
    61  
    62  			filteredImages = append(filteredImages, image)
    63  		}
    64  	} else {
    65  		filteredImages = imgutil.FilterDangling(imageList, true)
    66  	}
    67  
    68  	delOpts := []images.DeleteOpt{images.SynchronousDelete()}
    69  	removedImages := make(map[string][]digest.Digest)
    70  	for _, image := range filteredImages {
    71  		digests, err := image.RootFS(ctx, contentStore, platforms.DefaultStrict())
    72  		if err != nil {
    73  			log.G(ctx).WithError(err).Warnf("failed to enumerate rootfs")
    74  		}
    75  		if err := imageStore.Delete(ctx, image.Name, delOpts...); err != nil {
    76  			log.G(ctx).WithError(err).Warnf("failed to delete image %s", image.Name)
    77  			continue
    78  		}
    79  		removedImages[image.Name] = digests
    80  	}
    81  
    82  	if len(removedImages) > 0 {
    83  		fmt.Fprintln(options.Stdout, "Deleted Images:")
    84  		for image, digests := range removedImages {
    85  			fmt.Fprintf(options.Stdout, "Untagged: %s\n", image)
    86  			for _, digest := range digests {
    87  				fmt.Fprintf(options.Stdout, "deleted: %s\n", digest)
    88  			}
    89  		}
    90  		fmt.Fprintln(options.Stdout, "")
    91  	}
    92  	return nil
    93  }