Merge pull request #7414 from kofj/optimizing_native_adapter

Optimizing native adapter
This commit is contained in:
Wenkai Yin 2019-04-18 10:02:12 +08:00 committed by GitHub
commit a8dae4025c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 36 deletions

View File

@ -15,10 +15,8 @@
package native
import (
"net/http"
"strings"
common_http "github.com/goharbor/harbor/src/common/http"
adp "github.com/goharbor/harbor/src/replication/adapter"
"github.com/goharbor/harbor/src/replication/model"
"github.com/goharbor/harbor/src/replication/util"
@ -42,7 +40,7 @@ func (n native) FetchImages(filters []*model.Filter) ([]*model.Resource, error)
return nil, err
}
resources := []*model.Resource{}
var resources []*model.Resource
for _, repository := range repositories {
tags, err := n.filterTags(repository, tagFilterPattern)
if err != nil {
@ -70,17 +68,8 @@ func (n native) filterRepositories(pattern string) ([]string, error) {
// if the pattern contains no "*" and "?", it is a specific repository name
// just to make sure the repository exists
if len(pattern) > 0 && !strings.ContainsAny(pattern, "*?") {
_, err := n.ListTag(pattern)
// the repository exists
if err == nil {
return []string{pattern}, nil
}
// the repository doesn't exist
if e, ok := err.(*common_http.Error); ok && e.Code == http.StatusNotFound {
return nil, nil
}
// other error
return nil, err
// check is repository exist later at filterTags.
return []string{pattern}, nil
}
// search repositories from catalog api
repositories, err := n.Catalog()
@ -113,7 +102,7 @@ func (n native) filterTags(repository, pattern string) ([]string, error) {
return tags, nil
}
result := []string{}
var result []string
for _, tag := range tags {
match, err := util.Match(pattern, tag)
if err != nil {

View File

@ -70,27 +70,26 @@ func Test_native_FetchImages(t *testing.T) {
want []*model.Resource
wantErr bool
}{
// TODO: discuss: should we report error if not found in the source native registry.
// {
// name: "repository not exist",
// filters: []*model.Filter{
// {
// Type: model.FilterTypeName,
// Value: "b1",
// },
// },
// wantErr: true,
// },
// {
// name: "tag not exist",
// filters: []*model.Filter{
// {
// Type: model.FilterTypeTag,
// Value: "c",
// },
// },
// wantErr: true,
// },
{
name: "repository not exist",
filters: []*model.Filter{
{
Type: model.FilterTypeName,
Value: "b1",
},
},
wantErr: false,
},
{
name: "tag not exist",
filters: []*model.Filter{
{
Type: model.FilterTypeTag,
Value: "this_tag_not_exist_in_the_mock_server",
},
},
wantErr: false,
},
{
name: "no filters",
filters: []*model.Filter{},