From 56c1d6c149b18214a8d01ab3f1738cae4792109a Mon Sep 17 00:00:00 2001 From: f00231050 Date: Mon, 7 Dec 2020 15:30:11 +0800 Subject: [PATCH] docker: do not return when matched registry mirror reason: append hostname itself to make sure the hostname itself will be tried. --- components/engine/registry/service_v2.go | 86 +++++++++++++++----------------- 1 file changed, 41 insertions(+), 45 deletions(-) mode change 100644 => 100755 components/engine/registry/service_v2.go diff --git a/components/engine/registry/service_v2.go b/components/engine/registry/service_v2.go old mode 100644 new mode 100755 index adeb10c..df66cd7 --- a/components/engine/registry/service_v2.go +++ b/components/engine/registry/service_v2.go @@ -19,8 +19,7 @@ func (s *DefaultService) lookupV2Endpoints(hostname string) (endpoints []APIEndp if reg != nil { var regEndpoints []registrytypes.Endpoint = reg.Mirrors - lastIndex := len(regEndpoints) - 1 - for i, regEP := range regEndpoints { + for _, regEP := range regEndpoints { official := regEP.Address == registrytypes.DefaultEndpoint.Address regURL := regEP.GetURL() @@ -41,49 +40,48 @@ func (s *DefaultService) lookupV2Endpoints(hostname string) (endpoints []APIEndp TLSConfig: tlsConfig, Prefix: hostname, // the last endpoint is not considered a mirror - Mirror: i != lastIndex, + Mirror: true, }) } - return endpoints, nil + // don't return here, otherwise the hostname itself will not be appended to the endpoints, + // and the hostname itself will not be tried, which is not a desired action. } - } else { + } + if hostname == DefaultNamespace || hostname == IndexHostname { tlsConfig = tlsconfig.ServerDefault() - if hostname == DefaultNamespace || hostname == IndexHostname { - // v2 mirrors - for _, mirror := range s.config.Mirrors { - if !strings.HasPrefix(mirror, "http://") && !strings.HasPrefix(mirror, "https://") { - mirror = "https://" + mirror - } - mirrorURL, err := url.Parse(mirror) - if err != nil { - return nil, err - } - mirrorTLSConfig, err := s.tlsConfigForMirror(mirrorURL) - if err != nil { - return nil, err - } - endpoints = append(endpoints, APIEndpoint{ - URL: mirrorURL, - // guess mirrors are v2 - Version: APIVersion2, - Mirror: true, - TrimHostname: true, - TLSConfig: mirrorTLSConfig, - }) + // v2 mirrors + for _, mirror := range s.config.Mirrors { + if !strings.HasPrefix(mirror, "http://") && !strings.HasPrefix(mirror, "https://") { + mirror = "https://" + mirror + } + mirrorURL, err := url.Parse(mirror) + if err != nil { + return nil, err + } + mirrorTLSConfig, err := s.tlsConfigForMirror(mirrorURL) + if err != nil { + return nil, err } - // v2 registry endpoints = append(endpoints, APIEndpoint{ - URL: DefaultV2Registry, + URL: mirrorURL, + // guess mirrors are v2 Version: APIVersion2, - Official: true, + Mirror: true, TrimHostname: true, - TLSConfig: tlsConfig, + TLSConfig: mirrorTLSConfig, }) - - return endpoints, nil } - } + // v2 registry + endpoints = append(endpoints, APIEndpoint{ + URL: DefaultV2Registry, + Version: APIVersion2, + Official: true, + TrimHostname: true, + TLSConfig: tlsConfig, + }) + return endpoints, nil + } ana := allowNondistributableArtifacts(s.config, hostname) tlsConfig, err = s.tlsConfig(hostname) @@ -91,18 +89,16 @@ func (s *DefaultService) lookupV2Endpoints(hostname string) (endpoints []APIEndp return nil, err } - endpoints = []APIEndpoint{ - { - URL: &url.URL{ - Scheme: "https", - Host: hostname, - }, - Version: APIVersion2, - AllowNondistributableArtifacts: ana, - TrimHostname: true, - TLSConfig: tlsConfig, + endpoints = append(endpoints, APIEndpoint{ + URL: &url.URL{ + Scheme: "https", + Host: hostname, }, - } + Version: APIVersion2, + AllowNondistributableArtifacts: ana, + TrimHostname: true, + TLSConfig: tlsConfig, + }) if tlsConfig.InsecureSkipVerify { endpoints = append(endpoints, APIEndpoint{ -- 1.8.3.1