Squashed version of PR-9943

Signed-off-by: Martin Juhl <m@rtinjuhl.dk>
This commit is contained in:
Martin Juhl 2019-12-02 11:54:51 +01:00
parent e5f8c2d779
commit 06594a1756
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 ...
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: </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")
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{

View File

@ -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: </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),
}
}
}
return tags, &commonhttp.Error{
Code: resp.StatusCode,
Message: string(b),
}
sort.Strings(tags)
return tags, nil
}
// ManifestExist ...

View File

@ -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)
}
}
})