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