Skip to content

Commit

Permalink
Merge pull request #12386 from baude/playkuberename
Browse files Browse the repository at this point in the history
Rename pod on generate of container
  • Loading branch information
rhatdan authored Nov 22, 2021
2 parents 3eb1453 + 9c8fb5c commit 992fafa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
11 changes: 10 additions & 1 deletion libpod/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,9 @@ func simplePodWithV1Containers(ctx context.Context, ctrs []*Container) (*v1.Pod,
hostNetwork := true
podDNS := v1.PodDNSConfig{}
kubeAnnotations := make(map[string]string)
ctrNames := make([]string, 0, len(ctrs))
for _, ctr := range ctrs {
ctrNames = append(ctrNames, strings.ReplaceAll(ctr.Name(), "_", ""))
// Convert auto-update labels into kube annotations
for k, v := range getAutoUpdateAnnotations(removeUnderscores(ctr.Name()), ctr.Labels()) {
kubeAnnotations[k] = v
Expand Down Expand Up @@ -484,8 +486,15 @@ func simplePodWithV1Containers(ctx context.Context, ctrs []*Container) (*v1.Pod,
}
} // end if ctrDNS
}
podName := strings.ReplaceAll(ctrs[0].Name(), "_", "")
// Check if the pod name and container name will end up conflicting
// Append _pod if so
if util.StringInSlice(podName, ctrNames) {
podName = podName + "_pod"
}

return newPodObject(
strings.ReplaceAll(ctrs[0].Name(), "_", ""),
podName,
kubeAnnotations,
kubeInitCtrs,
kubeCtrs,
Expand Down
1 change: 1 addition & 0 deletions test/e2e/generate_kube_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ var _ = Describe("Podman generate kube", func() {
Expect(pod.Spec.DNSConfig).To(BeNil())
Expect(pod.Spec.Containers[0].WorkingDir).To(Equal(""))
Expect(pod.Spec.Containers[0].Env).To(BeNil())
Expect(pod.Name).To(Equal("top_pod"))

numContainers := 0
for range pod.Spec.Containers {
Expand Down

0 comments on commit 992fafa

Please sign in to comment.