mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-27 04:35:16 +01:00
Implement azure acr adapter
Signed-off-by: cd1989 <chende@caicloud.io>
This commit is contained in:
parent
6d2d5a6a2a
commit
2097e928d0
@ -38,6 +38,8 @@ import (
|
||||
_ "github.com/goharbor/harbor/src/replication/adapter/googlegcr"
|
||||
// register the AwsEcr adapter
|
||||
_ "github.com/goharbor/harbor/src/replication/adapter/awsecr"
|
||||
// register the AzureAcr adapter
|
||||
_ "github.com/goharbor/harbor/src/replication/adapter/azurecr"
|
||||
)
|
||||
|
||||
// Replication implements the job interface
|
||||
|
118
src/replication/adapter/azurecr/adapter.go
Normal file
118
src/replication/adapter/azurecr/adapter.go
Normal file
@ -0,0 +1,118 @@
|
||||
package azurecr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/goharbor/harbor/src/common/http/modifier"
|
||||
common_http_auth "github.com/goharbor/harbor/src/common/http/modifier/auth"
|
||||
"github.com/goharbor/harbor/src/common/utils/log"
|
||||
registry_pkg "github.com/goharbor/harbor/src/common/utils/registry"
|
||||
"github.com/goharbor/harbor/src/common/utils/registry/auth"
|
||||
adp "github.com/goharbor/harbor/src/replication/adapter"
|
||||
"github.com/goharbor/harbor/src/replication/adapter/native"
|
||||
"github.com/goharbor/harbor/src/replication/model"
|
||||
"github.com/goharbor/harbor/src/replication/util"
|
||||
)
|
||||
|
||||
func init() {
|
||||
if err := adp.RegisterFactory(model.RegistryTypeAzureAcr, factory); err != nil {
|
||||
log.Errorf("Register adapter factory for %s error: %v", model.RegistryTypeAzureAcr, err)
|
||||
return
|
||||
}
|
||||
log.Infof("Factory for adapter %s registered", model.RegistryTypeAzureAcr)
|
||||
}
|
||||
|
||||
func factory(registry *model.Registry) (adp.Adapter, error) {
|
||||
client, err := getClient(registry)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
reg, err := native.NewWithClient(registry, client)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &adapter{
|
||||
registry: registry,
|
||||
Native: reg,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type adapter struct {
|
||||
*native.Native
|
||||
registry *model.Registry
|
||||
}
|
||||
|
||||
// Ensure '*adapter' implements interface 'Adapter'.
|
||||
var _ adp.Adapter = (*adapter)(nil)
|
||||
|
||||
// Info returns information of the registry
|
||||
func (a *adapter) Info() (*model.RegistryInfo, error) {
|
||||
return &model.RegistryInfo{
|
||||
Type: model.RegistryTypeAzureAcr,
|
||||
SupportedResourceTypes: []model.ResourceType{
|
||||
model.ResourceTypeImage,
|
||||
},
|
||||
SupportedResourceFilters: []*model.FilterStyle{
|
||||
{
|
||||
Type: model.FilterTypeName,
|
||||
Style: model.FilterStyleTypeText,
|
||||
},
|
||||
{
|
||||
Type: model.FilterTypeTag,
|
||||
Style: model.FilterStyleTypeText,
|
||||
},
|
||||
},
|
||||
SupportedTriggers: []model.TriggerType{
|
||||
model.TriggerTypeManual,
|
||||
model.TriggerTypeScheduled,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// PrepareForPush no preparation needed for Azure container registry
|
||||
func (a *adapter) PrepareForPush(resources []*model.Resource) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// HealthCheck checks health status of a registry
|
||||
func (a adapter) HealthCheck() (model.HealthStatus, error) {
|
||||
err := a.PingGet()
|
||||
if err != nil {
|
||||
return model.Unhealthy, nil
|
||||
}
|
||||
|
||||
return model.Healthy, nil
|
||||
}
|
||||
|
||||
func getClient(registry *model.Registry) (*http.Client, error) {
|
||||
if registry.Credential == nil ||
|
||||
len(registry.Credential.AccessKey) == 0 || len(registry.Credential.AccessSecret) == 0 {
|
||||
return nil, fmt.Errorf("no credential to ping registry %s", registry.URL)
|
||||
}
|
||||
|
||||
var cred modifier.Modifier
|
||||
if registry.Credential.Type == model.CredentialTypeSecret {
|
||||
cred = common_http_auth.NewSecretAuthorizer(registry.Credential.AccessSecret)
|
||||
} else {
|
||||
cred = auth.NewBasicAuthCredential(
|
||||
registry.Credential.AccessKey,
|
||||
registry.Credential.AccessSecret)
|
||||
}
|
||||
|
||||
transport := util.GetHTTPTransport(registry.Insecure)
|
||||
modifiers := []modifier.Modifier{
|
||||
&auth.UserAgentModifier{
|
||||
UserAgent: adp.UserAgentReplication,
|
||||
},
|
||||
cred,
|
||||
}
|
||||
|
||||
client := &http.Client{
|
||||
Transport: registry_pkg.NewTransport(transport, modifiers...),
|
||||
}
|
||||
|
||||
return client, nil
|
||||
}
|
1
src/replication/adapter/azurecr/adapter_test.go
Normal file
1
src/replication/adapter/azurecr/adapter_test.go
Normal file
@ -0,0 +1 @@
|
||||
package azurecr
|
@ -114,6 +114,21 @@ type DefaultImageRegistry struct {
|
||||
clients map[string]*registry_pkg.Repository
|
||||
}
|
||||
|
||||
// NewDefaultRegistryWithClient returns an instance of DefaultImageRegistry
|
||||
func NewDefaultRegistryWithClient(registry *model.Registry, client *http.Client) (*DefaultImageRegistry, error) {
|
||||
reg, err := registry_pkg.NewRegistry(registry.URL, client)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &DefaultImageRegistry{
|
||||
Registry: reg,
|
||||
client: client,
|
||||
registry: registry,
|
||||
clients: map[string]*registry_pkg.Repository{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// NewDefaultImageRegistry returns an instance of DefaultImageRegistry
|
||||
func NewDefaultImageRegistry(registry *model.Registry) (*DefaultImageRegistry, error) {
|
||||
var authorizer modifier.Modifier
|
||||
|
@ -15,6 +15,8 @@
|
||||
package native
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/goharbor/harbor/src/common/utils/log"
|
||||
adp "github.com/goharbor/harbor/src/replication/adapter"
|
||||
"github.com/goharbor/harbor/src/replication/model"
|
||||
@ -30,25 +32,39 @@ func init() {
|
||||
log.Infof("the factory for adapter %s registered", model.RegistryTypeDockerRegistry)
|
||||
}
|
||||
|
||||
func newAdapter(registry *model.Registry) (*native, error) {
|
||||
func newAdapter(registry *model.Registry) (*Native, error) {
|
||||
reg, err := adp.NewDefaultImageRegistry(registry)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &native{
|
||||
return &Native{
|
||||
registry: registry,
|
||||
DefaultImageRegistry: reg,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type native struct {
|
||||
// NewWithClient ...
|
||||
func NewWithClient(registry *model.Registry, client *http.Client) (*Native, error) {
|
||||
reg, err := adp.NewDefaultRegistryWithClient(registry, client)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Native{
|
||||
registry: registry,
|
||||
DefaultImageRegistry: reg,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Native is adapter to native docker registry
|
||||
type Native struct {
|
||||
*adp.DefaultImageRegistry
|
||||
registry *model.Registry
|
||||
}
|
||||
|
||||
var _ adp.Adapter = native{}
|
||||
var _ adp.Adapter = Native{}
|
||||
|
||||
func (native) Info() (info *model.RegistryInfo, err error) {
|
||||
// Info ...
|
||||
func (Native) Info() (info *model.RegistryInfo, err error) {
|
||||
return &model.RegistryInfo{
|
||||
Type: model.RegistryTypeDockerRegistry,
|
||||
SupportedResourceTypes: []model.ResourceType{
|
||||
@ -72,4 +88,4 @@ func (native) Info() (info *model.RegistryInfo, err error) {
|
||||
}
|
||||
|
||||
// PrepareForPush nothing need to do.
|
||||
func (native) PrepareForPush([]*model.Resource) error { return nil }
|
||||
func (Native) PrepareForPush([]*model.Resource) error { return nil }
|
||||
|
@ -33,7 +33,7 @@ func Test_newAdapter(t *testing.T) {
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := newAdapter(tt.registry)
|
||||
got, err := NewAdapter(tt.registry)
|
||||
if tt.wantErr {
|
||||
assert.NotNil(t, err)
|
||||
assert.Nil(t, got)
|
||||
@ -48,7 +48,7 @@ func Test_newAdapter(t *testing.T) {
|
||||
func Test_native_Info(t *testing.T) {
|
||||
var registry = &model.Registry{URL: "abc"}
|
||||
var reg, _ = adp.NewDefaultImageRegistry(registry)
|
||||
var adapter = native{
|
||||
var adapter = Native{
|
||||
DefaultImageRegistry: reg,
|
||||
registry: registry,
|
||||
}
|
||||
@ -67,7 +67,7 @@ func Test_native_Info(t *testing.T) {
|
||||
func Test_native_PrepareForPush(t *testing.T) {
|
||||
var registry = &model.Registry{URL: "abc"}
|
||||
var reg, _ = adp.NewDefaultImageRegistry(registry)
|
||||
var adapter = native{
|
||||
var adapter = Native{
|
||||
DefaultImageRegistry: reg,
|
||||
registry: registry,
|
||||
}
|
||||
|
@ -20,9 +20,10 @@ import (
|
||||
"github.com/goharbor/harbor/src/replication/util"
|
||||
)
|
||||
|
||||
var _ adp.ImageRegistry = native{}
|
||||
var _ adp.ImageRegistry = Native{}
|
||||
|
||||
func (n native) FetchImages(filters []*model.Filter) ([]*model.Resource, error) {
|
||||
// FetchImages ...
|
||||
func (n Native) FetchImages(filters []*model.Filter) ([]*model.Resource, error) {
|
||||
nameFilterPattern := ""
|
||||
tagFilterPattern := ""
|
||||
for _, filter := range filters {
|
||||
@ -62,7 +63,7 @@ func (n native) FetchImages(filters []*model.Filter) ([]*model.Resource, error)
|
||||
return resources, nil
|
||||
}
|
||||
|
||||
func (n native) filterRepositories(pattern string) ([]string, error) {
|
||||
func (n Native) filterRepositories(pattern string) ([]string, error) {
|
||||
// if the pattern is a specific repository name, just returns the parsed repositories
|
||||
// and will check the existence later when filtering the tags
|
||||
if repositories, ok := util.IsSpecificPath(pattern); ok {
|
||||
@ -90,7 +91,7 @@ func (n native) filterRepositories(pattern string) ([]string, error) {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (n native) filterTags(repository, pattern string) ([]string, error) {
|
||||
func (n Native) filterTags(repository, pattern string) ([]string, error) {
|
||||
tags, err := n.ListTag(repository)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -72,7 +72,7 @@ func Test_native_FetchImages(t *testing.T) {
|
||||
var reg, err = adp.NewDefaultImageRegistry(registry)
|
||||
assert.NotNil(t, reg)
|
||||
assert.Nil(t, err)
|
||||
var adapter = native{
|
||||
var adapter = Native{
|
||||
DefaultImageRegistry: reg,
|
||||
registry: registry,
|
||||
}
|
||||
|
@ -28,6 +28,7 @@ const (
|
||||
RegistryTypeHuawei RegistryType = "huawei-SWR"
|
||||
RegistryTypeGoogleGcr RegistryType = "google-gcr"
|
||||
RegistryTypeAwsEcr RegistryType = "aws-ecr"
|
||||
RegistryTypeAzureAcr RegistryType = "azure-acr"
|
||||
|
||||
FilterStyleTypeText = "input"
|
||||
FilterStyleTypeRadio = "radio"
|
||||
|
@ -39,6 +39,8 @@ import (
|
||||
_ "github.com/goharbor/harbor/src/replication/adapter/googlegcr"
|
||||
// register the AwsEcr adapter
|
||||
_ "github.com/goharbor/harbor/src/replication/adapter/awsecr"
|
||||
// register the AzureAcr adapter
|
||||
_ "github.com/goharbor/harbor/src/replication/adapter/azurecr"
|
||||
)
|
||||
|
||||
var (
|
||||
|
Loading…
Reference in New Issue
Block a user