From 32c3820b8a07f4753901e9414affe83a79075c4f Mon Sep 17 00:00:00 2001 From: Ashish Amarnath Date: Wed, 21 Apr 2021 17:57:00 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=20Fix=20plugin=20name=20derivat?= =?UTF-8?q?ion=20from=20image=20name=20(#3711)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🐛 Fix plugin name derivation from image name Signed-off-by: Ashish Amarnath * changelog Signed-off-by: Ashish Amarnath --- changelogs/unreleased/3711-ashish-amarnath | 1 + pkg/builder/container_builder.go | 11 +++++++++-- pkg/builder/container_builder_test.go | 12 +++++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 changelogs/unreleased/3711-ashish-amarnath diff --git a/changelogs/unreleased/3711-ashish-amarnath b/changelogs/unreleased/3711-ashish-amarnath new file mode 100644 index 000000000..298a775b5 --- /dev/null +++ b/changelogs/unreleased/3711-ashish-amarnath @@ -0,0 +1 @@ +🐛 Fix plugin name derivation from image name diff --git a/pkg/builder/container_builder.go b/pkg/builder/container_builder.go index b56792e74..981b76c3f 100644 --- a/pkg/builder/container_builder.go +++ b/pkg/builder/container_builder.go @@ -45,7 +45,7 @@ func ForPluginContainer(image string, pullPolicy corev1api.PullPolicy) *Containe // getName returns the 'name' component of a docker // image that includes the entire string except the registry name, and transforms the combined -// string into a DNS-1123 compatible name. +// string into a RFC-1123 compatible name. func getName(image string) string { slashIndex := strings.Index(image, "/") slashCount := 0 @@ -67,7 +67,14 @@ func getName(image string) string { end = colonIndex } - return strings.Replace(image[start:end], "/", "-", -1) // this makes it DNS-1123 compatible + // https://github.com/distribution/distribution/blob/main/docs/spec/api.md#overview + // valid repository names match the regex [a-z0-9]+(?:[._-][a-z0-9]+)* + // image repository names can container [._] but [._] are not allowed in RFC-1123 labels. + // replace '/', '_' and '.' with '-' + re := strings.NewReplacer("/", "-", + "_", "-", + ".", "-") + return re.Replace(image[start:end]) } // Result returns the built Container. diff --git a/pkg/builder/container_builder_test.go b/pkg/builder/container_builder_test.go index f5eaf320d..654100022 100644 --- a/pkg/builder/container_builder_test.go +++ b/pkg/builder/container_builder_test.go @@ -75,7 +75,17 @@ func TestGetName(t *testing.T) { { name: "image name with registry hostname starting with a / will include the registry name ¯\\_(ツ)_/¯", image: "/gcr.io/my-repo/mystery/another/my-image", - expected: "gcr.io-my-repo-mystery-another-my-image", + expected: "gcr-io-my-repo-mystery-another-my-image", + }, + { + name: "image repository names containing _ ", + image: "projects.registry.vmware.com/tanzu_migrator/route-2-httpproxy:myTag", + expected: "tanzu-migrator-route-2-httpproxy", + }, + { + name: "image repository names containing . ", + image: "projects.registry.vmware.com/tanzu.migrator/route-2-httpproxy:myTag", + expected: "tanzu-migrator-route-2-httpproxy", }, }