Merge pull request #10083 from MrMEEE/fix-listings-squashed

Squashed version of PR-9943
This commit is contained in:
Wenkai Yin(尹文开) 2019-12-04 09:35:20 +08:00 committed by GitHub
commit a1712e5332
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 46 deletions

View File

@ -80,13 +80,10 @@ func NewRegistry(endpoint string, client *http.Client) (*Registry, error) {
// Catalog ... // Catalog ...
func (r *Registry) Catalog() ([]string, error) { func (r *Registry) Catalog() ([]string, error) {
repos := []string{} repos := []string{}
suffix := "/v2/_catalog?n=1000" aurl := r.Endpoint.String() + "/v2/_catalog?n=1000"
var url string
for len(suffix) > 0 { for len(aurl) > 0 {
url = r.Endpoint.String() + suffix req, err := http.NewRequest("GET", aurl, nil)
req, err := http.NewRequest("GET", url, nil)
if err != nil { if err != nil {
return repos, err return repos, err
} }
@ -112,11 +109,15 @@ func (r *Registry) Catalog() ([]string, error) {
repos = append(repos, catalogResp.Repositories...) repos = append(repos, catalogResp.Repositories...)
// Link: </v2/_catalog?last=library%2Fhello-world-25&n=100>; rel="next" // Link: </v2/_catalog?last=library%2Fhello-world-25&n=100>; rel="next"
// Link: <http://domain.com/v2/_catalog?last=library%2Fhello-world-25&n=100>; rel="next"
link := resp.Header.Get("Link") link := resp.Header.Get("Link")
if strings.HasSuffix(link, `rel="next"`) && strings.Index(link, "<") >= 0 && strings.Index(link, ">") >= 0 { 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 { } else {
suffix = "" aurl = ""
} }
} else { } else {
return repos, &commonhttp.Error{ return repos, &commonhttp.Error{

View File

@ -69,49 +69,61 @@ func parseError(err error) error {
// ListTag ... // ListTag ...
func (r *Repository) ListTag() ([]string, error) { func (r *Repository) ListTag() ([]string, error) {
tags := []string{} tags := []string{}
req, err := http.NewRequest("GET", buildTagListURL(r.Endpoint.String(), r.Name), nil) aurl := buildTagListURL(r.Endpoint.String(), r.Name)
if err != nil {
return tags, err
}
resp, err := r.client.Do(req) for len(aurl) > 0 {
if err != nil { req, err := http.NewRequest("GET", aurl, nil)
return tags, parseError(err) if err != nil {
}
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 {
return tags, err return tags, err
} }
sort.Strings(tags) resp, err := r.client.Do(req)
tags = tagsResp.Tags if err != nil {
return nil, parseError(err)
}
return tags, nil defer resp.Body.Close()
} else if resp.StatusCode == http.StatusNotFound { b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return tags, err
}
// TODO remove the logic if the bug of registry is fixed if resp.StatusCode == http.StatusOK {
// It's a workaround for a bug of registry: when listing tags of tagsResp := struct {
// a repository which is being pushed, a "NAME_UNKNOWN" error will Tags []string `json:"tags"`
// been returned, while the catalog API can list this repository. }{}
return tags, nil
if err := json.Unmarshal(b, &tagsResp); err != nil {
return tags, err
}
tags = append(tags, tagsResp.Tags...)
// Link: </v2/library/hello-world/tags/list?last=......>; rel="next"
// Link: <http://domain.com/v2/library/hello-world/tags/list?last=......>; 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),
}
}
} }
sort.Strings(tags)
return tags, &commonhttp.Error{ return tags, nil
Code: resp.StatusCode,
Message: string(b),
}
} }
// ManifestExist ... // ManifestExist ...

View File

@ -314,7 +314,7 @@ func Test_native_FetchImages(t *testing.T) {
for i, resource := range resources { for i, resource := range resources {
require.NotNil(t, resource.Metadata) require.NotNil(t, resource.Metadata)
assert.Equal(t, tt.want[i].Metadata.Repository, resource.Metadata.Repository) 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)
} }
} }
}) })