mirror of
https://github.com/containers/podman.git
synced 2025-07-02 00:30:00 +08:00
Don't rename pod if container has the same name
We enforce the naming scheme "<podname>-<containername>" here [1]. Therefore we must not rename the pod in case of a naming conflict between pod name and container name. Not renaming the pod increases the usability for the user and easies scripting based on the name. Otherwise a user must set some label to reliable find a pod after creation. Or have to implement the renaming logic in the script. [1] https://github.com/containers/podman/blob/main/pkg/specgen/generate/kube/kube.go#L140 Fixes #12722 Signed-off-by: Christoph Petrausch <chrobbert@gmail.com>
This commit is contained in:
@ -178,17 +178,10 @@ func (ic *ContainerEngine) playKubePod(ctx context.Context, podName string, podY
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// check for name collision between pod and container
|
||||
// Assert the pod has a name
|
||||
if podName == "" {
|
||||
return nil, errors.Errorf("pod does not have a name")
|
||||
}
|
||||
for _, n := range podYAML.Spec.Containers {
|
||||
if n.Name == podName {
|
||||
playKubePod.Logs = append(playKubePod.Logs,
|
||||
fmt.Sprintf("a container exists with the same name (%q) as the pod in your YAML file; changing pod name to %s_pod\n", podName, podName))
|
||||
podName = fmt.Sprintf("%s_pod", podName)
|
||||
}
|
||||
}
|
||||
|
||||
podOpt := entities.PodCreateOptions{Infra: true, Net: &entities.NetOptions{NoHosts: options.NoHosts}}
|
||||
podOpt, err = kube.ToPodOpt(ctx, podName, podOpt, podYAML)
|
||||
|
@ -37,6 +37,31 @@ metadata:
|
||||
spec:
|
||||
hostname: unknown
|
||||
`
|
||||
|
||||
var podnameEqualsContainerNameYaml = `
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
name: podnameEqualsContainerNameYaml
|
||||
spec:
|
||||
containers:
|
||||
- name: podnameEqualsContainerNameYaml
|
||||
image: quay.io/libpod/alpine:latest
|
||||
ports:
|
||||
- containerPort: 80
|
||||
`
|
||||
|
||||
var podWithoutAName = `
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
spec:
|
||||
containers:
|
||||
- name: podDoesntHaveAName
|
||||
image: quay.io/libpod/alpine:latest
|
||||
ports:
|
||||
- containerPort: 80
|
||||
`
|
||||
|
||||
var checkInfraImagePodYaml = `
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
@ -1278,6 +1303,39 @@ var _ = Describe("Podman play kube", func() {
|
||||
Expect(sharednamespaces).To(ContainSubstring("pid"))
|
||||
})
|
||||
|
||||
It("podman play kube should not rename pod if container in pod has same name", func() {
|
||||
err := writeYaml(podnameEqualsContainerNameYaml, kubeYaml)
|
||||
Expect(err).To(BeNil())
|
||||
|
||||
kube := podmanTest.Podman([]string{"play", "kube", kubeYaml})
|
||||
kube.WaitWithDefaultTimeout()
|
||||
Expect(kube).Should(Exit(0))
|
||||
|
||||
testPodCreated := podmanTest.Podman([]string{"pod", "exists", "podnameEqualsContainerNameYaml"})
|
||||
testPodCreated.WaitWithDefaultTimeout()
|
||||
Expect(testPodCreated).Should(Exit(0))
|
||||
|
||||
inspect := podmanTest.Podman([]string{"inspect", "podnameEqualsContainerNameYaml"})
|
||||
inspect.WaitWithDefaultTimeout()
|
||||
podInspect := inspect.InspectPodArrToJSON()
|
||||
Expect(podInspect).Should(HaveLen(1))
|
||||
var containerNames []string
|
||||
for _, container := range podInspect[0].Containers {
|
||||
containerNames = append(containerNames, container.Name)
|
||||
}
|
||||
Expect(containerNames).To(ContainElement("podnameEqualsContainerNameYaml-podnameEqualsContainerNameYaml"))
|
||||
})
|
||||
|
||||
It("podman play kube should error if pod dont have a name", func() {
|
||||
err := writeYaml(podWithoutAName, kubeYaml)
|
||||
Expect(err).To(BeNil())
|
||||
|
||||
kube := podmanTest.Podman([]string{"play", "kube", kubeYaml})
|
||||
kube.WaitWithDefaultTimeout()
|
||||
Expect(kube).Should(Exit(125))
|
||||
|
||||
})
|
||||
|
||||
It("podman play kube support container liveness probe", func() {
|
||||
err := writeYaml(livenessProbePodYaml, kubeYaml)
|
||||
Expect(err).To(BeNil())
|
||||
|
Reference in New Issue
Block a user