-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Copy pathhuawei_adapter.go
272 lines (237 loc) · 7.4 KB
/
huawei_adapter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package huawei
import (
"crypto/tls"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"regexp"
"strings"
"github.com/goharbor/harbor/src/common/utils/log"
adp "github.com/goharbor/harbor/src/replication/adapter"
"github.com/goharbor/harbor/src/replication/model"
"github.com/goharbor/harbor/src/replication/util"
)
func init() {
err := adp.RegisterFactory(model.RegistryTypeHuawei, AdapterFactory)
if err != nil {
log.Errorf("failed to register factory for Huawei: %v", err)
return
}
log.Infof("the factory of Huawei adapter was registered")
}
// Adapter is for images replications between harbor and Huawei image repository(SWR)
type adapter struct {
*adp.DefaultImageRegistry
registry *model.Registry
}
// Info gets info about Huawei SWR
func (a *adapter) Info() (*model.RegistryInfo, error) {
registryInfo := model.RegistryInfo{
Type: model.RegistryTypeHuawei,
Description: "Adapter for SWR -- The image registry of Huawei Cloud",
SupportedResourceTypes: []model.ResourceType{model.ResourceTypeImage},
SupportedResourceFilters: []*model.FilterStyle{},
SupportedTriggers: []model.TriggerType{},
}
return ®istryInfo, nil
}
// ListNamespaces lists namespaces from Huawei SWR with the provided query conditions.
func (a *adapter) ListNamespaces(query *model.NamespaceQuery) ([]*model.Namespace, error) {
var namespaces []*model.Namespace
urls := fmt.Sprintf("%s/dockyard/v2/visible/namespaces", a.registry.URL)
r, err := http.NewRequest("GET", urls, nil)
if err != nil {
return namespaces, err
}
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{}
if a.registry.Insecure == true {
client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
}
resp, err := client.Do(r)
if err != nil {
return namespaces, err
}
defer resp.Body.Close()
code := resp.StatusCode
if code >= 300 || code < 200 {
body, _ := ioutil.ReadAll(resp.Body)
return namespaces, fmt.Errorf("[%d][%s]", code, string(body))
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return namespaces, err
}
var namespacesData hwNamespaceList
err = json.Unmarshal(body, &namespacesData)
if err != nil {
return namespaces, err
}
reg := fmt.Sprintf(".*%s.*", strings.Replace(query.Name, " ", "", -1))
for _, namespaceData := range namespacesData.Namespace {
namespace := model.Namespace{
Name: namespaceData.Name,
Metadata: namespaceData.metadata(),
}
b, err := regexp.MatchString(reg, namespace.Name)
if err != nil {
return namespaces, nil
}
if b {
namespaces = append(namespaces, &namespace)
}
}
return namespaces, nil
}
// ConvertResourceMetadata convert resource metadata for Huawei SWR
func (a *adapter) ConvertResourceMetadata(resourceMetadata *model.ResourceMetadata, namespace *model.Namespace) (*model.ResourceMetadata, error) {
metadata := &model.ResourceMetadata{
Repository: resourceMetadata.Repository,
Vtags: resourceMetadata.Vtags,
Labels: resourceMetadata.Labels,
}
return metadata, nil
}
// PrepareForPush prepare for push to Huawei SWR
func (a *adapter) PrepareForPush(resources []*model.Resource) error {
namespaces := map[string]struct{}{}
for _, resource := range resources {
paths := strings.Split(resource.Metadata.Repository.Name, "/")
namespace := paths[0]
ns, err := a.GetNamespace(namespace)
if err != nil {
return err
}
if ns != nil && ns.Name == namespace {
continue
}
namespaces[namespace] = struct{}{}
}
url := fmt.Sprintf("%s/dockyard/v2/namespaces", a.registry.URL)
client := &http.Client{
Transport: util.GetHTTPTransport(a.registry.Insecure),
}
for namespace := range namespaces {
namespacebyte, err := json.Marshal(struct {
Namespace string `json:"namespace"`
}{
Namespace: namespace,
})
if err != nil {
return err
}
r, err := http.NewRequest("POST", url, strings.NewReader(string(namespacebyte)))
if err != nil {
return err
}
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)
if err != nil {
return err
}
defer resp.Body.Close()
code := resp.StatusCode
if code >= 300 || code < 200 {
body, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("[%d][%s]", code, string(body))
}
log.Debugf("namespace %s created", namespace)
}
return nil
}
// GetNamespace gets a namespace from Huawei SWR
func (a *adapter) GetNamespace(namespaceStr string) (*model.Namespace, error) {
var namespace = &model.Namespace{
Name: "",
Metadata: make(map[string]interface{}),
}
urls := fmt.Sprintf("%s/dockyard/v2/namespaces/%s", a.registry.URL, namespaceStr)
r, err := http.NewRequest("GET", urls, nil)
if err != nil {
return namespace, err
}
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
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 {
return namespace, err
}
defer resp.Body.Close()
code := resp.StatusCode
if code >= 300 || code < 200 {
body, _ := ioutil.ReadAll(resp.Body)
return namespace, fmt.Errorf("[%d][%s]", code, string(body))
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return namespace, err
}
var namespaceData hwNamespace
err = json.Unmarshal(body, &namespaceData)
if err != nil {
return namespace, err
}
namespace.Name = namespaceData.Name
namespace.Metadata = namespaceData.metadata()
return namespace, nil
}
// HealthCheck check health for huawei SWR
func (a *adapter) HealthCheck() (model.HealthStatus, error) {
return model.Healthy, nil
}
// AdapterFactory is the factory for huawei adapter
func AdapterFactory(registry *model.Registry) (adp.Adapter, error) {
reg, err := adp.NewDefaultImageRegistry(registry)
if err != nil {
return nil, err
}
return &adapter{
registry: registry,
DefaultImageRegistry: reg,
}, nil
}
type hwNamespaceList struct {
Namespace []hwNamespace `json:"namespaces"`
}
type hwNamespace struct {
ID int64 `json:"id" orm:"column(id)"`
Name string `json:"name"`
CreatorName string `json:"creator_name,omitempty"`
DomainPublic int `json:"-"`
Auth int `json:"auth"`
DomainName string `json:"-"`
UserCount int64 `json:"user_count"`
ImageCount int64 `json:"image_count"`
}
func (ns hwNamespace) metadata() map[string]interface{} {
var metadata = make(map[string]interface{})
metadata["id"] = ns.ID
metadata["creator_name"] = ns.CreatorName
metadata["domain_public"] = ns.DomainPublic
metadata["auth"] = ns.Auth
metadata["domain_name"] = ns.DomainName
metadata["user_count"] = ns.UserCount
metadata["image_count"] = ns.ImageCount
return metadata
}