mirror of
https://github.com/goharbor/harbor.git
synced 2024-10-05 16:57:49 +02:00
Merge pull request #7379 from ywk253100/190414_label_filter
Fix buf in native docker registry adapter
This commit is contained in:
commit
f0fa90d10f
@ -105,8 +105,6 @@ func NewDefaultImageRegistry(registry *model.Registry) (*DefaultImageRegistry, e
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d *DefaultImageRegistry) getClient(repository string) (*registry_pkg.Repository, error) {
|
func (d *DefaultImageRegistry) getClient(repository string) (*registry_pkg.Repository, error) {
|
||||||
d.RLock()
|
|
||||||
defer d.RUnlock()
|
|
||||||
client, exist := d.clients[repository]
|
client, exist := d.clients[repository]
|
||||||
if exist {
|
if exist {
|
||||||
return client, nil
|
return client, nil
|
||||||
@ -250,6 +248,5 @@ func (d *DefaultImageRegistry) ListTag(repository string) ([]string, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return []string{}, err
|
return []string{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return client.ListTag()
|
return client.ListTag()
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,17 @@
|
|||||||
|
// Copyright Project Harbor Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
package native
|
package native
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -1,9 +1,25 @@
|
|||||||
|
// Copyright Project Harbor Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
package native
|
package native
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"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"
|
||||||
@ -11,64 +27,35 @@ import (
|
|||||||
|
|
||||||
var _ adp.ImageRegistry = native{}
|
var _ adp.ImageRegistry = native{}
|
||||||
|
|
||||||
// TODO: support other filters
|
|
||||||
// MUST have filters and name filter
|
|
||||||
// NOT support namespaces
|
|
||||||
func (n native) FetchImages(namespaces []string, filters []*model.Filter) ([]*model.Resource, error) {
|
func (n native) FetchImages(namespaces []string, filters []*model.Filter) ([]*model.Resource, error) {
|
||||||
if len(namespaces) > 0 {
|
if len(namespaces) > 0 {
|
||||||
return nil, errors.New("native registry adapter not support namespace")
|
return nil, errors.New("native registry adapter not support namespace")
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(filters) < 1 {
|
nameFilterPattern := ""
|
||||||
return nil, errors.New("no any repository filter")
|
tagFilterPattern := ""
|
||||||
}
|
for _, filter := range filters {
|
||||||
|
|
||||||
var resources = []*model.Resource{}
|
|
||||||
var nameFilter, tagFilter *model.Filter
|
|
||||||
for i, filter := range filters {
|
|
||||||
switch filter.Type {
|
switch filter.Type {
|
||||||
case model.FilterTypeName:
|
case model.FilterTypeName:
|
||||||
nameFilter = filters[i]
|
nameFilterPattern = filter.Value.(string)
|
||||||
case model.FilterTypeTag:
|
case model.FilterTypeTag:
|
||||||
tagFilter = filters[i]
|
tagFilterPattern = filter.Value.(string)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
repositories, err := n.filterRepositories(nameFilterPattern)
|
||||||
repositories, err := n.filterRepoistory(nameFilter)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var tagFilterTerm string
|
resources := []*model.Resource{}
|
||||||
var haveWildcardChars bool
|
|
||||||
|
|
||||||
if tagFilter != nil {
|
|
||||||
tagFilterTerm = tagFilter.Value.(string)
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.ContainsAny(tagFilterTerm, "?*") {
|
|
||||||
haveWildcardChars = true
|
|
||||||
}
|
|
||||||
|
|
||||||
if haveWildcardChars || tagFilterTerm == "" {
|
|
||||||
// need call list tag api
|
|
||||||
for _, repository := range repositories {
|
for _, repository := range repositories {
|
||||||
var tags []string
|
tags, err := n.filterTags(repository, tagFilterPattern)
|
||||||
resp, err := n.DefaultImageRegistry.ListTag(repository)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if len(tags) == 0 {
|
||||||
if haveWildcardChars {
|
continue
|
||||||
for _, tag := range resp {
|
|
||||||
if m, _ := util.Match(tagFilterTerm, tag); m {
|
|
||||||
tags = append(tags, tag)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} else {
|
|
||||||
tags = resp
|
|
||||||
}
|
|
||||||
|
|
||||||
resources = append(resources, &model.Resource{
|
resources = append(resources, &model.Resource{
|
||||||
Type: model.ResourceTypeRepository,
|
Type: model.ResourceTypeRepository,
|
||||||
Registry: n.registry,
|
Registry: n.registry,
|
||||||
@ -80,50 +67,66 @@ func (n native) FetchImages(namespaces []string, filters []*model.Filter) ([]*mo
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
} else if tagFilterTerm != "" {
|
|
||||||
for _, repository := range repositories {
|
|
||||||
resources = append(resources, &model.Resource{
|
|
||||||
Type: model.ResourceTypeRepository,
|
|
||||||
Registry: n.registry,
|
|
||||||
Metadata: &model.ResourceMetadata{
|
|
||||||
Repository: &model.Repository{
|
|
||||||
Name: repository,
|
|
||||||
},
|
|
||||||
Vtags: []string{tagFilterTerm},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return resources, nil
|
return resources, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n native) filterRepoistory(nameFilter *model.Filter) (repositories []string, err error) {
|
func (n native) filterRepositories(pattern string) ([]string, error) {
|
||||||
if nameFilter == nil {
|
// if the pattern contains no "*" and "?", it is a specific repository name
|
||||||
return nil, errors.New("native registry adapter must have repository filter")
|
// 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
|
||||||
}
|
}
|
||||||
var nameFilterTerm = nameFilter.Value.(string)
|
// the repository doesn't exist
|
||||||
|
if e, ok := err.(*common_http.Error); ok && e.Code == http.StatusNotFound {
|
||||||
// search repoistories from catalog api
|
return nil, nil
|
||||||
if strings.ContainsAny(nameFilterTerm, "*?") {
|
}
|
||||||
repos, err := n.DefaultImageRegistry.Catalog()
|
// other error
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// search repositories from catalog api
|
||||||
|
repositories, err := n.Catalog()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
// if the pattern is null, just return the result of catalog API
|
||||||
for _, repo := range repos {
|
if len(pattern) == 0 {
|
||||||
m, err := util.Match(nameFilterTerm, repo)
|
return repositories, nil
|
||||||
|
}
|
||||||
|
result := []string{}
|
||||||
|
for _, repository := range repositories {
|
||||||
|
match, err := util.Match(pattern, repository)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if m {
|
if match {
|
||||||
repositories = append(repositories, repo)
|
result = append(result, repository)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if nameFilterTerm != "" {
|
return result, nil
|
||||||
// only single repository
|
|
||||||
repositories = []string{nameFilterTerm}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
func (n native) filterTags(repository, pattern string) ([]string, error) {
|
||||||
|
tags, err := n.ListTag(repository)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(pattern) == 0 {
|
||||||
|
return tags, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
result := []string{}
|
||||||
|
for _, tag := range tags {
|
||||||
|
match, err := util.Match(pattern, tag)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if match {
|
||||||
|
result = append(result, tag)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user