Skip to content

Commit 6812bd2

Browse files
authored
Fix Harbor incompatibility (#55)
1 parent 9884244 commit 6812bd2

File tree

7 files changed

+74
-21
lines changed

7 files changed

+74
-21
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.3.10-dev
1+
0.3.10

build/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

5-
FROM golang:1.24.2@sha256:d9db32125db0c3a680cfb7a1afcaefb89c898a075ec148fdc2f0f646cc2ed509 AS build
5+
FROM golang:1.24.4@sha256:20a022e5112a144aa7b7aeb3f22ebf2cdaefcc4aac0d64e8deeee8cdc18b9c0f AS build
66

77
RUN mkdir /build
88
WORKDIR /build

deploy/charts/app-orch-tenant-controller/Chart.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
apiVersion: v2
66
description: Tenant Controller
77
name: app-orch-tenant-controller
8-
version: 0.3.9
8+
version: 0.3.10
99
annotations:
1010
revision: ""
1111
created: ""
12-
appVersion: "0.3.9"
12+
appVersion: "0.3.10"

internal/plugins/harbor-provisioner.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@ package plugins
66
import (
77
"context"
88
"fmt"
9-
"github.com/open-edge-platform/app-orch-tenant-controller/internal/southbound"
109
"strings"
1110
"time"
11+
12+
"github.com/open-edge-platform/app-orch-tenant-controller/internal/southbound"
1213
)
1314

1415
type Harbor interface {
1516
Configurations(ctx context.Context) error
1617
CreateProject(ctx context.Context, org string, displayName string) error
1718
SetMemberPermissions(ctx context.Context, roleID int, org string, displayName string, groupName string) error
1819
CreateRobot(ctx context.Context, robotName string, org string, displayName string) (string, string, error)
19-
GetRobot(ctx context.Context, org string, displayName string, robotName string) (*southbound.HarborRobot, error)
20-
DeleteRobot(ctx context.Context, org string, displayName string, robotID int) error
20+
GetProjectID(ctx context.Context, org string, displayName string) (int, error)
21+
GetRobot(ctx context.Context, org string, displayName string, robotName string, projectID int) (*southbound.HarborRobot, error)
22+
DeleteRobot(ctx context.Context, robotID int) error
2123
DeleteProject(ctx context.Context, org string, displayName string) error
2224
Ping(ctx context.Context) error
2325
}
@@ -98,10 +100,14 @@ func (p *HarborProvisionerPlugin) CreateEvent(ctx context.Context, event Event,
98100
return err
99101
}
100102

101-
robot, _ := p.harbor.GetRobot(ctx, org, name, "catalog-apps-read-write")
103+
projectID, err := p.harbor.GetProjectID(ctx, org, name)
104+
if err != nil {
105+
return err
106+
}
102107

108+
robot, _ := p.harbor.GetRobot(ctx, org, name, "catalog-apps-read-write", projectID)
103109
if robot != nil {
104-
err = p.harbor.DeleteRobot(ctx, org, name, robot.ID)
110+
err = p.harbor.DeleteRobot(ctx, robot.ID)
105111
if err != nil {
106112
return err
107113
}
@@ -111,6 +117,7 @@ func (p *HarborProvisionerPlugin) CreateEvent(ctx context.Context, event Event,
111117
if err != nil {
112118
return err
113119
}
120+
114121
(*pluginData)[HarborUsernameName] = name
115122
(*pluginData)[HarborTokenName] = secret
116123

internal/plugins/mock-clients_test.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@ package plugins
66
import (
77
"context"
88
"fmt"
9+
"os"
10+
"path/filepath"
11+
912
"github.com/open-edge-platform/app-orch-tenant-controller/internal/config"
1013
"github.com/open-edge-platform/app-orch-tenant-controller/internal/southbound"
1114
"oras.land/oras-go/v2/content/file"
12-
"os"
13-
"path/filepath"
15+
)
16+
17+
const (
18+
HarborProjectID = 1234 // Mock project ID for testing
1419
)
1520

1621
// Catalog client mock
@@ -182,6 +187,10 @@ func (t *testHarbor) Ping(_ context.Context) error {
182187
return nil
183188
}
184189

190+
func (t *testHarbor) GetProjectID(_ context.Context, _ string, _ string) (int, error) {
191+
return HarborProjectID, nil
192+
}
193+
185194
var nextRobotID = 1
186195

187196
func (t *testHarbor) CreateRobot(_ context.Context, robotName string, org string, displayName string) (string, string, error) {
@@ -196,15 +205,18 @@ func (t *testHarbor) CreateRobot(_ context.Context, robotName string, org string
196205
return "name", "secret", nil
197206
}
198207

199-
func (t *testHarbor) GetRobot(_ context.Context, _ string, _ string, robotName string) (*southbound.HarborRobot, error) {
208+
func (t *testHarbor) GetRobot(_ context.Context, _ string, _ string, robotName string, projectID int) (*southbound.HarborRobot, error) {
209+
if projectID != HarborProjectID {
210+
return nil, fmt.Errorf("robot %s projectID %d not found", robotName, projectID)
211+
}
200212
r, ok := t.robots[robotName]
201213
if !ok {
202214
return nil, fmt.Errorf("robot %s not found", robotName)
203215
}
204216
return &southbound.HarborRobot{Name: r.robotName, ID: r.robotID}, nil
205217
}
206218

207-
func (t *testHarbor) DeleteRobot(_ context.Context, _ string, _ string, robotID int) error {
219+
func (t *testHarbor) DeleteRobot(_ context.Context, robotID int) error {
208220
for _, r := range t.robots {
209221
if r.robotID == robotID {
210222
delete(t.robots, r.robotName)

internal/southbound/harbor.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,32 @@ func (h *HarborOCI) SetMemberPermissions(ctx context.Context, roleID int, org st
216216
return nil
217217
}
218218

219+
type HarborProject struct {
220+
ProjectID int `json:"project_id"`
221+
}
222+
223+
func (h *HarborOCI) GetProjectID(ctx context.Context, org string, displayName string) (int, error) {
224+
URL := h.harborHost + "/api/v2.0/projects/" + HarborProjectName(org, displayName)
225+
226+
projectResults := HarborProject{}
227+
resp, err := h.doHarborREST(ctx, http.MethodGet, URL, nil, AddHeaders)
228+
if err != nil {
229+
return 0, err
230+
}
231+
if resp.StatusCode != http.StatusOK {
232+
responseBody, _ := io.ReadAll(resp.Body)
233+
responseJSON := string(responseBody)
234+
return 0, fmt.Errorf("%s", responseJSON)
235+
}
236+
237+
err = json.NewDecoder(resp.Body).Decode(&projectResults)
238+
if err != nil {
239+
return 0, err
240+
}
241+
242+
return projectResults.ProjectID, nil
243+
}
244+
219245
type RobotAccess struct {
220246
Action string `json:"action"`
221247
Resource string `json:"resource"`
@@ -319,9 +345,9 @@ type HarborRobot struct {
319345
UpdateTime time.Time `json:"update_time"`
320346
}
321347

322-
func (h *HarborOCI) GetRobot(ctx context.Context, org string, displayName string, robotName string) (*HarborRobot, error) {
348+
func (h *HarborOCI) GetRobot(ctx context.Context, org string, displayName string, robotName string, projectID int) (*HarborRobot, error) {
323349
robotName = fmt.Sprintf(`robot$%s+%s`, HarborProjectName(org, displayName), robotName)
324-
URL := h.harborHost + "/api/v2.0/projects/" + HarborProjectName(org, displayName) + "/robots"
350+
URL := h.harborHost + "/api/v2.0/robots?q=Level=project,ProjectID=" + fmt.Sprintf("%d", projectID)
325351

326352
robotsResults := []HarborRobot{}
327353
resp, err := h.doHarborREST(ctx, http.MethodGet, URL, nil, AddHeaders)
@@ -349,8 +375,8 @@ func (h *HarborOCI) GetRobot(ctx context.Context, org string, displayName string
349375
return nil, fmt.Errorf("harbor robot %s not found", robotName)
350376
}
351377

352-
func (h *HarborOCI) DeleteRobot(ctx context.Context, org string, displayName string, robotID int) error {
353-
URL := fmt.Sprintf("%s/api/v2.0/projects/%s/robots/%d", h.harborHost, HarborProjectName(org, displayName), robotID)
378+
func (h *HarborOCI) DeleteRobot(ctx context.Context, robotID int) error {
379+
URL := fmt.Sprintf("%s/api/v2.0/robots/%d", h.harborHost, robotID)
354380
resp, err := h.doHarborREST(ctx, http.MethodDelete, URL, nil, AddHeaders)
355381
if err != nil {
356382
return err

internal/southbound/harbor_test.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import (
77
"context"
88
"encoding/json"
99
"fmt"
10-
"github.com/stretchr/testify/suite"
1110
"io"
1211
"net/http"
1312
"net/http/httptest"
1413
"strconv"
1514
"strings"
1615
"testing"
1716
"time"
17+
18+
"github.com/stretchr/testify/suite"
1819
)
1920

2021
// Suite of harbor southbound tests
@@ -132,6 +133,10 @@ func projectHandler(w http.ResponseWriter, r *http.Request) {
132133
} else if r.Method == http.MethodDelete &&
133134
strings.Contains(r.URL.Path, `catalog-apps-org-new-project`) {
134135
w.WriteHeader(http.StatusOK)
136+
} else if r.Method == http.MethodGet {
137+
w.WriteHeader(http.StatusOK)
138+
projectResults := HarborProject{ProjectID: 0}
139+
_ = json.NewEncoder(w).Encode(projectResults)
135140
} else {
136141
w.WriteHeader(http.StatusBadRequest)
137142
}
@@ -281,16 +286,19 @@ func (s *HarborTestSuite) TestHarborCreateRobot() {
281286
s.Len(mockRobots, 1)
282287
s.Equal(mockRobots[name].Name, name)
283288

284-
robot, err := h.GetRobot(s.ctx, "org", "new-project", "new-robot")
289+
projectID, err := h.GetProjectID(s.ctx, "org", "new-project")
290+
s.NoError(err)
291+
292+
robot, err := h.GetRobot(s.ctx, "org", "new-project", "new-robot", projectID)
285293
s.NoError(err)
286294
s.NotNil(robot)
287295
s.Equal("robot$catalog-apps-org-new-project+new-robot", robot.Name)
288296

289-
err = h.DeleteRobot(s.ctx, "org", "new-project", robot.ID)
297+
err = h.DeleteRobot(s.ctx, robot.ID)
290298
s.NoError(err)
291299
s.Len(mockRobots, 0)
292300

293-
robot, err = h.GetRobot(s.ctx, "org", "new-project", "new-robot")
301+
robot, err = h.GetRobot(s.ctx, "org", "new-project", "new-robot", projectID)
294302
s.Error(err)
295303
s.Nil(robot)
296304
}

0 commit comments

Comments
 (0)