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 <chenyuzh@vmware.com>
This commit is contained in:
Chenyu Zhang 2022-08-04 15:10:19 +08:00 committed by GitHub
parent 70a95a9696
commit 49999ab1c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 2 deletions

View File

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

View File

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