Skip to content

Commit

Permalink
feat(retention) refactor to use go swagger api
Browse files Browse the repository at this point in the history
Signed-off-by: Ziming Zhang <zziming@vmware.com>
bitsf committed Jan 8, 2021
1 parent f566748 commit 39fb500
Showing 25 changed files with 1,465 additions and 1,780 deletions.
500 changes: 0 additions & 500 deletions api/v2.0/legacy_swagger.yaml

Large diffs are not rendered by default.

507 changes: 507 additions & 0 deletions api/v2.0/swagger.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/common/dao/testutils.go
Original file line number Diff line number Diff line change
@@ -96,7 +96,7 @@ func initDatabaseForTest(db *models.Database) {
}

if alias != "default" {
if err = globalOrm.Using(alias); err != nil {
if err = GetOrmer().Using(alias); err != nil {
log.Fatalf("failed to create new orm: %v", err)
}
}
2 changes: 1 addition & 1 deletion src/controller/event/handler/init.go
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ func init() {
notifier.Subscribe(event.TopicScanningCompleted, &scan.Handler{})
notifier.Subscribe(event.TopicDeleteArtifact, &scan.DelArtHandler{Context: orm.Context})
notifier.Subscribe(event.TopicReplication, &artifact.ReplicationHandler{})
notifier.Subscribe(event.TopicTagRetention, &artifact.RetentionHandler{RetentionController: artifact.DefaultRetentionControllerFunc})
notifier.Subscribe(event.TopicTagRetention, &artifact.RetentionHandler{})

// replication
notifier.Subscribe(event.TopicPushArtifact, &replication.Handler{})
20 changes: 6 additions & 14 deletions src/controller/event/handler/webhook/artifact/retention.go
Original file line number Diff line number Diff line change
@@ -2,31 +2,22 @@ package artifact

import (
"fmt"
"github.com/goharbor/harbor/src/controller/retention"
"github.com/goharbor/harbor/src/lib/orm"
"strings"

"github.com/goharbor/harbor/src/controller/event"
"github.com/goharbor/harbor/src/controller/event/handler/util"
evtModel "github.com/goharbor/harbor/src/controller/event/model"
"github.com/goharbor/harbor/src/core/api"
"github.com/goharbor/harbor/src/core/config"
"github.com/goharbor/harbor/src/lib/errors"
"github.com/goharbor/harbor/src/lib/log"
"github.com/goharbor/harbor/src/pkg/notification"
"github.com/goharbor/harbor/src/pkg/notifier/model"
"github.com/goharbor/harbor/src/pkg/retention"
)

// RetentionHandler preprocess tag retention event data
type RetentionHandler struct {
RetentionController func() retention.APIController
}

// DefaultRetentionControllerFunc ...
var DefaultRetentionControllerFunc = NewRetentionController

// NewRetentionController ...
func NewRetentionController() retention.APIController {
return api.GetRetentionController()
}

// Name ...
@@ -85,7 +76,8 @@ func (r *RetentionHandler) IsStateful() bool {
}

func (r *RetentionHandler) constructRetentionPayload(event *event.RetentionEvent) (*model.Payload, bool, int64, error) {
task, err := r.RetentionController().GetRetentionExecTask(event.TaskID)
ctx := orm.Context()
task, err := retention.Ctl.GetRetentionExecTask(ctx, event.TaskID)
if err != nil {
log.Errorf("failed to get retention task %d: error: %v", event.TaskID, err)
return nil, false, 0, err
@@ -94,7 +86,7 @@ func (r *RetentionHandler) constructRetentionPayload(event *event.RetentionEvent
return nil, false, 0, fmt.Errorf("task %d not found with retention event", event.TaskID)
}

execution, err := r.RetentionController().GetRetentionExec(task.ExecutionID)
execution, err := retention.Ctl.GetRetentionExec(ctx, task.ExecutionID)
if err != nil {
log.Errorf("failed to get retention execution %d: error: %v", task.ExecutionID, err)
return nil, false, 0, err
@@ -107,7 +99,7 @@ func (r *RetentionHandler) constructRetentionPayload(event *event.RetentionEvent
return nil, true, 0, nil
}

md, err := r.RetentionController().GetRetention(execution.PolicyID)
md, err := retention.Ctl.GetRetention(ctx, execution.PolicyID)
if err != nil {
log.Errorf("failed to get tag retention policy %d: error: %v", execution.PolicyID, err)
return nil, false, 0, err
39 changes: 33 additions & 6 deletions src/controller/event/handler/webhook/artifact/retention_test.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,54 @@
package artifact

import (
"github.com/goharbor/harbor/src/common/dao"
"github.com/goharbor/harbor/src/controller/retention"
ret "github.com/goharbor/harbor/src/pkg/retention"
"github.com/stretchr/testify/mock"
"os"
"testing"
"time"

"github.com/goharbor/harbor/src/pkg/retention"

"github.com/goharbor/harbor/src/controller/event"
"github.com/goharbor/harbor/src/core/config"
"github.com/goharbor/harbor/src/lib/selector"
"github.com/goharbor/harbor/src/pkg/notification"
retentiontesting "github.com/goharbor/harbor/src/testing/controller/retention"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestRetentionHandler_Handle(t *testing.T) {
config.Init()
handler := &RetentionHandler{RetentionController: DefaultRetentionControllerFunc}
handler := &RetentionHandler{}

policyMgr := notification.PolicyMgr
retentionCtlFunc := handler.RetentionController
oldretentionCtl := retention.Ctl

defer func() {
notification.PolicyMgr = policyMgr
handler.RetentionController = retentionCtlFunc
retention.Ctl = oldretentionCtl
}()
notification.PolicyMgr = &fakedNotificationPolicyMgr{}
handler.RetentionController = retention.FakedRetentionControllerFunc
retentionCtl := &retentiontesting.Controller{}
retention.Ctl = retentionCtl
retentionCtl.On("GetRetentionExecTask", mock.Anything, mock.Anything).
Return(&ret.Task{
ID: 1,
ExecutionID: 1,
Status: "Success",
StartTime: time.Now(),
EndTime: time.Now(),
}, nil)
retentionCtl.On("GetRetentionExec", mock.Anything, mock.Anything).Return(&ret.Execution{
ID: 1,
PolicyID: 1,
Status: "Success",
Trigger: "Manual",
DryRun: true,
StartTime: time.Now(),
EndTime: time.Now(),
}, nil)

type args struct {
data interface{}
@@ -80,3 +102,8 @@ func TestRetentionHandler_IsStateful(t *testing.T) {
handler := &RetentionHandler{}
assert.False(t, handler.IsStateful())
}

func TestMain(m *testing.M) {
dao.PrepareTestForPostgresSQL()
os.Exit(m.Run())
}
26 changes: 26 additions & 0 deletions src/controller/retention/callback.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package retention

import (
"context"
"encoding/json"
"fmt"

"github.com/goharbor/harbor/src/lib/log"
"github.com/goharbor/harbor/src/pkg/scheduler"
)

func init() {
err := scheduler.RegisterCallbackFunc(SchedulerCallback, retentionCallback)
if err != nil {
log.Fatalf("failed to register retention callback, %v", err)
}
}

func retentionCallback(ctx context.Context, p string) error {
param := &TriggerParam{}
if err := json.Unmarshal([]byte(p), param); err != nil {
return fmt.Errorf("failed to unmarshal the param: %v", err)
}
_, err := Ctl.TriggerRetentionExec(ctx, param.PolicyID, param.Trigger, false)
return err
}
Loading

0 comments on commit 39fb500

Please sign in to comment.