Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cherry-pick] fix robot deletion event (#21234) #21272

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

// 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 @@
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

Check warning on line 67 in src/controller/event/metadata/robot.go

View check run for this annotation

Codecov / codecov/patch

src/controller/event/metadata/robot.go#L67

Added line #L67 was not covered by tests
} else {
cx, exist := security.FromContext(d.Ctx)
if exist {
data.Operator = cx.GetUsername()
}

Check warning on line 72 in src/controller/event/metadata/robot.go

View check run for this annotation

Codecov / codecov/patch

src/controller/event/metadata/robot.go#L71-L72

Added lines #L71 - L72 were not covered by tests
}
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 @@
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 @@
}

// 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 @@
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
}

Check warning on line 170 in src/controller/robot/controller.go

View check run for this annotation

Codecov / codecov/patch

src/controller/robot/controller.go#L169-L170

Added lines #L169 - L170 were not covered by tests
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.

Loading