forked from goharbor/harbor-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
276 lines (225 loc) · 6.41 KB
/
client.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
package v2
import (
"context"
"fmt"
"strings"
"time"
"github.com/go-logr/logr"
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/health"
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/project"
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/robotv1"
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/plotly/harbor-operator/pkg/rest/model"
utilstring "github.com/plotly/harbor-operator/pkg/utils/strings"
"github.com/pkg/errors"
ctrl "sigs.k8s.io/controller-runtime"
)
// Client for talking to Harbor V2 API
// Wrap based on sdk v2.
type Client struct {
// Server info for talking to
server *model.HarborServer
// Timeout for client connection
timeout time.Duration
// Context for doing client connection
context context.Context
// Harbor API client
harborClient *model.HarborClientV2
// Logger
log logr.Logger
}
// New V2 client.
func New() *Client {
// Initialize with default settings
return &Client{
timeout: 30 * time.Second, //nolint:gomnd
context: context.Background(),
log: ctrl.Log.WithName("v2").WithName("client"),
}
}
// NewWithServer new V2 client with provided server.
func NewWithServer(s *model.HarborServer) (*Client, error) {
var err error
// Initialize with default settings
c := New()
c.server = s
c.harborClient, err = s.ClientV2()
if err != nil {
return nil, err
}
return c, nil
}
func (c *Client) WithContext(ctx context.Context) *Client {
if ctx != nil {
c.context = ctx
}
return c
}
func (c *Client) WithTimeout(timeout time.Duration) *Client {
c.timeout = timeout
return c
}
// EnsureProject ensures the specified project is on the harbor server
// If project with name is existing, then error will be nil.
func (c *Client) EnsureProject(name string) (int64, error) {
if len(name) == 0 {
return -1, errors.New("project name is empty")
}
if c.harborClient == nil {
return -1, errors.New("nil harbor client")
}
// Check existence first
p, err := c.GetProject(name)
if err == nil {
return int64(p.ProjectID), nil
}
if err != nil {
if !strings.Contains(err.Error(), "no project with name") {
return 0, errors.Errorf("error when getting project %s: %s", name, err)
}
}
fmt.Println("creating project since target project doesn't exist")
// Create one when the project does not exist
cparams := project.NewCreateProjectParamsWithContext(c.context).
WithTimeout(c.timeout).
WithProject(&models.ProjectReq{
ProjectName: name,
Metadata: &models.ProjectMetadata{
Public: "false",
},
})
cp, err := c.harborClient.Client.Project.CreateProject(c.context, cparams)
if err != nil {
return -1, fmt.Errorf("ensure project error: %w", err)
}
return utilstring.ExtractID(cp.Location)
}
// GetProject gets the project data.
func (c *Client) GetProject(name string) (*models.Project, error) {
if len(name) == 0 {
return nil, errors.New("project name is empty")
}
if c.harborClient == nil {
return nil, errors.New("nil harbor client")
}
// Use listProject endpoint since getProject requires project id query key
params := project.NewListProjectsParamsWithContext(c.context).
WithTimeout(c.timeout).
WithName(&name)
res, err := c.harborClient.Client.Project.ListProjects(c.context, params)
if err != nil {
return nil, fmt.Errorf("get project error: %w", err)
}
if len(res.Payload) < 1 {
return nil, errors.Errorf("no project with name %s exists", name)
}
return res.Payload[0], nil
}
// DeleteProject deletes project.
func (c *Client) DeleteProject(name string) error {
if len(name) == 0 {
return errors.New("project name is empty")
}
if c.harborClient == nil {
return errors.New("nil harbor client")
}
// Get ID first
p, err := c.GetProject(name)
if err != nil {
return fmt.Errorf("delete project error: %w", err)
}
params := project.NewDeleteProjectParamsWithContext(c.context).
WithTimeout(c.timeout).
WithProjectNameOrID(string(p.ProjectID))
if _, err = c.harborClient.Client.Project.DeleteProject(c.context, params); err != nil {
return err
}
return nil
}
func (c *Client) CheckHealth() (*models.OverallHealthStatus, error) {
params := health.NewGetHealthParams().
WithTimeout(c.timeout)
res, err := c.harborClient.Client.Health.GetHealth(c.context, params)
if err != nil {
return nil, err
}
return res.Payload, nil
}
func (c *Client) CreateRobotAccount(projectID string) (*models.Robot, error) {
if len(projectID) == 0 {
return nil, errors.New("empty project id")
}
if c.harborClient == nil {
return nil, errors.New("nil harbor client")
}
params := robotv1.NewCreateRobotV1Params().
WithTimeout(c.timeout).
WithProjectNameOrID(projectID).
WithRobot(&models.RobotCreateV1{
Access: []*models.Access{
{
Action: "push",
Resource: fmt.Sprintf("/project/%s/repository", projectID),
},
},
Description: "automated by harbor automation operator",
ExpiresAt: -1, // never
Name: utilstring.RandomName("4k8s"),
})
res, err := c.harborClient.Client.Robotv1.CreateRobotV1(c.context, params)
if err != nil {
return nil, err
}
rid, err := utilstring.ExtractID(res.Location)
if err != nil {
// ignore this error that should never happen
c.log.Error(err, "location", res.Location)
}
return &models.Robot{
ID: rid,
Name: res.Payload.Name,
Secret: res.Payload.Secret,
}, nil
}
func (c *Client) DeleteRobotAccount(projectID, robotID int64) error {
if projectID <= 0 {
return errors.New("invalid project id")
}
if robotID <= 0 {
return errors.New("invalid robot id")
}
if c.harborClient == nil {
return errors.New("nil harbor client")
}
params := robotv1.NewDeleteRobotV1Params().
WithTimeout(c.timeout).
WithProjectNameOrID(fmt.Sprintf("%d", projectID)).
WithRobotID(robotID)
if _, err := c.harborClient.Client.Robotv1.DeleteRobotV1(c.context, params); err != nil {
return err
}
return nil
}
func (c *Client) GetRobotAccount(projectID, robotID int64) (*models.Robot, error) {
if projectID <= 0 {
return nil, errors.New("invalid project id")
}
if robotID <= 0 {
return nil, errors.New("invalid robot id")
}
if c.harborClient == nil {
return nil, errors.New("nil harbor client")
}
params := robotv1.NewGetRobotByIDV1Params().
WithTimeout(c.timeout).
WithProjectNameOrID(fmt.Sprintf("%d", projectID)).
WithRobotID(robotID)
res, err := c.harborClient.Client.Robotv1.GetRobotByIDV1(c.context, params)
if err != nil {
return nil, err
}
return &models.Robot{
ID: robotID,
Name: res.Payload.Name,
}, nil
}