pull.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package distribution // import "github.com/docker/docker/distribution"
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/containerd/containerd/log"
  6. "github.com/docker/distribution/reference"
  7. "github.com/docker/docker/api"
  8. refstore "github.com/docker/docker/reference"
  9. "github.com/opencontainers/go-digest"
  10. "github.com/pkg/errors"
  11. )
  12. // Pull initiates a pull operation. image is the repository name to pull, and
  13. // tag may be either empty, or indicate a specific tag to pull.
  14. func Pull(ctx context.Context, ref reference.Named, config *ImagePullConfig, local ContentStore) error {
  15. // Resolve the Repository name from fqn to RepositoryInfo
  16. repoInfo, err := config.RegistryService.ResolveRepository(ref)
  17. if err != nil {
  18. return err
  19. }
  20. // makes sure name is not `scratch`
  21. if err := validateRepoName(repoInfo.Name); err != nil {
  22. return err
  23. }
  24. endpoints, err := config.RegistryService.LookupPullEndpoints(reference.Domain(repoInfo.Name))
  25. if err != nil {
  26. return err
  27. }
  28. var (
  29. lastErr error
  30. // confirmedTLSRegistries is a map indicating which registries
  31. // are known to be using TLS. There should never be a plaintext
  32. // retry for any of these.
  33. confirmedTLSRegistries = make(map[string]struct{})
  34. )
  35. for _, endpoint := range endpoints {
  36. if endpoint.URL.Scheme != "https" {
  37. if _, confirmedTLS := confirmedTLSRegistries[endpoint.URL.Host]; confirmedTLS {
  38. log.G(ctx).Debugf("Skipping non-TLS endpoint %s for host/port that appears to use TLS", endpoint.URL)
  39. continue
  40. }
  41. }
  42. log.G(ctx).Debugf("Trying to pull %s from %s", reference.FamiliarName(repoInfo.Name), endpoint.URL)
  43. if err := newPuller(endpoint, repoInfo, config, local).pull(ctx, ref); err != nil {
  44. // Was this pull cancelled? If so, don't try to fall
  45. // back.
  46. fallback := false
  47. select {
  48. case <-ctx.Done():
  49. default:
  50. if fallbackErr, ok := err.(fallbackError); ok {
  51. fallback = true
  52. if fallbackErr.transportOK && endpoint.URL.Scheme == "https" {
  53. confirmedTLSRegistries[endpoint.URL.Host] = struct{}{}
  54. }
  55. err = fallbackErr.err
  56. }
  57. }
  58. if fallback {
  59. lastErr = err
  60. log.G(ctx).Infof("Attempting next endpoint for pull after error: %v", err)
  61. continue
  62. }
  63. log.G(ctx).Errorf("Not continuing with pull after error: %v", err)
  64. return translatePullError(err, ref)
  65. }
  66. config.ImageEventLogger(reference.FamiliarString(ref), reference.FamiliarName(repoInfo.Name), "pull")
  67. return nil
  68. }
  69. if lastErr == nil {
  70. lastErr = fmt.Errorf("no endpoints found for %s", reference.FamiliarString(ref))
  71. }
  72. return translatePullError(lastErr, ref)
  73. }
  74. // validateRepoName validates the name of a repository.
  75. func validateRepoName(name reference.Named) error {
  76. if reference.FamiliarName(name) == api.NoBaseImageSpecifier {
  77. return errors.WithStack(reservedNameError(api.NoBaseImageSpecifier))
  78. }
  79. return nil
  80. }
  81. func addDigestReference(store refstore.Store, ref reference.Named, dgst digest.Digest, id digest.Digest) error {
  82. dgstRef, err := reference.WithDigest(reference.TrimNamed(ref), dgst)
  83. if err != nil {
  84. return err
  85. }
  86. if oldTagID, err := store.Get(dgstRef); err == nil {
  87. if oldTagID != id {
  88. // Updating digests not supported by reference store
  89. log.G(context.TODO()).Errorf("Image ID for digest %s changed from %s to %s, cannot update", dgst.String(), oldTagID, id)
  90. }
  91. return nil
  92. } else if err != refstore.ErrDoesNotExist {
  93. return err
  94. }
  95. return store.AddDigest(dgstRef, id, true)
  96. }