image_pull.go 1.9 KB

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