mirror of
https://github.com/containers/podman.git
synced 2025-06-25 12:20:42 +08:00
Add ctrName to network alias during kube play
We currently name the container being created during kube play as ctrName-podName, but this is not how it is done in k8s. Since we can't change this at the CLI level as it will be a breaking change (it will be planned for podman 5.0), add only ctrName as an alias to the network of the pod. Signed-off-by: Urvashi Mohnani <umohnani@redhat.com>
This commit is contained in:
@ -583,6 +583,19 @@ func (ic *ContainerEngine) playKubePod(ctx context.Context, podName string, podY
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add the the original container names from the kube yaml as aliases for it. This will allow network to work with
|
||||||
|
// both just containerName as well as containerName-podName.
|
||||||
|
// In the future, we want to extend this to the CLI as well, where the name of the container created will not have
|
||||||
|
// the podName appended to it, but this is a breaking change and will be done in podman 5.0
|
||||||
|
ctrNameAliases := make([]string, 0, len(podYAML.Spec.Containers))
|
||||||
|
for _, container := range podYAML.Spec.Containers {
|
||||||
|
ctrNameAliases = append(ctrNameAliases, container.Name)
|
||||||
|
}
|
||||||
|
for k, v := range podSpec.PodSpecGen.Networks {
|
||||||
|
v.Aliases = append(v.Aliases, ctrNameAliases...)
|
||||||
|
podSpec.PodSpecGen.Networks[k] = v
|
||||||
|
}
|
||||||
|
|
||||||
if serviceContainer != nil {
|
if serviceContainer != nil {
|
||||||
podSpec.PodSpecGen.ServiceContainerID = serviceContainer.ID()
|
podSpec.PodSpecGen.ServiceContainerID = serviceContainer.ID()
|
||||||
}
|
}
|
||||||
@ -695,6 +708,7 @@ func (ic *ContainerEngine) playKubePod(ctx context.Context, podName string, podY
|
|||||||
if _, ok := ctrNames[container.Name]; ok {
|
if _, ok := ctrNames[container.Name]; ok {
|
||||||
return nil, nil, fmt.Errorf("the pod %q is invalid; duplicate container name %q detected", podName, container.Name)
|
return nil, nil, fmt.Errorf("the pod %q is invalid; duplicate container name %q detected", podName, container.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
ctrNames[container.Name] = ""
|
ctrNames[container.Name] = ""
|
||||||
pulledImage, labels, err := ic.getImageAndLabelInfo(ctx, cwd, annotations, writer, container, options)
|
pulledImage, labels, err := ic.getImageAndLabelInfo(ctx, cwd, annotations, writer, container, options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -5006,4 +5006,30 @@ spec:
|
|||||||
Expect(hostIpcNS).To(Equal(ctrIpcNS))
|
Expect(hostIpcNS).To(Equal(ctrIpcNS))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
It("podman play kube with ctrName should be in network alias", func() {
|
||||||
|
ctrName := "test-ctr"
|
||||||
|
ctrNameInKubePod := ctrName + "-pod-" + ctrName
|
||||||
|
session1 := podmanTest.Podman([]string{"run", "-d", "--name", ctrName, ALPINE, "top"})
|
||||||
|
session1.WaitWithDefaultTimeout()
|
||||||
|
Expect(session1).Should(Exit(0))
|
||||||
|
|
||||||
|
outputFile := filepath.Join(podmanTest.RunRoot, "pod.yaml")
|
||||||
|
kube := podmanTest.Podman([]string{"kube", "generate", ctrName, "-f", outputFile})
|
||||||
|
kube.WaitWithDefaultTimeout()
|
||||||
|
Expect(kube).Should(Exit(0))
|
||||||
|
|
||||||
|
rm := podmanTest.Podman([]string{"pod", "rm", "-t", "0", "-f", ctrName})
|
||||||
|
rm.WaitWithDefaultTimeout()
|
||||||
|
Expect(rm).Should(Exit(0))
|
||||||
|
|
||||||
|
play := podmanTest.Podman([]string{"kube", "play", outputFile})
|
||||||
|
play.WaitWithDefaultTimeout()
|
||||||
|
Expect(play).Should(Exit(0))
|
||||||
|
|
||||||
|
inspect := podmanTest.Podman([]string{"inspect", ctrNameInKubePod})
|
||||||
|
inspect.WaitWithDefaultTimeout()
|
||||||
|
Expect(inspect).Should(Exit(0))
|
||||||
|
Expect(inspect.OutputToString()).To(ContainSubstring("\"Aliases\": [ \"" + ctrName + "\""))
|
||||||
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user