From da3c85be5a700acae54fa21dc08befb75a20b64c Mon Sep 17 00:00:00 2001 From: Taras Katkov Date: Sat, 30 Mar 2024 09:41:50 -0400 Subject: [PATCH] fix image name extraction (#18992) * Update replication.go It also could be 'library/bitnami/fluentd:1.13.3-debian-10-r0' so we need to split resource to only 2 parts - possible namespace and image name which may include slashes for example - namespace: library, image: bitnami/fluentd:1.13.3-debian-10-r0 Signed-off-by: Taras Katkov * Update replication_test.go Adding namespace and resource extraction tests. Signed-off-by: Taras Katkov * Reformat only Signed-off-by: Taras Katkov --------- Signed-off-by: Taras Katkov --- .../handler/webhook/artifact/replication.go | 4 +++- .../webhook/artifact/replication_test.go | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/controller/event/handler/webhook/artifact/replication.go b/src/controller/event/handler/webhook/artifact/replication.go index fab71978b..56ceb0bb1 100644 --- a/src/controller/event/handler/webhook/artifact/replication.go +++ b/src/controller/event/handler/webhook/artifact/replication.go @@ -216,7 +216,9 @@ func constructReplicationPayload(ctx context.Context, event *event.ReplicationEv func getMetadataFromResource(resource string) (namespace, nameAndTag string) { // Usually resource format likes 'library/busybox:v1', but it could be 'busybox:v1' in docker registry - meta := strings.Split(resource, "/") + // It also could be 'library/bitnami/fluentd:1.13.3-debian-10-r0' so we need to split resource to only 2 parts + // possible namespace and image name which may include slashes for example: bitnami/fluentd:1.13.3-debian-10-r0 + meta := strings.SplitN(resource, "/", 2) if len(meta) == 1 { return "", meta[0] } diff --git a/src/controller/event/handler/webhook/artifact/replication_test.go b/src/controller/event/handler/webhook/artifact/replication_test.go index f920ff25d..8a8d1bca2 100644 --- a/src/controller/event/handler/webhook/artifact/replication_test.go +++ b/src/controller/event/handler/webhook/artifact/replication_test.go @@ -146,3 +146,21 @@ func TestIsLocalRegistry(t *testing.T) { } assert.False(t, isLocalRegistry(reg2)) } + +func TestReplicationHandler_ShortResourceName(t *testing.T) { + namespace, resource := getMetadataFromResource("busybox:v1") + assert.Equal(t, "", namespace) + assert.Equal(t, "busybox:v1", resource) +} + +func TestReplicationHandler_NormalResourceName(t *testing.T) { + namespace, resource := getMetadataFromResource("library/busybox:v1") + assert.Equal(t, "library", namespace) + assert.Equal(t, "busybox:v1", resource) +} + +func TestReplicationHandler_LongResourceName(t *testing.T) { + namespace, resource := getMetadataFromResource("library/bitnami/fluentd:1.13.3-debian-10-r0") + assert.Equal(t, "library", namespace) + assert.Equal(t, "bitnami/fluentd:1.13.3-debian-10-r0", resource) +}