pull.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package distribution // import "github.com/docker/docker/distribution"
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/docker/distribution/reference"
  6. "github.com/docker/docker/api"
  7. "github.com/docker/docker/distribution/metadata"
  8. "github.com/docker/docker/pkg/progress"
  9. refstore "github.com/docker/docker/reference"
  10. "github.com/docker/docker/registry"
  11. "github.com/opencontainers/go-digest"
  12. specs "github.com/opencontainers/image-spec/specs-go/v1"
  13. "github.com/pkg/errors"
  14. "github.com/sirupsen/logrus"
  15. )
  16. // Puller is an interface that abstracts pulling for different API versions.
  17. type Puller interface {
  18. // Pull tries to pull the image referenced by `tag`
  19. // Pull returns an error if any, as well as a boolean that determines whether to retry Pull on the next configured endpoint.
  20. //
  21. Pull(ctx context.Context, ref reference.Named, platform *specs.Platform) error
  22. }
  23. // newPuller returns a Puller interface that will pull from either a v1 or v2
  24. // registry. The endpoint argument contains a Version field that determines
  25. // whether a v1 or v2 puller will be created. The other parameters are passed
  26. // through to the underlying puller implementation for use during the actual
  27. // pull operation.
  28. func newPuller(endpoint registry.APIEndpoint, repoInfo *registry.RepositoryInfo, imagePullConfig *ImagePullConfig) (Puller, error) {
  29. switch endpoint.Version {
  30. case registry.APIVersion2:
  31. return &v2Puller{
  32. V2MetadataService: metadata.NewV2MetadataService(imagePullConfig.MetadataStore),
  33. endpoint: endpoint,
  34. config: imagePullConfig,
  35. repoInfo: repoInfo,
  36. }, nil
  37. case registry.APIVersion1:
  38. return nil, fmt.Errorf("protocol version %d no longer supported. Please contact admins of registry %s", endpoint.Version, endpoint.URL)
  39. }
  40. return nil, fmt.Errorf("unknown version %d for registry %s", endpoint.Version, endpoint.URL)
  41. }
  42. // Pull initiates a pull operation. image is the repository name to pull, and
  43. // tag may be either empty, or indicate a specific tag to pull.
  44. func Pull(ctx context.Context, ref reference.Named, imagePullConfig *ImagePullConfig) error {
  45. // Resolve the Repository name from fqn to RepositoryInfo
  46. repoInfo, err := imagePullConfig.RegistryService.ResolveRepository(ref)
  47. if err != nil {
  48. return err
  49. }
  50. // makes sure name is not `scratch`
  51. if err := ValidateRepoName(repoInfo.Name); err != nil {
  52. return err
  53. }
  54. endpoints, err := imagePullConfig.RegistryService.LookupPullEndpoints(reference.Domain(repoInfo.Name))
  55. if err != nil {
  56. return err
  57. }
  58. var (
  59. lastErr error
  60. // discardNoSupportErrors is used to track whether an endpoint encountered an error of type registry.ErrNoSupport
  61. // By default it is false, which means that if an ErrNoSupport error is encountered, it will be saved in lastErr.
  62. // As soon as another kind of error is encountered, discardNoSupportErrors is set to true, avoiding the saving of
  63. // any subsequent ErrNoSupport errors in lastErr.
  64. // It's needed for pull-by-digest on v1 endpoints: if there are only v1 endpoints configured, the error should be
  65. // returned and displayed, but if there was a v2 endpoint which supports pull-by-digest, then the last relevant
  66. // error is the ones from v2 endpoints not v1.
  67. discardNoSupportErrors bool
  68. // confirmedV2 is set to true if a pull attempt managed to
  69. // confirm that it was talking to a v2 registry. This will
  70. // prevent fallback to the v1 protocol.
  71. confirmedV2 bool
  72. // confirmedTLSRegistries is a map indicating which registries
  73. // are known to be using TLS. There should never be a plaintext
  74. // retry for any of these.
  75. confirmedTLSRegistries = make(map[string]struct{})
  76. )
  77. for _, endpoint := range endpoints {
  78. if imagePullConfig.RequireSchema2 && endpoint.Version == registry.APIVersion1 {
  79. continue
  80. }
  81. if confirmedV2 && endpoint.Version == registry.APIVersion1 {
  82. logrus.Debugf("Skipping v1 endpoint %s because v2 registry was detected", endpoint.URL)
  83. continue
  84. }
  85. if endpoint.URL.Scheme != "https" {
  86. if _, confirmedTLS := confirmedTLSRegistries[endpoint.URL.Host]; confirmedTLS {
  87. logrus.Debugf("Skipping non-TLS endpoint %s for host/port that appears to use TLS", endpoint.URL)
  88. continue
  89. }
  90. }
  91. logrus.Debugf("Trying to pull %s from %s %s", reference.FamiliarName(repoInfo.Name), endpoint.URL, endpoint.Version)
  92. puller, err := newPuller(endpoint, repoInfo, imagePullConfig)
  93. if err != nil {
  94. lastErr = err
  95. continue
  96. }
  97. if err := puller.Pull(ctx, ref, imagePullConfig.Platform); err != nil {
  98. // Was this pull cancelled? If so, don't try to fall
  99. // back.
  100. fallback := false
  101. select {
  102. case <-ctx.Done():
  103. default:
  104. if fallbackErr, ok := err.(fallbackError); ok {
  105. fallback = true
  106. confirmedV2 = confirmedV2 || fallbackErr.confirmedV2
  107. if fallbackErr.transportOK && endpoint.URL.Scheme == "https" {
  108. confirmedTLSRegistries[endpoint.URL.Host] = struct{}{}
  109. }
  110. err = fallbackErr.err
  111. }
  112. }
  113. if fallback {
  114. if _, ok := err.(ErrNoSupport); !ok {
  115. // Because we found an error that's not ErrNoSupport, discard all subsequent ErrNoSupport errors.
  116. discardNoSupportErrors = true
  117. // append subsequent errors
  118. lastErr = err
  119. } else if !discardNoSupportErrors {
  120. // Save the ErrNoSupport error, because it's either the first error or all encountered errors
  121. // were also ErrNoSupport errors.
  122. // append subsequent errors
  123. lastErr = err
  124. }
  125. logrus.Infof("Attempting next endpoint for pull after error: %v", err)
  126. continue
  127. }
  128. logrus.Errorf("Not continuing with pull after error: %v", err)
  129. return TranslatePullError(err, ref)
  130. }
  131. imagePullConfig.ImageEventLogger(reference.FamiliarString(ref), reference.FamiliarName(repoInfo.Name), "pull")
  132. return nil
  133. }
  134. if lastErr == nil {
  135. lastErr = fmt.Errorf("no endpoints found for %s", reference.FamiliarString(ref))
  136. }
  137. return TranslatePullError(lastErr, ref)
  138. }
  139. // writeStatus writes a status message to out. If layersDownloaded is true, the
  140. // status message indicates that a newer image was downloaded. Otherwise, it
  141. // indicates that the image is up to date. requestedTag is the tag the message
  142. // will refer to.
  143. func writeStatus(requestedTag string, out progress.Output, layersDownloaded bool) {
  144. if layersDownloaded {
  145. progress.Message(out, "", "Status: Downloaded newer image for "+requestedTag)
  146. } else {
  147. progress.Message(out, "", "Status: Image is up to date for "+requestedTag)
  148. }
  149. }
  150. // ValidateRepoName validates the name of a repository.
  151. func ValidateRepoName(name reference.Named) error {
  152. if reference.FamiliarName(name) == api.NoBaseImageSpecifier {
  153. return errors.WithStack(reservedNameError(api.NoBaseImageSpecifier))
  154. }
  155. return nil
  156. }
  157. func addDigestReference(store refstore.Store, ref reference.Named, dgst digest.Digest, id digest.Digest) error {
  158. dgstRef, err := reference.WithDigest(reference.TrimNamed(ref), dgst)
  159. if err != nil {
  160. return err
  161. }
  162. if oldTagID, err := store.Get(dgstRef); err == nil {
  163. if oldTagID != id {
  164. // Updating digests not supported by reference store
  165. logrus.Errorf("Image ID for digest %s changed from %s to %s, cannot update", dgst.String(), oldTagID, id)
  166. }
  167. return nil
  168. } else if err != refstore.ErrDoesNotExist {
  169. return err
  170. }
  171. return store.AddDigest(dgstRef, id, true)
  172. }