From 49999ab1c02c1fe80f5052b875fefc8bcfa33f71 Mon Sep 17 00:00:00 2001 From: Chenyu Zhang Date: Thu, 4 Aug 2022 15:10:19 +0800 Subject: [PATCH] fix: replication webhook lost when src namespace different with dest (#17312) Fix the replication webhook notification lost when the rule is pull-based and src namespace different with dest. Closes: #17298 Signed-off-by: chlins --- .../handler/webhook/artifact/replication.go | 17 +++++++++++++++-- .../webhook/artifact/replication_test.go | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/controller/event/handler/webhook/artifact/replication.go b/src/controller/event/handler/webhook/artifact/replication.go index c916474ed..f87f6edab 100644 --- a/src/controller/event/handler/webhook/artifact/replication.go +++ b/src/controller/event/handler/webhook/artifact/replication.go @@ -157,7 +157,8 @@ func constructReplicationPayload(event *event.ReplicationEvent) (*model.Payload, var prjName, nameAndTag string // remote(src) -> local harbor(dest) - if rpPolicy.SrcRegistry != nil { + // if the dest registry is local harbor, that is pull-mode replication. + if isLocalRegistry(rpPolicy.DestRegistry) { payload.EventData.Replication.SrcResource = remoteRes payload.EventData.Replication.DestResource = localRes prjName = destNamespace @@ -165,7 +166,8 @@ func constructReplicationPayload(event *event.ReplicationEvent) (*model.Payload, } // local harbor(src) -> remote(dest) - if rpPolicy.DestRegistry != nil { + // if the src registry is local harbor, that is push-mode replication. + if isLocalRegistry(rpPolicy.SrcRegistry) { payload.EventData.Replication.DestResource = remoteRes payload.EventData.Replication.SrcResource = localRes prjName = srcNamespace @@ -206,3 +208,14 @@ func getMetadataFromResource(resource string) (namespace, nameAndTag string) { } return meta[0], meta[1] } + +// isLocalRegistry checks whether the registry is local harbor. +func isLocalRegistry(registry *rpModel.Registry) bool { + if registry != nil { + return registry.Type == rpModel.RegistryTypeHarbor && + registry.Name == "Local" && + registry.URL == config.InternalCoreURL() + } + + return false +} diff --git a/src/controller/event/handler/webhook/artifact/replication_test.go b/src/controller/event/handler/webhook/artifact/replication_test.go index 24948e41e..1dd311347 100644 --- a/src/controller/event/handler/webhook/artifact/replication_test.go +++ b/src/controller/event/handler/webhook/artifact/replication_test.go @@ -33,6 +33,7 @@ import ( "github.com/goharbor/harbor/src/pkg/notification" policy_model "github.com/goharbor/harbor/src/pkg/notification/policy/model" proModels "github.com/goharbor/harbor/src/pkg/project/models" + rpModel "github.com/goharbor/harbor/src/pkg/reg/model" projecttesting "github.com/goharbor/harbor/src/testing/controller/project" replicationtesting "github.com/goharbor/harbor/src/testing/controller/replication" "github.com/goharbor/harbor/src/testing/mock" @@ -128,3 +129,20 @@ func TestReplicationHandler_Name(t *testing.T) { handler := &ReplicationHandler{} assert.Equal(t, "ReplicationWebhook", handler.Name()) } + +func TestIsLocalRegistry(t *testing.T) { + // local registry should return true + reg1 := &rpModel.Registry{ + Type: "harbor", + Name: "Local", + URL: config.InternalCoreURL(), + } + assert.True(t, isLocalRegistry(reg1)) + // non-local registry should return false + reg2 := &rpModel.Registry{ + Type: "docker-registry", + Name: "distribution", + URL: "http://127.0.0.1:5000", + } + assert.False(t, isLocalRegistry(reg2)) +}