Merge pull request #8172 from cd1989/azure-acr-adapter

Implement azure acr adapter
This commit is contained in:
Wenkai Yin(尹文开) 2019-07-01 19:06:19 +08:00 committed by GitHub
commit 174cfd5de5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 296 additions and 13 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,128 @@
package azurecr
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/goharbor/harbor/src/common/utils/test"
adp "github.com/goharbor/harbor/src/replication/adapter"
"github.com/goharbor/harbor/src/replication/model"
)
func getMockAdapter(t *testing.T, hasCred, health bool) (*adapter, *httptest.Server) {
server := test.NewServer(
&test.RequestHandlerMapping{
Method: http.MethodGet,
Pattern: "/v2/_catalog",
Handler: func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"repositories": ["test1"]}`))
},
},
&test.RequestHandlerMapping{
Method: http.MethodGet,
Pattern: "/v2/{repo}/tags/list",
Handler: func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"name": "test1", "tags": ["latest"]}`))
},
},
&test.RequestHandlerMapping{
Method: http.MethodGet,
Pattern: "/v2/",
Handler: func(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.Method, r.URL)
if health {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusBadRequest)
}
},
},
&test.RequestHandlerMapping{
Method: http.MethodGet,
Pattern: "/",
Handler: func(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.Method, r.URL)
w.WriteHeader(http.StatusOK)
},
},
&test.RequestHandlerMapping{
Method: http.MethodPost,
Pattern: "/",
Handler: func(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.Method, r.URL)
if buf, e := ioutil.ReadAll(&io.LimitedReader{R: r.Body, N: 80}); e == nil {
fmt.Println("\t", string(buf))
}
w.WriteHeader(http.StatusOK)
},
},
)
registry := &model.Registry{
Type: model.RegistryTypeAzureAcr,
URL: server.URL,
}
if hasCred {
registry.Credential = &model.Credential{
AccessKey: "acr",
AccessSecret: "pwd",
}
}
factory, err := adp.GetFactory(model.RegistryTypeAzureAcr)
assert.Nil(t, err)
assert.NotNil(t, factory)
a, err := factory(registry)
assert.Nil(t, err)
return a.(*adapter), server
}
func TestInfo(t *testing.T) {
a, s := getMockAdapter(t, true, true)
defer s.Close()
info, err := a.Info()
assert.Nil(t, err)
assert.NotNil(t, info)
assert.EqualValues(t, 1, len(info.SupportedResourceTypes))
assert.EqualValues(t, model.ResourceTypeImage, info.SupportedResourceTypes[0])
}
func TestHealthCheck(t *testing.T) {
a, s := getMockAdapter(t, true, false)
defer s.Close()
status, err := a.HealthCheck()
assert.Nil(t, err)
assert.NotNil(t, status)
assert.EqualValues(t, model.Unhealthy, status)
a, s = getMockAdapter(t, true, true)
defer s.Close()
status, err = a.HealthCheck()
assert.Nil(t, err)
assert.NotNil(t, status)
assert.EqualValues(t, model.Healthy, status)
}
func TestPrepareForPush(t *testing.T) {
a, s := getMockAdapter(t, true, true)
defer s.Close()
resources := []*model.Resource{
{
Type: model.ResourceTypeImage,
Metadata: &model.ResourceMetadata{
Repository: &model.Repository{
Name: "busybox",
},
},
},
}
err := a.PrepareForPush(resources)
assert.Nil(t, err)
}

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

@ -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 (