diff --git a/src/common/utils/registry/registry.go b/src/common/utils/registry/registry.go index 563c25ed2..548ab66b2 100644 --- a/src/common/utils/registry/registry.go +++ b/src/common/utils/registry/registry.go @@ -80,13 +80,10 @@ func NewRegistry(endpoint string, client *http.Client) (*Registry, error) { // Catalog ... func (r *Registry) Catalog() ([]string, error) { repos := []string{} - suffix := "/v2/_catalog?n=1000" - var url string + aurl := r.Endpoint.String() + "/v2/_catalog?n=1000" - for len(suffix) > 0 { - url = r.Endpoint.String() + suffix - - req, err := http.NewRequest("GET", url, nil) + for len(aurl) > 0 { + req, err := http.NewRequest("GET", aurl, nil) if err != nil { return repos, err } @@ -112,11 +109,15 @@ func (r *Registry) Catalog() ([]string, error) { repos = append(repos, catalogResp.Repositories...) // Link: ; rel="next" + // Link: ; rel="next" link := resp.Header.Get("Link") if strings.HasSuffix(link, `rel="next"`) && strings.Index(link, "<") >= 0 && strings.Index(link, ">") >= 0 { - suffix = link[strings.Index(link, "<")+1 : strings.Index(link, ">")] + aurl = link[strings.Index(link, "<")+1 : strings.Index(link, ">")] + if strings.Index(aurl, ":") < 0 { + aurl = r.Endpoint.String() + aurl + } } else { - suffix = "" + aurl = "" } } else { return repos, &commonhttp.Error{ diff --git a/src/common/utils/registry/repository.go b/src/common/utils/registry/repository.go index 94bb2108d..539c05037 100644 --- a/src/common/utils/registry/repository.go +++ b/src/common/utils/registry/repository.go @@ -69,49 +69,61 @@ func parseError(err error) error { // ListTag ... func (r *Repository) ListTag() ([]string, error) { tags := []string{} - req, err := http.NewRequest("GET", buildTagListURL(r.Endpoint.String(), r.Name), nil) - if err != nil { - return tags, err - } + aurl := buildTagListURL(r.Endpoint.String(), r.Name) - resp, err := r.client.Do(req) - if err != nil { - return tags, parseError(err) - } - - defer resp.Body.Close() - - b, err := ioutil.ReadAll(resp.Body) - if err != nil { - return tags, err - } - - if resp.StatusCode == http.StatusOK { - tagsResp := struct { - Tags []string `json:"tags"` - }{} - - if err := json.Unmarshal(b, &tagsResp); err != nil { + for len(aurl) > 0 { + req, err := http.NewRequest("GET", aurl, nil) + if err != nil { return tags, err } - sort.Strings(tags) - tags = tagsResp.Tags + resp, err := r.client.Do(req) + if err != nil { + return nil, parseError(err) + } - return tags, nil - } else if resp.StatusCode == http.StatusNotFound { + defer resp.Body.Close() + b, err := ioutil.ReadAll(resp.Body) + if err != nil { + return tags, err + } - // TODO remove the logic if the bug of registry is fixed - // It's a workaround for a bug of registry: when listing tags of - // a repository which is being pushed, a "NAME_UNKNOWN" error will - // been returned, while the catalog API can list this repository. - return tags, nil + if resp.StatusCode == http.StatusOK { + tagsResp := struct { + Tags []string `json:"tags"` + }{} + + if err := json.Unmarshal(b, &tagsResp); err != nil { + return tags, err + } + + tags = append(tags, tagsResp.Tags...) + // Link: ; rel="next" + // Link: ; rel="next" + link := resp.Header.Get("Link") + if strings.HasSuffix(link, `rel="next"`) && strings.Index(link, "<") >= 0 && strings.Index(link, ">") >= 0 { + aurl = link[strings.Index(link, "<")+1 : strings.Index(link, ">")] + if strings.Index(aurl, ":") < 0 { + aurl = r.Endpoint.String() + aurl + } + } else { + aurl = "" + } + } else if resp.StatusCode == http.StatusNotFound { + + // TODO remove the logic if the bug of registry is fixed + // It's a workaround for a bug of registry: when listing tags of + // a repository which is being pushed, a "NAME_UNKNOWN" error will + // been returned, while the catalog API can list this repository. + return tags, nil + } else { + return tags, &commonhttp.Error{ + Code: resp.StatusCode, + Message: string(b), + } + } } - - return tags, &commonhttp.Error{ - Code: resp.StatusCode, - Message: string(b), - } - + sort.Strings(tags) + return tags, nil } // ManifestExist ... diff --git a/src/replication/adapter/native/adapter_test.go b/src/replication/adapter/native/adapter_test.go index 27b0fe4f0..7f0ead94e 100644 --- a/src/replication/adapter/native/adapter_test.go +++ b/src/replication/adapter/native/adapter_test.go @@ -314,7 +314,7 @@ func Test_native_FetchImages(t *testing.T) { for i, resource := range resources { require.NotNil(t, resource.Metadata) assert.Equal(t, tt.want[i].Metadata.Repository, resource.Metadata.Repository) - assert.Equal(t, tt.want[i].Metadata.Vtags, resource.Metadata.Vtags) + assert.ElementsMatch(t, tt.want[i].Metadata.Vtags, resource.Metadata.Vtags) } } })