mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-23 10:45:45 +01:00
Merge pull request #8134 from chlins/refactor/small-refactor-for-huawei-adapter
refactor: adjust some logic in huawei adapter
This commit is contained in:
commit
94f0fb506b
@ -1,8 +1,6 @@
|
|||||||
package huawei
|
package huawei
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
|
||||||
"encoding/base64"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
@ -10,7 +8,10 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
common_http "github.com/goharbor/harbor/src/common/http"
|
||||||
|
"github.com/goharbor/harbor/src/common/http/modifier"
|
||||||
"github.com/goharbor/harbor/src/common/utils/log"
|
"github.com/goharbor/harbor/src/common/utils/log"
|
||||||
|
"github.com/goharbor/harbor/src/common/utils/registry/auth"
|
||||||
adp "github.com/goharbor/harbor/src/replication/adapter"
|
adp "github.com/goharbor/harbor/src/replication/adapter"
|
||||||
"github.com/goharbor/harbor/src/replication/adapter/native"
|
"github.com/goharbor/harbor/src/replication/adapter/native"
|
||||||
"github.com/goharbor/harbor/src/replication/model"
|
"github.com/goharbor/harbor/src/replication/model"
|
||||||
@ -30,6 +31,7 @@ func init() {
|
|||||||
type adapter struct {
|
type adapter struct {
|
||||||
*native.Adapter
|
*native.Adapter
|
||||||
registry *model.Registry
|
registry *model.Registry
|
||||||
|
client *common_http.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
// Info gets info about Huawei SWR
|
// Info gets info about Huawei SWR
|
||||||
@ -56,18 +58,8 @@ func (a *adapter) ListNamespaces(query *model.NamespaceQuery) ([]*model.Namespac
|
|||||||
}
|
}
|
||||||
|
|
||||||
r.Header.Add("content-type", "application/json; charset=utf-8")
|
r.Header.Add("content-type", "application/json; charset=utf-8")
|
||||||
encodeAuth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", a.registry.Credential.AccessKey, a.registry.Credential.AccessSecret)))
|
|
||||||
r.Header.Add("Authorization", "Basic "+encodeAuth)
|
|
||||||
|
|
||||||
client := &http.Client{}
|
resp, err := a.client.Do(r)
|
||||||
if a.registry.Insecure == true {
|
|
||||||
client = &http.Client{
|
|
||||||
Transport: &http.Transport{
|
|
||||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
resp, err := client.Do(r)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return namespaces, err
|
return namespaces, err
|
||||||
}
|
}
|
||||||
@ -120,8 +112,11 @@ func (a *adapter) ConvertResourceMetadata(resourceMetadata *model.ResourceMetada
|
|||||||
func (a *adapter) PrepareForPush(resources []*model.Resource) error {
|
func (a *adapter) PrepareForPush(resources []*model.Resource) error {
|
||||||
namespaces := map[string]struct{}{}
|
namespaces := map[string]struct{}{}
|
||||||
for _, resource := range resources {
|
for _, resource := range resources {
|
||||||
|
var namespace string
|
||||||
paths := strings.Split(resource.Metadata.Repository.Name, "/")
|
paths := strings.Split(resource.Metadata.Repository.Name, "/")
|
||||||
namespace := paths[0]
|
if len(paths) > 0 {
|
||||||
|
namespace = paths[0]
|
||||||
|
}
|
||||||
ns, err := a.GetNamespace(namespace)
|
ns, err := a.GetNamespace(namespace)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -133,9 +128,7 @@ func (a *adapter) PrepareForPush(resources []*model.Resource) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
url := fmt.Sprintf("%s/dockyard/v2/namespaces", a.registry.URL)
|
url := fmt.Sprintf("%s/dockyard/v2/namespaces", a.registry.URL)
|
||||||
client := &http.Client{
|
|
||||||
Transport: util.GetHTTPTransport(a.registry.Insecure),
|
|
||||||
}
|
|
||||||
for namespace := range namespaces {
|
for namespace := range namespaces {
|
||||||
namespacebyte, err := json.Marshal(struct {
|
namespacebyte, err := json.Marshal(struct {
|
||||||
Namespace string `json:"namespace"`
|
Namespace string `json:"namespace"`
|
||||||
@ -152,10 +145,8 @@ func (a *adapter) PrepareForPush(resources []*model.Resource) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
r.Header.Add("content-type", "application/json; charset=utf-8")
|
r.Header.Add("content-type", "application/json; charset=utf-8")
|
||||||
encodeAuth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", a.registry.Credential.AccessKey, a.registry.Credential.AccessSecret)))
|
|
||||||
r.Header.Add("Authorization", "Basic "+encodeAuth)
|
|
||||||
|
|
||||||
resp, err := client.Do(r)
|
resp, err := a.client.Do(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -185,20 +176,8 @@ func (a *adapter) GetNamespace(namespaceStr string) (*model.Namespace, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
r.Header.Add("content-type", "application/json; charset=utf-8")
|
r.Header.Add("content-type", "application/json; charset=utf-8")
|
||||||
encodeAuth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", a.registry.Credential.AccessKey, a.registry.Credential.AccessSecret)))
|
|
||||||
r.Header.Add("Authorization", "Basic "+encodeAuth)
|
|
||||||
|
|
||||||
var client *http.Client
|
resp, err := a.client.Do(r)
|
||||||
if a.registry.Insecure == true {
|
|
||||||
client = &http.Client{
|
|
||||||
Transport: &http.Transport{
|
|
||||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
client = &http.Client{}
|
|
||||||
}
|
|
||||||
resp, err := client.Do(r)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return namespace, err
|
return namespace, err
|
||||||
}
|
}
|
||||||
@ -237,9 +216,30 @@ func AdapterFactory(registry *model.Registry) (adp.Adapter, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
modifiers = []modifier.Modifier{
|
||||||
|
&auth.UserAgentModifier{
|
||||||
|
UserAgent: adp.UserAgentReplication,
|
||||||
|
}}
|
||||||
|
authorizer modifier.Modifier
|
||||||
|
)
|
||||||
|
if registry.Credential != nil {
|
||||||
|
authorizer = auth.NewBasicAuthCredential(
|
||||||
|
registry.Credential.AccessKey,
|
||||||
|
registry.Credential.AccessSecret)
|
||||||
|
modifiers = append(modifiers, authorizer)
|
||||||
|
}
|
||||||
|
|
||||||
return &adapter{
|
return &adapter{
|
||||||
registry: registry,
|
|
||||||
Adapter: dockerRegistryAdapter,
|
Adapter: dockerRegistryAdapter,
|
||||||
|
registry: registry,
|
||||||
|
client: common_http.NewClient(
|
||||||
|
&http.Client{
|
||||||
|
Transport: util.GetHTTPTransport(registry.Insecure),
|
||||||
|
},
|
||||||
|
modifiers...,
|
||||||
|
),
|
||||||
}, nil
|
}, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
package huawei
|
package huawei
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
|
||||||
"encoding/base64"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
@ -25,18 +23,8 @@ func (a *adapter) FetchImages(filters []*model.Filter) ([]*model.Resource, error
|
|||||||
}
|
}
|
||||||
|
|
||||||
r.Header.Add("content-type", "application/json; charset=utf-8")
|
r.Header.Add("content-type", "application/json; charset=utf-8")
|
||||||
encodeAuth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", a.registry.Credential.AccessKey, a.registry.Credential.AccessSecret)))
|
|
||||||
r.Header.Add("Authorization", "Basic "+encodeAuth)
|
|
||||||
|
|
||||||
client := &http.Client{}
|
resp, err := a.client.Do(r)
|
||||||
if a.registry.Insecure == true {
|
|
||||||
client = &http.Client{
|
|
||||||
Transport: &http.Transport{
|
|
||||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
resp, err := client.Do(r)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return resources, err
|
return resources, err
|
||||||
}
|
}
|
||||||
@ -82,15 +70,7 @@ func (a *adapter) ManifestExist(repository, reference string) (exist bool, diges
|
|||||||
r.Header.Add("content-type", "application/json; charset=utf-8")
|
r.Header.Add("content-type", "application/json; charset=utf-8")
|
||||||
r.Header.Add("Authorization", "Bearer "+token.Token)
|
r.Header.Add("Authorization", "Bearer "+token.Token)
|
||||||
|
|
||||||
client := &http.Client{}
|
resp, err := a.client.Do(r)
|
||||||
if a.registry.Insecure == true {
|
|
||||||
client = &http.Client{
|
|
||||||
Transport: &http.Transport{
|
|
||||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
resp, err := client.Do(r)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return exist, digest, err
|
return exist, digest, err
|
||||||
}
|
}
|
||||||
@ -133,15 +113,7 @@ func (a *adapter) DeleteManifest(repository, reference string) error {
|
|||||||
r.Header.Add("content-type", "application/json; charset=utf-8")
|
r.Header.Add("content-type", "application/json; charset=utf-8")
|
||||||
r.Header.Add("Authorization", "Bearer "+token.Token)
|
r.Header.Add("Authorization", "Bearer "+token.Token)
|
||||||
|
|
||||||
client := &http.Client{}
|
resp, err := a.client.Do(r)
|
||||||
if a.registry.Insecure == true {
|
|
||||||
client = &http.Client{
|
|
||||||
Transport: &http.Transport{
|
|
||||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
resp, err := client.Do(r)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -220,18 +192,8 @@ func getJwtToken(a *adapter, repository string) (token jwtToken, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
r.Header.Add("content-type", "application/json; charset=utf-8")
|
r.Header.Add("content-type", "application/json; charset=utf-8")
|
||||||
encodeAuth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", a.registry.Credential.AccessKey, a.registry.Credential.AccessSecret)))
|
|
||||||
r.Header.Add("Authorization", "Basic "+encodeAuth)
|
|
||||||
|
|
||||||
client := &http.Client{}
|
resp, err := a.client.Do(r)
|
||||||
if a.registry.Insecure == true {
|
|
||||||
client = &http.Client{
|
|
||||||
Transport: &http.Transport{
|
|
||||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
resp, err := client.Do(r)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return token, err
|
return token, err
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package huawei
|
package huawei
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -20,7 +21,11 @@ func init() {
|
|||||||
Insecure: false,
|
Insecure: false,
|
||||||
Status: "",
|
Status: "",
|
||||||
}
|
}
|
||||||
HWAdapter.registry = hwRegistry
|
adp, err := AdapterFactory(hwRegistry)
|
||||||
|
if err != nil {
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
HWAdapter = *adp.(*adapter)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAdapter_FetchImages(t *testing.T) {
|
func TestAdapter_FetchImages(t *testing.T) {
|
||||||
|
Loading…
Reference in New Issue
Block a user