2019-01-25 06:22:37 +01:00
|
|
|
// 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 adapter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-07-01 12:33:55 +02:00
|
|
|
"io"
|
2020-03-11 08:03:10 +01:00
|
|
|
"sort"
|
2019-01-25 06:22:37 +01:00
|
|
|
|
2019-07-01 12:33:55 +02:00
|
|
|
"github.com/docker/distribution"
|
2022-07-20 05:33:08 +02:00
|
|
|
|
2021-03-31 09:49:23 +02:00
|
|
|
"github.com/goharbor/harbor/src/pkg/reg/model"
|
2019-01-25 06:22:37 +01:00
|
|
|
)
|
|
|
|
|
2019-07-01 12:33:55 +02:00
|
|
|
// const definition
|
|
|
|
const (
|
2021-10-14 13:05:30 +02:00
|
|
|
MaxConcurrency = 100
|
2019-07-01 12:33:55 +02:00
|
|
|
)
|
|
|
|
|
2021-03-31 09:49:23 +02:00
|
|
|
var registry = map[string]Factory{}
|
2020-03-11 08:03:10 +01:00
|
|
|
var registryKeys = []string{}
|
2021-03-31 09:49:23 +02:00
|
|
|
var adapterInfoMap = map[string]*model.AdapterPattern{}
|
2019-01-25 06:22:37 +01:00
|
|
|
|
2019-03-04 11:33:20 +01:00
|
|
|
// Factory creates a specific Adapter according to the params
|
2019-10-22 11:53:53 +02:00
|
|
|
type Factory interface {
|
|
|
|
Create(*model.Registry) (Adapter, error)
|
|
|
|
AdapterPattern() *model.AdapterPattern
|
|
|
|
}
|
2019-03-04 11:33:20 +01:00
|
|
|
|
2019-01-25 06:22:37 +01:00
|
|
|
// Adapter interface defines the capabilities of registry
|
|
|
|
type Adapter interface {
|
2019-03-23 00:26:10 +01:00
|
|
|
// Info return the information of this adapter
|
|
|
|
Info() (*model.RegistryInfo, error)
|
2019-04-16 10:40:31 +02:00
|
|
|
// PrepareForPush does the prepare work that needed for pushing/uploading the resources
|
2019-04-10 13:37:33 +02:00
|
|
|
// eg: create the namespace or repository
|
2019-04-16 10:40:31 +02:00
|
|
|
PrepareForPush([]*model.Resource) error
|
2019-04-06 12:05:38 +02:00
|
|
|
// HealthCheck checks health status of registry
|
2021-03-31 09:49:23 +02:00
|
|
|
HealthCheck() (string, error)
|
2019-01-25 06:22:37 +01:00
|
|
|
}
|
|
|
|
|
2020-03-12 09:00:23 +01:00
|
|
|
// ArtifactRegistry defines the capabilities that an artifact registry should have
|
|
|
|
type ArtifactRegistry interface {
|
|
|
|
FetchArtifacts(filters []*model.Filter) ([]*model.Resource, error)
|
2020-12-11 11:14:15 +01:00
|
|
|
ManifestExist(repository, reference string) (exist bool, desc *distribution.Descriptor, err error)
|
2020-02-21 02:29:28 +01:00
|
|
|
PullManifest(repository, reference string, accepttedMediaTypes ...string) (manifest distribution.Manifest, digest string, err error)
|
|
|
|
PushManifest(repository, reference, mediaType string, payload []byte) (string, error)
|
2020-03-12 09:00:23 +01:00
|
|
|
DeleteManifest(repository, reference string) error // the "reference" can be "tag" or "digest", the function needs to handle both
|
2019-07-01 12:33:55 +02:00
|
|
|
BlobExist(repository, digest string) (exist bool, err error)
|
|
|
|
PullBlob(repository, digest string) (size int64, blob io.ReadCloser, err error)
|
|
|
|
PushBlob(repository, digest string, size int64, blob io.Reader) error
|
2021-03-18 16:07:21 +01:00
|
|
|
MountBlob(srcRepository, digest, dstRepository string) (err error)
|
|
|
|
CanBeMount(digest string) (mount bool, repository string, err error) // check whether the blob can be mounted from the remote registry
|
2020-03-12 09:00:23 +01:00
|
|
|
DeleteTag(repository, tag string) error
|
2022-02-24 03:27:45 +01:00
|
|
|
ListTags(repository string) (tags []string, err error)
|
2019-07-01 12:33:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ChartRegistry defines the capabilities that a chart registry should have
|
|
|
|
type ChartRegistry interface {
|
|
|
|
FetchCharts(filters []*model.Filter) ([]*model.Resource, error)
|
|
|
|
ChartExist(name, version string) (bool, error)
|
2021-01-06 07:47:38 +01:00
|
|
|
DownloadChart(name, version, contentURL string) (io.ReadCloser, error)
|
2019-07-01 12:33:55 +02:00
|
|
|
UploadChart(name, version string, chart io.Reader) error
|
|
|
|
DeleteChart(name, version string) error
|
|
|
|
}
|
|
|
|
|
2019-01-25 06:22:37 +01:00
|
|
|
// RegisterFactory registers one adapter factory to the registry
|
2021-03-31 09:49:23 +02:00
|
|
|
func RegisterFactory(t string, factory Factory) error {
|
2019-03-23 00:26:10 +01:00
|
|
|
if len(t) == 0 {
|
2019-03-04 11:33:20 +01:00
|
|
|
return errors.New("invalid registry type")
|
|
|
|
}
|
2019-01-25 06:22:37 +01:00
|
|
|
if factory == nil {
|
|
|
|
return errors.New("empty adapter factory")
|
|
|
|
}
|
2019-03-23 00:26:10 +01:00
|
|
|
|
|
|
|
if _, exist := registry[t]; exist {
|
|
|
|
return fmt.Errorf("adapter factory for %s already exists", t)
|
2019-01-25 06:22:37 +01:00
|
|
|
}
|
2019-03-23 00:26:10 +01:00
|
|
|
registry[t] = factory
|
2021-03-31 09:49:23 +02:00
|
|
|
registryKeys = append(registryKeys, t)
|
2020-03-11 08:03:10 +01:00
|
|
|
sort.Strings(registryKeys)
|
2019-10-22 11:53:53 +02:00
|
|
|
adapterInfo := factory.AdapterPattern()
|
2019-07-01 04:46:44 +02:00
|
|
|
if adapterInfo != nil {
|
|
|
|
adapterInfoMap[t] = adapterInfo
|
|
|
|
}
|
2019-01-25 06:22:37 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFactory gets the adapter factory by the specified name
|
2021-03-31 09:49:23 +02:00
|
|
|
func GetFactory(t string) (Factory, error) {
|
2019-03-23 00:26:10 +01:00
|
|
|
factory, exist := registry[t]
|
|
|
|
if !exist {
|
|
|
|
return nil, fmt.Errorf("adapter factory for %s not found", t)
|
2019-01-25 06:22:37 +01:00
|
|
|
}
|
2019-03-23 00:26:10 +01:00
|
|
|
return factory, nil
|
2019-03-04 11:33:20 +01:00
|
|
|
}
|
|
|
|
|
2019-03-23 00:26:10 +01:00
|
|
|
// ListRegisteredAdapterTypes lists the registered Adapter type
|
2021-03-31 09:49:23 +02:00
|
|
|
func ListRegisteredAdapterTypes() []string {
|
|
|
|
return registryKeys
|
2019-01-25 06:22:37 +01:00
|
|
|
}
|
2019-07-01 04:46:44 +02:00
|
|
|
|
2021-03-31 09:49:23 +02:00
|
|
|
// ListRegisteredAdapterInfos list the adapter infos
|
|
|
|
func ListRegisteredAdapterInfos() map[string]*model.AdapterPattern {
|
2019-07-01 04:46:44 +02:00
|
|
|
return adapterInfoMap
|
|
|
|
}
|