github.com/containerd/nerdctl/v2@v2.0.0-beta.5.0.20240520001846-b5758f54fa28/pkg/idutil/imagewalker/imagewalker.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 imagewalker
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"regexp"
    23  	"strings"
    24  
    25  	"github.com/containerd/containerd"
    26  	"github.com/containerd/containerd/images"
    27  	"github.com/containerd/nerdctl/v2/pkg/referenceutil"
    28  	"github.com/opencontainers/go-digest"
    29  )
    30  
    31  type Found struct {
    32  	Image        images.Image
    33  	Req          string // The raw request string. name, short ID, or long ID.
    34  	MatchIndex   int    // Begins with 0, up to MatchCount - 1.
    35  	MatchCount   int    // 1 on exact match. > 1 on ambiguous match. Never be <= 0.
    36  	UniqueImages int    // Number of unique images in all found images.
    37  }
    38  
    39  type OnFound func(ctx context.Context, found Found) error
    40  
    41  type ImageWalker struct {
    42  	Client  *containerd.Client
    43  	OnFound OnFound
    44  }
    45  
    46  // Walk walks images and calls w.OnFound .
    47  // Req is name, short ID, or long ID.
    48  // Returns the number of the found entries.
    49  func (w *ImageWalker) Walk(ctx context.Context, req string) (int, error) {
    50  	var filters []string
    51  	if canonicalRef, err := referenceutil.ParseAny(req); err == nil {
    52  		filters = append(filters, fmt.Sprintf("name==%s", canonicalRef.String()))
    53  	}
    54  	filters = append(filters,
    55  		fmt.Sprintf("name==%s", req),
    56  		fmt.Sprintf("target.digest~=^sha256:%s.*$", regexp.QuoteMeta(req)),
    57  		fmt.Sprintf("target.digest~=^%s.*$", regexp.QuoteMeta(req)),
    58  	)
    59  
    60  	images, err := w.Client.ImageService().List(ctx, filters...)
    61  	if err != nil {
    62  		return -1, err
    63  	}
    64  
    65  	matchCount := len(images)
    66  	// to handle the `rmi -f` case where returned images are different but
    67  	// have the same short prefix.
    68  	uniqueImages := make(map[digest.Digest]bool)
    69  	for _, image := range images {
    70  		uniqueImages[image.Target.Digest] = true
    71  	}
    72  
    73  	for i, img := range images {
    74  		f := Found{
    75  			Image:        img,
    76  			Req:          req,
    77  			MatchIndex:   i,
    78  			MatchCount:   matchCount,
    79  			UniqueImages: len(uniqueImages),
    80  		}
    81  		if e := w.OnFound(ctx, f); e != nil {
    82  			return -1, e
    83  		}
    84  	}
    85  	return matchCount, nil
    86  }
    87  
    88  // WalkAll calls `Walk` for each req in `reqs`.
    89  //
    90  // It can be used when the matchCount is not important (e.g., only care if there
    91  // is any error or if matchCount == 0 (not found error) when walking all reqs).
    92  // If `forceAll`, it calls `Walk` on every req
    93  // and return all errors joined by `\n`. If not `forceAll`, it returns the first error
    94  // encountered while calling `Walk`.
    95  func (w *ImageWalker) WalkAll(ctx context.Context, reqs []string, forceAll bool) error {
    96  	var errs []string
    97  	for _, req := range reqs {
    98  		n, err := w.Walk(ctx, req)
    99  		if err == nil && n == 0 {
   100  			err = fmt.Errorf("no such image: %s", req)
   101  		}
   102  		if err != nil {
   103  			if !forceAll {
   104  				return err
   105  			}
   106  			errs = append(errs, err.Error())
   107  		}
   108  	}
   109  	if len(errs) > 0 {
   110  		return fmt.Errorf("%d errors:\n%s", len(errs), strings.Join(errs, "\n"))
   111  	}
   112  	return nil
   113  }