image_pull.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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/docker/distribution/reference"
  12. "github.com/docker/docker/api/types/registry"
  13. "github.com/docker/docker/errdefs"
  14. "github.com/docker/docker/pkg/streamformatter"
  15. "github.com/opencontainers/go-digest"
  16. ocispec "github.com/opencontainers/image-spec/specs-go/v1"
  17. "github.com/sirupsen/logrus"
  18. )
  19. // PullImage initiates a pull operation. image is the repository name to pull, and
  20. // tagOrDigest may be either empty, or indicate a specific tag or digest to pull.
  21. func (i *ImageService) PullImage(ctx context.Context, image, tagOrDigest string, platform *ocispec.Platform, metaHeaders map[string][]string, authConfig *registry.AuthConfig, outStream io.Writer) error {
  22. var opts []containerd.RemoteOpt
  23. if platform != nil {
  24. opts = append(opts, containerd.WithPlatform(platforms.Format(*platform)))
  25. }
  26. ref, err := reference.ParseNormalizedNamed(image)
  27. if err != nil {
  28. return errdefs.InvalidParameter(err)
  29. }
  30. // TODO(thaJeztah) this could use a WithTagOrDigest() utility
  31. if tagOrDigest != "" {
  32. // The "tag" could actually be a digest.
  33. var dgst digest.Digest
  34. dgst, err = digest.Parse(tagOrDigest)
  35. if err == nil {
  36. ref, err = reference.WithDigest(reference.TrimNamed(ref), dgst)
  37. } else {
  38. ref, err = reference.WithTag(ref, tagOrDigest)
  39. }
  40. if err != nil {
  41. return errdefs.InvalidParameter(err)
  42. }
  43. }
  44. resolver, _ := i.newResolverFromAuthConfig(ctx, authConfig)
  45. opts = append(opts, containerd.WithResolver(resolver))
  46. jobs := newJobs()
  47. h := images.HandlerFunc(func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
  48. if desc.MediaType != images.MediaTypeDockerSchema1Manifest {
  49. jobs.Add(desc)
  50. }
  51. return nil, nil
  52. })
  53. opts = append(opts, containerd.WithImageHandler(h))
  54. out := streamformatter.NewJSONProgressOutput(outStream, false)
  55. finishProgress := jobs.showProgress(ctx, out, pullProgress{Store: i.client.ContentStore(), ShowExists: true})
  56. defer finishProgress()
  57. opts = append(opts, containerd.WithPullUnpack)
  58. // 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
  59. opts = append(opts, containerd.WithPullSnapshotter(i.snapshotter))
  60. // AppendInfoHandlerWrapper will annotate the image with basic information like manifest and layer digests as labels;
  61. // this information is used to enable remote snapshotters like nydus and stargz to query a registry.
  62. infoHandler := snapshotters.AppendInfoHandlerWrapper(ref.String())
  63. opts = append(opts, containerd.WithImageHandlerWrapper(infoHandler))
  64. img, err := i.client.Pull(ctx, ref.String(), opts...)
  65. if err != nil {
  66. return err
  67. }
  68. logger := log.G(ctx).WithFields(logrus.Fields{
  69. "digest": img.Target().Digest,
  70. "remote": ref.String(),
  71. })
  72. logger.Info("image pulled")
  73. // The pull succeeded, so try to remove any dangling image we have for this target
  74. err = i.client.ImageService().Delete(context.Background(), danglingImageName(img.Target().Digest))
  75. if err != nil && !cerrdefs.IsNotFound(err) {
  76. // Image pull succeeded, but cleaning up the dangling image failed. Ignore the
  77. // error to not mark the pull as failed.
  78. logger.WithError(err).Warn("unexpected error while removing outdated dangling image reference")
  79. }
  80. return nil
  81. }