Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid pod name and hostname during kube generate #18068

Merged
merged 1 commit into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions libpod/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
umohnani8 marked this conversation as resolved.
Show resolved Hide resolved
}
}
Expand Down Expand Up @@ -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())
umohnani8 marked this conversation as resolved.
Show resolved Hide resolved

return newPodObject(
p.Name(),
podName,
podAnnotations,
podInitCtrs,
podContainers,
Expand Down
22 changes: 22 additions & 0 deletions test/e2e/generate_kube_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(""))
})
})
2 changes: 1 addition & 1 deletion test/system/710-kube.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down