image_pull.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package client
  2. import (
  3. "io"
  4. "net/http"
  5. "net/url"
  6. "golang.org/x/net/context"
  7. "github.com/docker/docker/api/types"
  8. "github.com/docker/docker/api/types/reference"
  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, ref string, options types.ImagePullOptions) (io.ReadCloser, error) {
  19. repository, tag, err := reference.Parse(ref)
  20. if err != nil {
  21. return nil, err
  22. }
  23. query := url.Values{}
  24. query.Set("fromImage", repository)
  25. if tag != "" && !options.All {
  26. query.Set("tag", tag)
  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. }