From d0ffb8792577a7361b0268c14245beb4660ed359 Mon Sep 17 00:00:00 2001 From: Urvashi Mohnani Date: Wed, 5 Apr 2023 11:44:42 -0400 Subject: [PATCH] Fix invalid pod name and hostname during kube generate Kube generate on pods was not checking for any underscores in the pod name so was creating a kube yaml with an invalid pod name when there were underscores present. The hostname for the pod is set to the podname by default. There is no need to set that to the container's name or the pod name again in the generated yaml. So removed that field unless a hostname was set for the container by the user. Signed-off-by: Urvashi Mohnani --- libpod/kube.go | 7 ++++--- test/e2e/generate_kube_test.go | 22 ++++++++++++++++++++++ test/system/710-kube.bats | 2 +- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/libpod/kube.go b/libpod/kube.go index 4c8c6a9284..d9741d92e8 100644 --- a/libpod/kube.go +++ b/libpod/kube.go @@ -470,8 +470,8 @@ func (p *Pod) podWithContainers(ctx context.Context, containers []*Container, po // Since hostname is only set at pod level, set the hostname to the hostname of the first container we encounter if hostname == "" { // Only set the hostname if it is not set to the truncated container ID, which we do by default if no - // hostname is specified for the container - if !strings.Contains(ctr.ID(), ctr.Hostname()) { + // hostname is specified for the container and if it is not set to the pod name. + if !strings.Contains(ctr.ID(), ctr.Hostname()) && ctr.Hostname() != p.Name() { hostname = ctr.Hostname() } } @@ -533,9 +533,10 @@ func (p *Pod) podWithContainers(ctx context.Context, containers []*Container, po for _, vol := range deDupPodVolumes { podVolumes = append(podVolumes, *vol) } + podName := removeUnderscores(p.Name()) return newPodObject( - p.Name(), + podName, podAnnotations, podInitCtrs, podContainers, diff --git a/test/e2e/generate_kube_test.go b/test/e2e/generate_kube_test.go index 1b4f158bbb..686dc0d4c8 100644 --- a/test/e2e/generate_kube_test.go +++ b/test/e2e/generate_kube_test.go @@ -1485,4 +1485,26 @@ USER test1` kube.WaitWithDefaultTimeout() Expect(kube).Should(Exit(125)) }) + + It("podman generate kube on pod with invalid name", func() { + podName := "test_pod" + session := podmanTest.Podman([]string{"pod", "create", podName}) + session.WaitWithDefaultTimeout() + Expect(session).Should(Exit(0)) + + session = podmanTest.Podman([]string{"create", "--pod", podName, "--restart", "no", ALPINE, "top"}) + session.WaitWithDefaultTimeout() + Expect(session).Should(Exit(0)) + + kube := podmanTest.Podman([]string{"generate", "kube", podName}) + kube.WaitWithDefaultTimeout() + Expect(kube).Should(Exit(0)) + + pod := new(v1.Pod) + err := yaml.Unmarshal(kube.Out.Contents(), pod) + Expect(err).ToNot(HaveOccurred()) + // The pod name should no longer have _ and there should be no hostname in the generated yaml + Expect(pod.Name).To(Equal("testpod")) + Expect(pod.Spec.Hostname).To(Equal("")) + }) }) diff --git a/test/system/710-kube.bats b/test/system/710-kube.bats index d5b3f9e89b..ce5edf4513 100644 --- a/test/system/710-kube.bats +++ b/test/system/710-kube.bats @@ -105,7 +105,7 @@ metadata.creationTimestamp | =~ | [0-9T:-]\\+Z metadata.labels.app | = | ${pname} metadata.name | = | ${pname} -spec.hostname | = | $pname +spec.hostname | = | null spec.containers[0].command | = | [\"top\"] spec.containers[0].image | = | $IMAGE