pull.go 7.0 KB

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