github.com/toplink-cn/moby@v0.0.0-20240305205811-460b4aebdf81/daemon/containerd/image_pull.go (about)

     1  package containerd
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io"
     7  	"os"
     8  	"strings"
     9  
    10  	"github.com/containerd/containerd"
    11  	cerrdefs "github.com/containerd/containerd/errdefs"
    12  	"github.com/containerd/containerd/images"
    13  	"github.com/containerd/containerd/pkg/snapshotters"
    14  	"github.com/containerd/containerd/platforms"
    15  	"github.com/containerd/containerd/remotes/docker"
    16  	"github.com/containerd/log"
    17  	"github.com/distribution/reference"
    18  	"github.com/docker/docker/api/types/events"
    19  	registrytypes "github.com/docker/docker/api/types/registry"
    20  	"github.com/docker/docker/distribution"
    21  	"github.com/docker/docker/errdefs"
    22  	"github.com/docker/docker/internal/compatcontext"
    23  	"github.com/docker/docker/pkg/progress"
    24  	"github.com/docker/docker/pkg/streamformatter"
    25  	"github.com/docker/docker/pkg/stringid"
    26  	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
    27  	"github.com/pkg/errors"
    28  )
    29  
    30  // PullImage initiates a pull operation. baseRef is the image to pull.
    31  // If reference is not tagged, all tags are pulled.
    32  func (i *ImageService) PullImage(ctx context.Context, baseRef reference.Named, platform *ocispec.Platform, metaHeaders map[string][]string, authConfig *registrytypes.AuthConfig, outStream io.Writer) error {
    33  	out := streamformatter.NewJSONProgressOutput(outStream, false)
    34  
    35  	if !reference.IsNameOnly(baseRef) {
    36  		return i.pullTag(ctx, baseRef, platform, metaHeaders, authConfig, out)
    37  	}
    38  
    39  	tags, err := distribution.Tags(ctx, baseRef, &distribution.Config{
    40  		RegistryService: i.registryService,
    41  		MetaHeaders:     metaHeaders,
    42  		AuthConfig:      authConfig,
    43  	})
    44  	if err != nil {
    45  		return err
    46  	}
    47  
    48  	for _, tag := range tags {
    49  		ref, err := reference.WithTag(baseRef, tag)
    50  		if err != nil {
    51  			log.G(ctx).WithFields(log.Fields{
    52  				"tag":     tag,
    53  				"baseRef": baseRef,
    54  			}).Warn("invalid tag, won't pull")
    55  			continue
    56  		}
    57  
    58  		if err := i.pullTag(ctx, ref, platform, metaHeaders, authConfig, out); err != nil {
    59  			return fmt.Errorf("error pulling %s: %w", ref, err)
    60  		}
    61  	}
    62  
    63  	return nil
    64  }
    65  
    66  func (i *ImageService) pullTag(ctx context.Context, ref reference.Named, platform *ocispec.Platform, metaHeaders map[string][]string, authConfig *registrytypes.AuthConfig, out progress.Output) error {
    67  	var opts []containerd.RemoteOpt
    68  	if platform != nil {
    69  		opts = append(opts, containerd.WithPlatform(platforms.Format(*platform)))
    70  	}
    71  
    72  	resolver, _ := i.newResolverFromAuthConfig(ctx, authConfig, ref)
    73  	opts = append(opts, containerd.WithResolver(resolver))
    74  
    75  	old, err := i.resolveDescriptor(ctx, ref.String())
    76  	if err != nil && !errdefs.IsNotFound(err) {
    77  		return err
    78  	}
    79  	p := platforms.Default()
    80  	if platform != nil {
    81  		p = platforms.Only(*platform)
    82  	}
    83  
    84  	jobs := newJobs()
    85  	h := images.HandlerFunc(func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
    86  		if images.IsLayerType(desc.MediaType) {
    87  			jobs.Add(desc)
    88  		}
    89  		return nil, nil
    90  	})
    91  	opts = append(opts, containerd.WithImageHandler(h))
    92  
    93  	pp := pullProgress{store: i.client.ContentStore(), showExists: true}
    94  	finishProgress := jobs.showProgress(ctx, out, pp)
    95  
    96  	var outNewImg *containerd.Image
    97  	defer func() {
    98  		finishProgress()
    99  
   100  		// Send final status message after the progress updater has finished.
   101  		// Otherwise the layer/manifest progress messages may arrive AFTER the
   102  		// status message have been sent, so they won't update the previous
   103  		// progress leaving stale progress like:
   104  		// 70f5ac315c5a: Downloading [>       ]       0B/3.19kB
   105  		// Digest: sha256:4f53e2564790c8e7856ec08e384732aa38dc43c52f02952483e3f003afbf23db
   106  		// 70f5ac315c5a: Download complete
   107  		// Status: Downloaded newer image for hello-world:latest
   108  		// docker.io/library/hello-world:latest
   109  		if outNewImg != nil {
   110  			img := *outNewImg
   111  			progress.Message(out, "", "Digest: "+img.Target().Digest.String())
   112  			writeStatus(out, reference.FamiliarString(ref), old.Digest != img.Target().Digest)
   113  		}
   114  	}()
   115  
   116  	var sentPullingFrom, sentSchema1Deprecation bool
   117  	ah := images.HandlerFunc(func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
   118  		if desc.MediaType == images.MediaTypeDockerSchema1Manifest && !sentSchema1Deprecation {
   119  			err := distribution.DeprecatedSchema1ImageError(ref)
   120  			if os.Getenv("DOCKER_ENABLE_DEPRECATED_PULL_SCHEMA_1_IMAGE") == "" {
   121  				log.G(context.TODO()).Warn(err.Error())
   122  				return nil, err
   123  			}
   124  			progress.Message(out, "", err.Error())
   125  			sentSchema1Deprecation = true
   126  		}
   127  		if images.IsLayerType(desc.MediaType) {
   128  			id := stringid.TruncateID(desc.Digest.String())
   129  			progress.Update(out, id, "Pulling fs layer")
   130  		}
   131  		if images.IsManifestType(desc.MediaType) {
   132  			if !sentPullingFrom {
   133  				var tagOrDigest string
   134  				if tagged, ok := ref.(reference.Tagged); ok {
   135  					tagOrDigest = tagged.Tag()
   136  				} else {
   137  					tagOrDigest = ref.String()
   138  				}
   139  				progress.Message(out, tagOrDigest, "Pulling from "+reference.Path(ref))
   140  				sentPullingFrom = true
   141  			}
   142  
   143  			available, _, _, missing, err := images.Check(ctx, i.client.ContentStore(), desc, p)
   144  			if err != nil {
   145  				return nil, err
   146  			}
   147  			// If we already have all the contents pull shouldn't show any layer
   148  			// download progress, not even a "Already present" message.
   149  			if available && len(missing) == 0 {
   150  				pp.hideLayers = true
   151  			}
   152  		}
   153  		return nil, nil
   154  	})
   155  	opts = append(opts, containerd.WithImageHandler(ah))
   156  
   157  	opts = append(opts, containerd.WithPullUnpack)
   158  	// TODO(thaJeztah): we may have to pass the snapshotter to use if the pull is part of a "docker run" (container create -> pull image if missing). See https://github.com/moby/moby/issues/45273
   159  	opts = append(opts, containerd.WithPullSnapshotter(i.snapshotter))
   160  
   161  	// AppendInfoHandlerWrapper will annotate the image with basic information like manifest and layer digests as labels;
   162  	// this information is used to enable remote snapshotters like nydus and stargz to query a registry.
   163  	infoHandler := snapshotters.AppendInfoHandlerWrapper(ref.String())
   164  	opts = append(opts, containerd.WithImageHandlerWrapper(infoHandler))
   165  
   166  	// Allow pulling application/vnd.docker.distribution.manifest.v1+prettyjws images
   167  	// by converting them to OCI manifests.
   168  	opts = append(opts, containerd.WithSchema1Conversion) //nolint:staticcheck // Ignore SA1019: containerd.WithSchema1Conversion is deprecated: use Schema 2 or OCI images.
   169  
   170  	img, err := i.client.Pull(ctx, ref.String(), opts...)
   171  	if err != nil {
   172  		if errors.Is(err, docker.ErrInvalidAuthorization) {
   173  			// Match error returned by containerd.
   174  			// https://github.com/containerd/containerd/blob/v1.7.8/remotes/docker/authorizer.go#L189-L191
   175  			if strings.Contains(err.Error(), "no basic auth credentials") {
   176  				return err
   177  			}
   178  			return errdefs.NotFound(fmt.Errorf("pull access denied for %s, repository does not exist or may require 'docker login'", reference.FamiliarName(ref)))
   179  		}
   180  		return err
   181  	}
   182  
   183  	logger := log.G(ctx).WithFields(log.Fields{
   184  		"digest": img.Target().Digest,
   185  		"remote": ref.String(),
   186  	})
   187  	logger.Info("image pulled")
   188  
   189  	// The pull succeeded, so try to remove any dangling image we have for this target
   190  	err = i.client.ImageService().Delete(compatcontext.WithoutCancel(ctx), danglingImageName(img.Target().Digest))
   191  	if err != nil && !cerrdefs.IsNotFound(err) {
   192  		// Image pull succeeded, but cleaning up the dangling image failed. Ignore the
   193  		// error to not mark the pull as failed.
   194  		logger.WithError(err).Warn("unexpected error while removing outdated dangling image reference")
   195  	}
   196  
   197  	i.LogImageEvent(reference.FamiliarString(ref), reference.FamiliarName(ref), events.ActionPull)
   198  	outNewImg = &img
   199  	return nil
   200  }
   201  
   202  // writeStatus writes a status message to out. If newerDownloaded is true, the
   203  // status message indicates that a newer image was downloaded. Otherwise, it
   204  // indicates that the image is up to date. requestedTag is the tag the message
   205  // will refer to.
   206  func writeStatus(out progress.Output, requestedTag string, newerDownloaded bool) {
   207  	if newerDownloaded {
   208  		progress.Message(out, "", "Status: Downloaded newer image for "+requestedTag)
   209  	} else {
   210  		progress.Message(out, "", "Status: Image is up to date for "+requestedTag)
   211  	}
   212  }