@@ -111,6 +111,7 @@ func newEndpoint(address string, tlsConfig *tls.Config, metaHeaders http.Header)
111111 return endpoint , nil
112112}
113113
114+ // GetEndpoint returns a new endpoint with the specified headers
114115func (repoInfo * RepositoryInfo ) GetEndpoint (metaHeaders http.Header ) (* Endpoint , error ) {
115116 return NewEndpoint (repoInfo .Index , metaHeaders )
116117}
@@ -142,7 +143,10 @@ func (e *Endpoint) Path(path string) string {
142143 return fmt .Sprintf ("%s/v%d/%s" , e .URL , e .Version , path )
143144}
144145
145- func (e * Endpoint ) Ping () (RegistryInfo , error ) {
146+ // Ping pings the remote endpoint with v2 and v1 pings to determine the API
147+ // version. It returns a PingResult containing the discovered version. The
148+ // PingResult also indicates whether the registry is standalone or not.
149+ func (e * Endpoint ) Ping () (PingResult , error ) {
146150 // The ping logic to use is determined by the registry endpoint version.
147151 switch e .Version {
148152 case APIVersion1 :
@@ -167,49 +171,49 @@ func (e *Endpoint) Ping() (RegistryInfo, error) {
167171 }
168172
169173 e .Version = APIVersionUnknown
170- return RegistryInfo {}, fmt .Errorf ("unable to ping registry endpoint %s\n v2 ping attempt failed with error: %s\n v1 ping attempt failed with error: %s" , e , errV2 , errV1 )
174+ return PingResult {}, fmt .Errorf ("unable to ping registry endpoint %s\n v2 ping attempt failed with error: %s\n v1 ping attempt failed with error: %s" , e , errV2 , errV1 )
171175}
172176
173- func (e * Endpoint ) pingV1 () (RegistryInfo , error ) {
177+ func (e * Endpoint ) pingV1 () (PingResult , error ) {
174178 logrus .Debugf ("attempting v1 ping for registry endpoint %s" , e )
175179
176- if e .String () == INDEXSERVER {
180+ if e .String () == IndexServer {
177181 // Skip the check, we know this one is valid
178182 // (and we never want to fallback to http in case of error)
179- return RegistryInfo {Standalone : false }, nil
183+ return PingResult {Standalone : false }, nil
180184 }
181185
182186 req , err := http .NewRequest ("GET" , e .Path ("_ping" ), nil )
183187 if err != nil {
184- return RegistryInfo {Standalone : false }, err
188+ return PingResult {Standalone : false }, err
185189 }
186190
187191 resp , err := e .client .Do (req )
188192 if err != nil {
189- return RegistryInfo {Standalone : false }, err
193+ return PingResult {Standalone : false }, err
190194 }
191195
192196 defer resp .Body .Close ()
193197
194198 jsonString , err := ioutil .ReadAll (resp .Body )
195199 if err != nil {
196- return RegistryInfo {Standalone : false }, fmt .Errorf ("error while reading the http response: %s" , err )
200+ return PingResult {Standalone : false }, fmt .Errorf ("error while reading the http response: %s" , err )
197201 }
198202
199203 // If the header is absent, we assume true for compatibility with earlier
200204 // versions of the registry. default to true
201- info := RegistryInfo {
205+ info := PingResult {
202206 Standalone : true ,
203207 }
204208 if err := json .Unmarshal (jsonString , & info ); err != nil {
205- logrus .Debugf ("Error unmarshalling the _ping RegistryInfo : %s" , err )
209+ logrus .Debugf ("Error unmarshalling the _ping PingResult : %s" , err )
206210 // don't stop here. Just assume sane defaults
207211 }
208212 if hdr := resp .Header .Get ("X-Docker-Registry-Version" ); hdr != "" {
209213 logrus .Debugf ("Registry version header: '%s'" , hdr )
210214 info .Version = hdr
211215 }
212- logrus .Debugf ("RegistryInfo .Version: %q" , info .Version )
216+ logrus .Debugf ("PingResult .Version: %q" , info .Version )
213217
214218 standalone := resp .Header .Get ("X-Docker-Registry-Standalone" )
215219 logrus .Debugf ("Registry standalone header: '%s'" , standalone )
@@ -220,21 +224,21 @@ func (e *Endpoint) pingV1() (RegistryInfo, error) {
220224 // there is a header set, and it is not "true" or "1", so assume fails
221225 info .Standalone = false
222226 }
223- logrus .Debugf ("RegistryInfo .Standalone: %t" , info .Standalone )
227+ logrus .Debugf ("PingResult .Standalone: %t" , info .Standalone )
224228 return info , nil
225229}
226230
227- func (e * Endpoint ) pingV2 () (RegistryInfo , error ) {
231+ func (e * Endpoint ) pingV2 () (PingResult , error ) {
228232 logrus .Debugf ("attempting v2 ping for registry endpoint %s" , e )
229233
230234 req , err := http .NewRequest ("GET" , e .Path ("" ), nil )
231235 if err != nil {
232- return RegistryInfo {}, err
236+ return PingResult {}, err
233237 }
234238
235239 resp , err := e .client .Do (req )
236240 if err != nil {
237- return RegistryInfo {}, err
241+ return PingResult {}, err
238242 }
239243 defer resp .Body .Close ()
240244
@@ -253,21 +257,21 @@ HeaderLoop:
253257 }
254258
255259 if ! supportsV2 {
256- return RegistryInfo {}, fmt .Errorf ("%s does not appear to be a v2 registry endpoint" , e )
260+ return PingResult {}, fmt .Errorf ("%s does not appear to be a v2 registry endpoint" , e )
257261 }
258262
259263 if resp .StatusCode == http .StatusOK {
260264 // It would seem that no authentication/authorization is required.
261265 // So we don't need to parse/add any authorization schemes.
262- return RegistryInfo {Standalone : true }, nil
266+ return PingResult {Standalone : true }, nil
263267 }
264268
265269 if resp .StatusCode == http .StatusUnauthorized {
266270 // Parse the WWW-Authenticate Header and store the challenges
267271 // on this endpoint object.
268272 e .AuthChallenges = parseAuthHeader (resp .Header )
269- return RegistryInfo {}, nil
273+ return PingResult {}, nil
270274 }
271275
272- return RegistryInfo {}, fmt .Errorf ("v2 registry endpoint returned status %d: %q" , resp .StatusCode , http .StatusText (resp .StatusCode ))
276+ return PingResult {}, fmt .Errorf ("v2 registry endpoint returned status %d: %q" , resp .StatusCode , http .StatusText (resp .StatusCode ))
273277}
0 commit comments