-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
manager.go
300 lines (256 loc) · 8.21 KB
/
manager.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package registry
import (
"fmt"
"github.com/goharbor/harbor/src/common/utils"
"github.com/goharbor/harbor/src/common/utils/log"
"github.com/goharbor/harbor/src/replication/adapter"
"github.com/goharbor/harbor/src/replication/config"
"github.com/goharbor/harbor/src/replication/dao"
"github.com/goharbor/harbor/src/replication/dao/models"
"github.com/goharbor/harbor/src/replication/model"
)
// Manager defines the methods that a target manager should implement
type Manager interface {
// Add new registry
Add(*model.Registry) (int64, error)
// List registries, returns total count, registry list and error
List(...*model.RegistryQuery) (int64, []*model.Registry, error)
// Get the specified registry
Get(int64) (*model.Registry, error)
// GetByName gets registry by name
GetByName(name string) (*model.Registry, error)
// Update the registry, the "props" are the properties of registry
// that need to be updated
Update(registry *model.Registry, props ...string) error
// Remove the registry with the specified ID
Remove(int64) error
// HealthCheck checks health status of all registries and update result in database
HealthCheck() error
}
// DefaultManager implement the Manager interface
type DefaultManager struct{}
// NewDefaultManager returns an instance of DefaultManger
func NewDefaultManager() *DefaultManager {
return &DefaultManager{}
}
// Ensure *DefaultManager has implemented Manager interface.
var _ Manager = (*DefaultManager)(nil)
// Get gets a registry by id
func (m *DefaultManager) Get(id int64) (*model.Registry, error) {
registry, err := dao.GetRegistry(id)
if err != nil {
return nil, err
}
if registry == nil {
return nil, nil
}
return fromDaoModel(registry)
}
// GetByName gets a registry by its name
func (m *DefaultManager) GetByName(name string) (*model.Registry, error) {
registry, err := dao.GetRegistryByName(name)
if err != nil {
return nil, err
}
if registry == nil {
return nil, nil
}
return fromDaoModel(registry)
}
// List lists registries according to query provided.
func (m *DefaultManager) List(query ...*model.RegistryQuery) (int64, []*model.Registry, error) {
var registryQueries []*dao.ListRegistryQuery
if len(query) > 0 {
// limit being -1 indicates no pagination specified, result in all registries matching name returned.
listQuery := &dao.ListRegistryQuery{
Query: query[0].Name,
Limit: -1,
}
if query[0].Pagination != nil {
listQuery.Offset = query[0].Pagination.Page * query[0].Pagination.Size
listQuery.Limit = query[0].Pagination.Size
}
registryQueries = append(registryQueries, listQuery)
}
total, registries, err := dao.ListRegistries(registryQueries...)
if err != nil {
return -1, nil, err
}
var results []*model.Registry
for _, r := range registries {
registry, err := fromDaoModel(r)
if err != nil {
return -1, nil, err
}
results = append(results, registry)
}
return total, results, nil
}
// Add adds a new registry
func (m *DefaultManager) Add(registry *model.Registry) (int64, error) {
r, err := toDaoModel(registry)
if err != nil {
log.Errorf("Convert registry model to dao layer model error: %v", err)
return -1, err
}
id, err := dao.AddRegistry(r)
if err != nil {
log.Errorf("Add registry error: %v", err)
return -1, err
}
return id, nil
}
// Update updates a registry
func (m *DefaultManager) Update(registry *model.Registry, props ...string) error {
r, err := toDaoModel(registry)
if err != nil {
log.Errorf("Convert registry model to dao layer model error: %v", err)
return err
}
return dao.UpdateRegistry(r)
}
// Remove deletes a registry
func (m *DefaultManager) Remove(id int64) error {
if err := dao.DeleteRegistry(id); err != nil {
log.Errorf("Delete registry %d error: %v", id, err)
return err
}
return nil
}
// HealthCheck checks health status of every registries and update their status. It will check whether a registry
// is reachable and the credential is valid
func (m *DefaultManager) HealthCheck() error {
_, registries, err := m.List()
if err != nil {
return err
}
errCount := 0
for _, r := range registries {
status, err := CheckHealthStatus(r)
if err != nil {
log.Warningf("Check health status for %s error: %v", r.URL, err)
}
r.Status = string(status)
err = m.Update(r, "status")
if err != nil {
log.Warningf("Update health status for '%s' error: %v", r.URL, err)
errCount++
continue
}
}
if errCount > 0 {
return fmt.Errorf("%d out of %d registries failed to update health status", errCount, len(registries))
}
return nil
}
// CheckHealthStatus checks status of a given registry
func CheckHealthStatus(r *model.Registry) (model.HealthStatus, error) {
if !adapter.HasFactory(r.Type) {
return model.Unknown, fmt.Errorf("no adapter factory for type '%s' registered", r.Type)
}
factory, err := adapter.GetFactory(r.Type)
if err != nil {
return model.Unknown, fmt.Errorf("get adaper for type '%s' error: %v", r.Type, err)
}
rAdapter, err := factory(r)
if err != nil {
return model.Unknown, fmt.Errorf("generate '%s' type adapter form factory error: %v", r.Type, err)
}
return rAdapter.HealthCheck()
}
// decrypt checks whether access secret is set in the registry, if so, decrypt it.
func decrypt(secret string) (string, error) {
if len(secret) == 0 {
return "", nil
}
decrypted, err := utils.ReversibleDecrypt(secret, config.Config.SecretKey)
if err != nil {
return "", err
}
return decrypted, nil
}
// encrypt checks whether access secret is set in the registry, if so, encrypt it.
func encrypt(secret string) (string, error) {
if len(secret) == 0 {
return secret, nil
}
encrypted, err := utils.ReversibleEncrypt(secret, config.Config.SecretKey)
if err != nil {
return "", err
}
return encrypted, nil
}
// fromDaoModel converts DAO layer registry model to replication model.
// Also, if access secret is provided, decrypt it.
func fromDaoModel(registry *models.Registry) (*model.Registry, error) {
r := &model.Registry{
ID: registry.ID,
Name: registry.Name,
Description: registry.Description,
Type: model.RegistryType(registry.Type),
Credential: &model.Credential{},
URL: registry.URL,
Insecure: registry.Insecure,
Status: registry.Health,
CreationTime: registry.CreationTime,
UpdateTime: registry.UpdateTime,
}
if len(registry.AccessKey) != 0 {
credentialType := registry.CredentialType
if len(credentialType) == 0 {
credentialType = model.CredentialTypeBasic
}
decrypted, err := decrypt(registry.AccessSecret)
if err != nil {
return nil, err
}
r.Credential = &model.Credential{
Type: model.CredentialType(credentialType),
AccessKey: registry.AccessKey,
AccessSecret: decrypted,
}
}
return r, nil
}
// toDaoModel converts registry model from replication to DAO layer model.
// Also, if access secret is provided, encrypt it.
func toDaoModel(registry *model.Registry) (*models.Registry, error) {
m := &models.Registry{
ID: registry.ID,
URL: registry.URL,
Name: registry.Name,
Type: string(registry.Type),
Insecure: registry.Insecure,
Description: registry.Description,
Health: registry.Status,
CreationTime: registry.CreationTime,
UpdateTime: registry.UpdateTime,
}
if registry.Credential != nil && len(registry.Credential.AccessKey) != 0 {
credentialType := registry.Credential.Type
if len(credentialType) == 0 {
credentialType = model.CredentialTypeBasic
}
encrypted, err := encrypt(registry.Credential.AccessSecret)
if err != nil {
return nil, err
}
m.CredentialType = string(credentialType)
m.AccessKey = registry.Credential.AccessKey
m.AccessSecret = encrypted
}
return m, nil
}