image_pull.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package containerd
  2. import (
  3. "context"
  4. "io"
  5. "github.com/containerd/containerd"
  6. "github.com/containerd/containerd/images"
  7. "github.com/containerd/containerd/pkg/snapshotters"
  8. "github.com/containerd/containerd/platforms"
  9. "github.com/docker/distribution/reference"
  10. "github.com/docker/docker/api/types/registry"
  11. "github.com/docker/docker/errdefs"
  12. "github.com/docker/docker/pkg/streamformatter"
  13. "github.com/opencontainers/go-digest"
  14. specs "github.com/opencontainers/image-spec/specs-go/v1"
  15. )
  16. // PullImage initiates a pull operation. image is the repository name to pull, and
  17. // tagOrDigest may be either empty, or indicate a specific tag or digest to pull.
  18. func (i *ImageService) PullImage(ctx context.Context, image, tagOrDigest string, platform *specs.Platform, metaHeaders map[string][]string, authConfig *registry.AuthConfig, outStream io.Writer) error {
  19. var opts []containerd.RemoteOpt
  20. if platform != nil {
  21. opts = append(opts, containerd.WithPlatform(platforms.Format(*platform)))
  22. }
  23. ref, err := reference.ParseNormalizedNamed(image)
  24. if err != nil {
  25. return errdefs.InvalidParameter(err)
  26. }
  27. // TODO(thaJeztah) this could use a WithTagOrDigest() utility
  28. if tagOrDigest != "" {
  29. // The "tag" could actually be a digest.
  30. var dgst digest.Digest
  31. dgst, err = digest.Parse(tagOrDigest)
  32. if err == nil {
  33. ref, err = reference.WithDigest(reference.TrimNamed(ref), dgst)
  34. } else {
  35. ref, err = reference.WithTag(ref, tagOrDigest)
  36. }
  37. if err != nil {
  38. return errdefs.InvalidParameter(err)
  39. }
  40. }
  41. resolver, _ := i.newResolverFromAuthConfig(authConfig)
  42. opts = append(opts, containerd.WithResolver(resolver))
  43. jobs := newJobs()
  44. h := images.HandlerFunc(func(ctx context.Context, desc specs.Descriptor) ([]specs.Descriptor, error) {
  45. if desc.MediaType != images.MediaTypeDockerSchema1Manifest {
  46. jobs.Add(desc)
  47. }
  48. return nil, nil
  49. })
  50. opts = append(opts, containerd.WithImageHandler(h))
  51. out := streamformatter.NewJSONProgressOutput(outStream, false)
  52. finishProgress := jobs.showProgress(ctx, out, pullProgress{Store: i.client.ContentStore(), ShowExists: true})
  53. defer finishProgress()
  54. opts = append(opts, containerd.WithPullUnpack)
  55. // 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
  56. opts = append(opts, containerd.WithPullSnapshotter(i.snapshotter))
  57. // AppendInfoHandlerWrapper will annotate the image with basic information like manifest and layer digests as labels;
  58. // this information is used to enable remote snapshotters like nydus and stargz to query a registry.
  59. infoHandler := snapshotters.AppendInfoHandlerWrapper(ref.String())
  60. opts = append(opts, containerd.WithImageHandlerWrapper(infoHandler))
  61. _, err = i.client.Pull(ctx, ref.String(), opts...)
  62. return err
  63. }