fix: attach labels for replication event (#17108)

Attach labels for replication event, list labels by artifact ID so for
event-based replication rule filter by label can work as expect.

Closes: #17014

Signed-off-by: chlins <chenyuzh@vmware.com>
This commit is contained in:
Chenyu Zhang 2022-07-05 19:57:13 +08:00 committed by GitHub
parent 5b52ec1a3f
commit 927f9f5af3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -23,6 +23,8 @@ import (
"github.com/goharbor/harbor/src/controller/project"
"github.com/goharbor/harbor/src/lib/log"
"github.com/goharbor/harbor/src/lib/orm"
"github.com/goharbor/harbor/src/pkg/label"
labmodel "github.com/goharbor/harbor/src/pkg/label/model"
"github.com/goharbor/harbor/src/pkg/reg/model"
)
@ -61,6 +63,16 @@ func (r *Handler) IsStateful() bool {
return false
}
// abstractLabelNames returns labels name.
func abstractLabelNames(labels []*labmodel.Label) []string {
res := make([]string, 0, len(labels))
for _, lab := range labels {
res = append(res, lab.Name)
}
return res
}
func (r *Handler) handlePushArtifact(ctx context.Context, event *event.PushArtifactEvent) error {
art := event.Artifact
public := false
@ -70,6 +82,12 @@ func (r *Handler) handlePushArtifact(ctx context.Context, event *event.PushArtif
return err
}
public = prj.IsPublic()
// list attached labels
labels, err := label.Mgr.ListByArtifact(ctx, art.ID)
if err != nil {
log.Errorf("failed to list artifact %d labels, error: %v", art.ID, err)
return err
}
e := &repevent.Event{
Type: repevent.EventTypeArtifactPush,
@ -87,6 +105,7 @@ func (r *Handler) handlePushArtifact(ctx context.Context, event *event.PushArtif
Type: art.Type,
Digest: art.Digest,
Tags: event.Tags,
Labels: abstractLabelNames(labels),
}},
},
},
@ -96,6 +115,13 @@ func (r *Handler) handlePushArtifact(ctx context.Context, event *event.PushArtif
func (r *Handler) handleDeleteArtifact(ctx context.Context, event *event.DeleteArtifactEvent) error {
art := event.Artifact
// list attached labels
labels, err := label.Mgr.ListByArtifact(ctx, art.ID)
if err != nil {
log.Errorf("failed to list artifact %d labels, error: %v", art.ID, err)
return err
}
e := &repevent.Event{
Type: repevent.EventTypeArtifactDelete,
Resource: &model.Resource{
@ -109,6 +135,7 @@ func (r *Handler) handleDeleteArtifact(ctx context.Context, event *event.DeleteA
Type: art.Type,
Digest: art.Digest,
Tags: event.Tags,
Labels: abstractLabelNames(labels),
}},
},
Deleted: true,
@ -126,6 +153,12 @@ func (r *Handler) handleCreateTag(ctx context.Context, event *event.CreateTagEve
return err
}
public = prj.IsPublic()
// list attached labels
labels, err := label.Mgr.ListByArtifact(ctx, art.ID)
if err != nil {
log.Errorf("failed to list artifact %d labels, error: %v", art.ID, err)
return err
}
e := &repevent.Event{
Type: repevent.EventTypeArtifactPush,
@ -143,6 +176,7 @@ func (r *Handler) handleCreateTag(ctx context.Context, event *event.CreateTagEve
Type: art.Type,
Digest: art.Digest,
Tags: []string{event.Tag},
Labels: abstractLabelNames(labels),
}},
},
},
@ -152,6 +186,13 @@ func (r *Handler) handleCreateTag(ctx context.Context, event *event.CreateTagEve
func (r *Handler) handleDeleteTag(ctx context.Context, event *event.DeleteTagEvent) error {
art := event.AttachedArtifact
// list attached labels
labels, err := label.Mgr.ListByArtifact(ctx, art.ID)
if err != nil {
log.Errorf("failed to list artifact %d labels, error: %v", art.ID, err)
return err
}
e := &repevent.Event{
Type: repevent.EventTypeTagDelete,
Resource: &model.Resource{
@ -165,6 +206,7 @@ func (r *Handler) handleDeleteTag(ctx context.Context, event *event.DeleteTagEve
Type: art.Type,
Digest: art.Digest,
Tags: []string{event.Tag},
Labels: abstractLabelNames(labels),
}},
},
Deleted: true,