image_pull.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "context"
  4. "io"
  5. "net/url"
  6. "strings"
  7. "github.com/distribution/reference"
  8. "github.com/docker/docker/api/types"
  9. "github.com/docker/docker/errdefs"
  10. )
  11. // ImagePull requests the docker host to pull an image from a remote registry.
  12. // It executes the privileged function if the operation is unauthorized
  13. // and it tries one more time.
  14. // It's up to the caller to handle the io.ReadCloser and close it properly.
  15. //
  16. // FIXME(vdemeester): there is currently used in a few way in docker/docker
  17. // - if not in trusted content, ref is used to pass the whole reference, and tag is empty
  18. // - if in trusted content, ref is used to pass the reference name, and tag for the digest
  19. func (cli *Client) ImagePull(ctx context.Context, refStr string, options types.ImagePullOptions) (io.ReadCloser, error) {
  20. ref, err := reference.ParseNormalizedNamed(refStr)
  21. if err != nil {
  22. return nil, err
  23. }
  24. query := url.Values{}
  25. query.Set("fromImage", reference.FamiliarName(ref))
  26. if !options.All {
  27. query.Set("tag", getAPITagFromNamedRef(ref))
  28. }
  29. if options.Platform != "" {
  30. query.Set("platform", strings.ToLower(options.Platform))
  31. }
  32. resp, err := cli.tryImageCreate(ctx, query, options.RegistryAuth)
  33. if errdefs.IsUnauthorized(err) && options.PrivilegeFunc != nil {
  34. newAuthHeader, privilegeErr := options.PrivilegeFunc()
  35. if privilegeErr != nil {
  36. return nil, privilegeErr
  37. }
  38. resp, err = cli.tryImageCreate(ctx, query, newAuthHeader)
  39. }
  40. if err != nil {
  41. return nil, err
  42. }
  43. return resp.body, nil
  44. }
  45. // getAPITagFromNamedRef returns a tag from the specified reference.
  46. // This function is necessary as long as the docker "server" api expects
  47. // digests to be sent as tags and makes a distinction between the name
  48. // and tag/digest part of a reference.
  49. func getAPITagFromNamedRef(ref reference.Named) string {
  50. if digested, ok := ref.(reference.Digested); ok {
  51. return digested.Digest().String()
  52. }
  53. ref = reference.TagNameOnly(ref)
  54. if tagged, ok := ref.(reference.Tagged); ok {
  55. return tagged.Tag()
  56. }
  57. return ""
  58. }