Implement azure acr adapter

Signed-off-by: cd1989 <chende@caicloud.io>
This commit is contained in:
cd1989 2019-06-29 18:26:46 +08:00
parent 6d2d5a6a2a
commit 2097e928d0
10 changed files with 170 additions and 14 deletions

View File

@ -38,6 +38,8 @@ import (
_ "github.com/goharbor/harbor/src/replication/adapter/googlegcr" _ "github.com/goharbor/harbor/src/replication/adapter/googlegcr"
// register the AwsEcr adapter // register the AwsEcr adapter
_ "github.com/goharbor/harbor/src/replication/adapter/awsecr" _ "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 // Replication implements the job interface

View 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
}

View File

@ -0,0 +1 @@
package azurecr

View File

@ -114,6 +114,21 @@ type DefaultImageRegistry struct {
clients map[string]*registry_pkg.Repository 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 // NewDefaultImageRegistry returns an instance of DefaultImageRegistry
func NewDefaultImageRegistry(registry *model.Registry) (*DefaultImageRegistry, error) { func NewDefaultImageRegistry(registry *model.Registry) (*DefaultImageRegistry, error) {
var authorizer modifier.Modifier var authorizer modifier.Modifier

View File

@ -15,6 +15,8 @@
package native package native
import ( import (
"net/http"
"github.com/goharbor/harbor/src/common/utils/log" "github.com/goharbor/harbor/src/common/utils/log"
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"
@ -30,25 +32,39 @@ func init() {
log.Infof("the factory for adapter %s registered", model.RegistryTypeDockerRegistry) 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) reg, err := adp.NewDefaultImageRegistry(registry)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &native{ return &Native{
registry: registry, registry: registry,
DefaultImageRegistry: reg, DefaultImageRegistry: reg,
}, nil }, 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 *adp.DefaultImageRegistry
registry *model.Registry 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{ return &model.RegistryInfo{
Type: model.RegistryTypeDockerRegistry, Type: model.RegistryTypeDockerRegistry,
SupportedResourceTypes: []model.ResourceType{ SupportedResourceTypes: []model.ResourceType{
@ -72,4 +88,4 @@ func (native) Info() (info *model.RegistryInfo, err error) {
} }
// PrepareForPush nothing need to do. // PrepareForPush nothing need to do.
func (native) PrepareForPush([]*model.Resource) error { return nil } func (Native) PrepareForPush([]*model.Resource) error { return nil }

View File

@ -33,7 +33,7 @@ func Test_newAdapter(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
got, err := newAdapter(tt.registry) got, err := NewAdapter(tt.registry)
if tt.wantErr { if tt.wantErr {
assert.NotNil(t, err) assert.NotNil(t, err)
assert.Nil(t, got) assert.Nil(t, got)
@ -48,7 +48,7 @@ func Test_newAdapter(t *testing.T) {
func Test_native_Info(t *testing.T) { func Test_native_Info(t *testing.T) {
var registry = &model.Registry{URL: "abc"} var registry = &model.Registry{URL: "abc"}
var reg, _ = adp.NewDefaultImageRegistry(registry) var reg, _ = adp.NewDefaultImageRegistry(registry)
var adapter = native{ var adapter = Native{
DefaultImageRegistry: reg, DefaultImageRegistry: reg,
registry: registry, registry: registry,
} }
@ -67,7 +67,7 @@ func Test_native_Info(t *testing.T) {
func Test_native_PrepareForPush(t *testing.T) { func Test_native_PrepareForPush(t *testing.T) {
var registry = &model.Registry{URL: "abc"} var registry = &model.Registry{URL: "abc"}
var reg, _ = adp.NewDefaultImageRegistry(registry) var reg, _ = adp.NewDefaultImageRegistry(registry)
var adapter = native{ var adapter = Native{
DefaultImageRegistry: reg, DefaultImageRegistry: reg,
registry: registry, registry: registry,
} }

View File

@ -20,9 +20,10 @@ import (
"github.com/goharbor/harbor/src/replication/util" "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 := "" nameFilterPattern := ""
tagFilterPattern := "" tagFilterPattern := ""
for _, filter := range filters { for _, filter := range filters {
@ -62,7 +63,7 @@ func (n native) FetchImages(filters []*model.Filter) ([]*model.Resource, error)
return resources, nil 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 // if the pattern is a specific repository name, just returns the parsed repositories
// and will check the existence later when filtering the tags // and will check the existence later when filtering the tags
if repositories, ok := util.IsSpecificPath(pattern); ok { if repositories, ok := util.IsSpecificPath(pattern); ok {
@ -90,7 +91,7 @@ func (n native) filterRepositories(pattern string) ([]string, error) {
return result, nil 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) tags, err := n.ListTag(repository)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -72,7 +72,7 @@ func Test_native_FetchImages(t *testing.T) {
var reg, err = adp.NewDefaultImageRegistry(registry) var reg, err = adp.NewDefaultImageRegistry(registry)
assert.NotNil(t, reg) assert.NotNil(t, reg)
assert.Nil(t, err) assert.Nil(t, err)
var adapter = native{ var adapter = Native{
DefaultImageRegistry: reg, DefaultImageRegistry: reg,
registry: registry, registry: registry,
} }

View File

@ -28,6 +28,7 @@ const (
RegistryTypeHuawei RegistryType = "huawei-SWR" RegistryTypeHuawei RegistryType = "huawei-SWR"
RegistryTypeGoogleGcr RegistryType = "google-gcr" RegistryTypeGoogleGcr RegistryType = "google-gcr"
RegistryTypeAwsEcr RegistryType = "aws-ecr" RegistryTypeAwsEcr RegistryType = "aws-ecr"
RegistryTypeAzureAcr RegistryType = "azure-acr"
FilterStyleTypeText = "input" FilterStyleTypeText = "input"
FilterStyleTypeRadio = "radio" FilterStyleTypeRadio = "radio"

View File

@ -39,6 +39,8 @@ import (
_ "github.com/goharbor/harbor/src/replication/adapter/googlegcr" _ "github.com/goharbor/harbor/src/replication/adapter/googlegcr"
// register the AwsEcr adapter // register the AwsEcr adapter
_ "github.com/goharbor/harbor/src/replication/adapter/awsecr" _ "github.com/goharbor/harbor/src/replication/adapter/awsecr"
// register the AzureAcr adapter
_ "github.com/goharbor/harbor/src/replication/adapter/azurecr"
) )
var ( var (