From 0b1c45bc542636d48abcc3980baa02b9a65f57a9 Mon Sep 17 00:00:00 2001
From: Paul Holzinger <pholzing@redhat.com>
Date: Wed, 15 Sep 2021 15:36:00 +0200
Subject: [PATCH] container runlabel remove image tag from name

When no name is given for podman container runlabel it will default to
the image base name. However this can contain a tag. Since podman does
not accept container names with a colon the run command will fail if it
contains something like `podman run --name NAME ...`.

Fixes https://bugzilla.redhat.com/show_bug.cgi?id=2004263

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
---
 pkg/domain/infra/abi/containers_runlabel.go |  3 +++
 test/e2e/runlabel_test.go                   | 18 ++++++++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/pkg/domain/infra/abi/containers_runlabel.go b/pkg/domain/infra/abi/containers_runlabel.go
index d448627dcd..435baa8c8c 100644
--- a/pkg/domain/infra/abi/containers_runlabel.go
+++ b/pkg/domain/infra/abi/containers_runlabel.go
@@ -133,6 +133,9 @@ func generateRunlabelCommand(runlabel string, img *libimage.Image, inputName str
 		}
 		splitImageName := strings.Split(normalize, "/")
 		name = splitImageName[len(splitImageName)-1]
+		// make sure to remove the tag from the image name, otherwise the name cannot
+		// be used as container name because a colon is an illegal character
+		name = strings.SplitN(name, ":", 2)[0]
 	}
 
 	// Append the user-specified arguments to the runlabel (command).
diff --git a/test/e2e/runlabel_test.go b/test/e2e/runlabel_test.go
index e67b6cba11..656eaaceb6 100644
--- a/test/e2e/runlabel_test.go
+++ b/test/e2e/runlabel_test.go
@@ -22,6 +22,10 @@ var GlobalDockerfile = fmt.Sprintf(`
 FROM %s
 LABEL RUN echo \$GLOBAL_OPTS`, ALPINE)
 
+var PodmanRunlabelNameDockerfile = fmt.Sprintf(`
+FROM  %s
+LABEL RUN podman run --name NAME IMAGE`, ALPINE)
+
 var _ = Describe("podman container runlabel", func() {
 	var (
 		tempdir    string
@@ -128,4 +132,18 @@ var _ = Describe("podman container runlabel", func() {
 		result.WaitWithDefaultTimeout()
 		Expect(result).Should(Exit(0))
 	})
+
+	It("podman container runlabel name removes tag from image", func() {
+		image := "podman-runlabel-name:sometag"
+		podmanTest.BuildImage(PodmanRunlabelNameDockerfile, image, "false")
+
+		result := podmanTest.Podman([]string{"container", "runlabel", "--display", "RUN", image})
+		result.WaitWithDefaultTimeout()
+		Expect(result).Should(Exit(0))
+		Expect(result.OutputToString()).To(Equal("command: " + podmanTest.PodmanBinary + " run --name podman-runlabel-name localhost/" + image))
+
+		result = podmanTest.Podman([]string{"rmi", image})
+		result.WaitWithDefaultTimeout()
+		Expect(result).Should(Exit(0))
+	})
 })