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

View File

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