image_pull.go 3.5 KB

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