Skip to content

Commit

Permalink
fix robot deletion event (#21234)
Browse files Browse the repository at this point in the history
* fix robot deletion event

Signed-off-by: wang yan <[email protected]>

* resolve comments

Signed-off-by: wang yan <[email protected]>

---------

Signed-off-by: wang yan <[email protected]>
  • Loading branch information
wy65701436 authored Nov 26, 2024
1 parent 05233b0 commit 29bd094
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 17 deletions.
15 changes: 10 additions & 5 deletions src/controller/event/metadata/robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ func (c *CreateRobotEventMetadata) Resolve(event *event.Event) error {

// DeleteRobotEventMetadata is the metadata from which the delete robot event can be resolved
type DeleteRobotEventMetadata struct {
Ctx context.Context
Robot *model.Robot
Ctx context.Context
Robot *model.Robot
Operator string
}

// Resolve to the event from the metadata
Expand All @@ -62,9 +63,13 @@ func (d *DeleteRobotEventMetadata) Resolve(event *event.Event) error {
Robot: d.Robot,
OccurAt: time.Now(),
}
cx, exist := security.FromContext(d.Ctx)
if exist {
data.Operator = cx.GetUsername()
if d.Operator != "" {
data.Operator = d.Operator
} else {
cx, exist := security.FromContext(d.Ctx)
if exist {
data.Operator = cx.GetUsername()
}
}
data.Robot.Name = fmt.Sprintf("%s%s", config.RobotPrefix(d.Ctx), data.Robot.Name)
event.Topic = event2.TopicDeleteRobot
Expand Down
12 changes: 8 additions & 4 deletions src/controller/robot/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type Controller interface {
Create(ctx context.Context, r *Robot) (int64, string, error)

// Delete ...
Delete(ctx context.Context, id int64) error
Delete(ctx context.Context, id int64, option ...*Option) error

// Update ...
Update(ctx context.Context, r *Robot, option *Option) error
Expand Down Expand Up @@ -149,7 +149,7 @@ func (d *controller) Create(ctx context.Context, r *Robot) (int64, string, error
}

// Delete ...
func (d *controller) Delete(ctx context.Context, id int64) error {
func (d *controller) Delete(ctx context.Context, id int64, option ...*Option) error {
rDelete, err := d.robotMgr.Get(ctx, id)
if err != nil {
return err
Expand All @@ -161,10 +161,14 @@ func (d *controller) Delete(ctx context.Context, id int64) error {
return err
}
// fire event
notification.AddEvent(ctx, &metadata.DeleteRobotEventMetadata{
deleteMetadata := &metadata.DeleteRobotEventMetadata{
Ctx: ctx,
Robot: rDelete,
})
}
if len(option) != 0 && option[0].Operator != "" {
deleteMetadata.Operator = option[0].Operator
}
notification.AddEvent(ctx, deleteMetadata)
return nil
}

Expand Down
1 change: 1 addition & 0 deletions src/controller/robot/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,5 @@ func (p *Permission) IsCoverAll() bool {
// Option ...
type Option struct {
WithPermission bool
Operator string
}
3 changes: 2 additions & 1 deletion src/controller/scan/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"encoding/json"

"github.com/goharbor/harbor/src/common/secret"
"github.com/goharbor/harbor/src/controller/artifact"
"github.com/goharbor/harbor/src/controller/event/metadata"
"github.com/goharbor/harbor/src/controller/event/operator"
Expand Down Expand Up @@ -92,7 +93,7 @@ func scanTaskStatusChange(ctx context.Context, taskID int64, status string) (err

robotID := getRobotID(t.ExtraAttrs)
if robotID > 0 {
if err := robotCtl.Delete(ctx, robotID); err != nil {
if err := robotCtl.Delete(ctx, robotID, &robot.Option{Operator: secret.JobserviceUser}); err != nil {
// Should not block the main flow, just logged
logger.WithFields(log.Fields{"robot_id": robotID, "error": err}).Error("delete robot account failed")
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/controller/scan/callback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (suite *CallbackTestSuite) TestScanTaskStatusChange() {
},
nil,
).Once()
suite.robotCtl.On("Delete", mock.Anything, int64(1)).Return(nil).Once()
suite.robotCtl.On("Delete", mock.Anything, int64(1), mock.Anything).Return(nil).Once()
suite.NoError(scanTaskStatusChange(suite.ctx, 1, job.SuccessStatus.String()))
}

Expand All @@ -108,7 +108,7 @@ func (suite *CallbackTestSuite) TestScanTaskStatusChange() {
},
nil,
).Once()
suite.robotCtl.On("Delete", mock.Anything, int64(1)).Return(fmt.Errorf("failed")).Once()
suite.robotCtl.On("Delete", mock.Anything, int64(1), mock.Anything).Return(fmt.Errorf("failed")).Once()
suite.NoError(scanTaskStatusChange(suite.ctx, 1, job.SuccessStatus.String()))
}

Expand Down
17 changes: 12 additions & 5 deletions src/testing/controller/robot/controller.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 29bd094

Please sign in to comment.