image_pull.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package containerd
  2. import (
  3. "context"
  4. "io"
  5. "github.com/containerd/containerd"
  6. cerrdefs "github.com/containerd/containerd/errdefs"
  7. "github.com/containerd/containerd/images"
  8. "github.com/containerd/containerd/log"
  9. "github.com/containerd/containerd/pkg/snapshotters"
  10. "github.com/containerd/containerd/platforms"
  11. "github.com/distribution/reference"
  12. "github.com/docker/docker/api/types/events"
  13. "github.com/docker/docker/api/types/registry"
  14. "github.com/docker/docker/distribution"
  15. "github.com/docker/docker/errdefs"
  16. "github.com/docker/docker/pkg/progress"
  17. "github.com/docker/docker/pkg/streamformatter"
  18. "github.com/opencontainers/go-digest"
  19. ocispec "github.com/opencontainers/image-spec/specs-go/v1"
  20. )
  21. // PullImage initiates a pull operation. image is the repository name to pull, and
  22. // tagOrDigest may be either empty, or indicate a specific tag or digest to pull.
  23. func (i *ImageService) PullImage(ctx context.Context, image, tagOrDigest string, platform *ocispec.Platform, metaHeaders map[string][]string, authConfig *registry.AuthConfig, outStream io.Writer) error {
  24. var opts []containerd.RemoteOpt
  25. if platform != nil {
  26. opts = append(opts, containerd.WithPlatform(platforms.Format(*platform)))
  27. }
  28. ref, err := reference.ParseNormalizedNamed(image)
  29. if err != nil {
  30. return errdefs.InvalidParameter(err)
  31. }
  32. // TODO(thaJeztah) this could use a WithTagOrDigest() utility
  33. if tagOrDigest != "" {
  34. // The "tag" could actually be a digest.
  35. var dgst digest.Digest
  36. dgst, err = digest.Parse(tagOrDigest)
  37. if err == nil {
  38. ref, err = reference.WithDigest(reference.TrimNamed(ref), dgst)
  39. } else {
  40. ref, err = reference.WithTag(ref, tagOrDigest)
  41. }
  42. if err != nil {
  43. return errdefs.InvalidParameter(err)
  44. }
  45. }
  46. resolver, _ := i.newResolverFromAuthConfig(ctx, authConfig)
  47. opts = append(opts, containerd.WithResolver(resolver))
  48. old, err := i.resolveDescriptor(ctx, ref.String())
  49. if err != nil && !errdefs.IsNotFound(err) {
  50. return err
  51. }
  52. p := platforms.Default()
  53. if platform != nil {
  54. p = platforms.Only(*platform)
  55. }
  56. jobs := newJobs()
  57. h := images.HandlerFunc(func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
  58. if images.IsLayerType(desc.MediaType) {
  59. jobs.Add(desc)
  60. }
  61. return nil, nil
  62. })
  63. opts = append(opts, containerd.WithImageHandler(h))
  64. out := streamformatter.NewJSONProgressOutput(outStream, false)
  65. pp := pullProgress{store: i.client.ContentStore(), showExists: true}
  66. finishProgress := jobs.showProgress(ctx, out, pp)
  67. defer finishProgress()
  68. var sentPullingFrom, sentSchema1Deprecation bool
  69. ah := images.HandlerFunc(func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
  70. if desc.MediaType == images.MediaTypeDockerSchema1Manifest && !sentSchema1Deprecation {
  71. progress.Message(out, "", distribution.DeprecatedSchema1ImageMessage(ref))
  72. sentSchema1Deprecation = true
  73. }
  74. if images.IsManifestType(desc.MediaType) {
  75. if !sentPullingFrom {
  76. progress.Message(out, tagOrDigest, "Pulling from "+reference.Path(ref))
  77. sentPullingFrom = true
  78. }
  79. available, _, _, missing, err := images.Check(ctx, i.client.ContentStore(), desc, p)
  80. if err != nil {
  81. return nil, err
  82. }
  83. // If we already have all the contents pull shouldn't show any layer
  84. // download progress, not even a "Already present" message.
  85. if available && len(missing) == 0 {
  86. pp.hideLayers = true
  87. }
  88. }
  89. return nil, nil
  90. })
  91. opts = append(opts, containerd.WithImageHandler(ah))
  92. opts = append(opts, containerd.WithPullUnpack)
  93. // 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
  94. opts = append(opts, containerd.WithPullSnapshotter(i.snapshotter))
  95. // AppendInfoHandlerWrapper will annotate the image with basic information like manifest and layer digests as labels;
  96. // this information is used to enable remote snapshotters like nydus and stargz to query a registry.
  97. infoHandler := snapshotters.AppendInfoHandlerWrapper(ref.String())
  98. opts = append(opts, containerd.WithImageHandlerWrapper(infoHandler))
  99. // Allow pulling application/vnd.docker.distribution.manifest.v1+prettyjws images
  100. // by converting them to OCI manifests.
  101. opts = append(opts, containerd.WithSchema1Conversion)
  102. img, err := i.client.Pull(ctx, ref.String(), opts...)
  103. if err != nil {
  104. return err
  105. }
  106. logger := log.G(ctx).WithFields(log.Fields{
  107. "digest": img.Target().Digest,
  108. "remote": ref.String(),
  109. })
  110. logger.Info("image pulled")
  111. progress.Message(out, "", "Digest: "+img.Target().Digest.String())
  112. writeStatus(out, reference.FamiliarString(ref), old.Digest != img.Target().Digest)
  113. // The pull succeeded, so try to remove any dangling image we have for this target
  114. err = i.client.ImageService().Delete(context.Background(), danglingImageName(img.Target().Digest))
  115. if err != nil && !cerrdefs.IsNotFound(err) {
  116. // Image pull succeeded, but cleaning up the dangling image failed. Ignore the
  117. // error to not mark the pull as failed.
  118. logger.WithError(err).Warn("unexpected error while removing outdated dangling image reference")
  119. }
  120. i.LogImageEvent(reference.FamiliarString(ref), reference.FamiliarName(ref), events.ActionPull)
  121. return nil
  122. }
  123. // writeStatus writes a status message to out. If newerDownloaded is true, the
  124. // status message indicates that a newer image was downloaded. Otherwise, it
  125. // indicates that the image is up to date. requestedTag is the tag the message
  126. // will refer to.
  127. func writeStatus(out progress.Output, requestedTag string, newerDownloaded bool) {
  128. if newerDownloaded {
  129. progress.Message(out, "", "Status: Downloaded newer image for "+requestedTag)
  130. } else {
  131. progress.Message(out, "", "Status: Image is up to date for "+requestedTag)
  132. }
  133. }