pull.go 7.0 KB

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