Add infra-name annotations to kube gen/play

Add io.podman.annotations.infra.name annotation to kube play so
users can set the name of the infra container created.
When a pod is created with --infra-name set, the generated
kube yaml will have an infraName annotation set that will
be used when playing the generated yaml with podman.

Signed-off-by: Urvashi Mohnani <umohnani@redhat.com>
This commit is contained in:
Urvashi Mohnani
2023-08-03 10:07:09 -04:00
parent f29986178e
commit 52ed7fce2a
8 changed files with 141 additions and 3 deletions

View File

@@ -1838,4 +1838,46 @@ EXPOSE 2004-2005/tcp`, ALPINE)
Expect(err).ToNot(HaveOccurred())
Expect(pod.Annotations).To(HaveKeyWithValue(define.InspectAnnotationPublishAll+"/"+ctr, define.InspectResponseTrue))
})
It("podman generate kube on pod with --infra-name set", func() {
infraName := "infra-ctr"
podName := "test-pod"
podSession := podmanTest.Podman([]string{"pod", "create", "--infra-name", infraName, podName})
podSession.WaitWithDefaultTimeout()
Expect(podSession).Should(Exit(0))
session := podmanTest.Podman([]string{"create", "--pod", podName, 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())
Expect(pod.Annotations).To(HaveKeyWithValue(define.InfraNameAnnotation, infraName))
})
It("podman generate kube on pod without --infra-name set", func() {
podName := "test-pod"
podSession := podmanTest.Podman([]string{"pod", "create", podName})
podSession.WaitWithDefaultTimeout()
Expect(podSession).Should(Exit(0))
session := podmanTest.Podman([]string{"create", "--pod", podName, ALPINE, "top"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
kube := podmanTest.Podman([]string{"generate", "kube", podName})
kube.WaitWithDefaultTimeout()
Expect(kube).Should(Exit(0))
// There should be no infra name annotation set if the --infra-name flag wasn't set during pod creation
pod := new(v1.Pod)
err := yaml.Unmarshal(kube.Out.Contents(), pod)
Expect(err).ToNot(HaveOccurred())
Expect(pod.Annotations).To(BeEmpty())
})
})

View File

@@ -5879,4 +5879,80 @@ EXPOSE 2004-2005/tcp`, ALPINE)
Expect(inspect).Should(Exit(0))
Expect(inspect.OutputToString()).To(Equal(defaultUmask))
})
// podman play with infra name annotation
It("podman play kube test with infra name annotation set", func() {
infraName := "infra-ctr"
podName := "mypod"
outputFile := filepath.Join(podmanTest.TempDir, "pod.yaml")
pod := podmanTest.Podman([]string{"pod", "create", "--infra-name", infraName, podName})
pod.WaitWithDefaultTimeout()
Expect(pod).Should(Exit(0))
ctr := podmanTest.Podman([]string{"create", "--pod", podName, ALPINE, "top"})
ctr.WaitWithDefaultTimeout()
Expect(ctr).Should(Exit(0))
// Generate kube yaml and it should have the infra name annotation set
gen := podmanTest.Podman([]string{"kube", "generate", "-f", outputFile, podName})
gen.WaitWithDefaultTimeout()
Expect(gen).Should(Exit(0))
// Remove the pod so it can be recreated via kube play
rm := podmanTest.Podman([]string{"pod", "rm", "-f", podName})
rm.WaitWithDefaultTimeout()
Expect(rm).Should(Exit(0))
kube := podmanTest.Podman([]string{"kube", "play", outputFile})
kube.WaitWithDefaultTimeout()
Expect(kube).Should(Exit(0))
// Expect the number of containers created to be 2, infra, and regular container
numOfCtrs := podmanTest.NumberOfContainers()
Expect(numOfCtrs).To(Equal(2))
ps := podmanTest.Podman([]string{"ps", "--format", "{{.Names}}"})
ps.WaitWithDefaultTimeout()
Expect(ps).Should(Exit(0))
Expect(ps.OutputToString()).To(ContainSubstring(infraName))
})
// podman play with default infra name
It("podman play kube test with default infra name", func() {
podName := "mypod"
outputFile := filepath.Join(podmanTest.TempDir, "pod.yaml")
pod := podmanTest.Podman([]string{"pod", "create", podName})
pod.WaitWithDefaultTimeout()
Expect(pod).Should(Exit(0))
ctr := podmanTest.Podman([]string{"create", "--pod", podName, ALPINE, "top"})
ctr.WaitWithDefaultTimeout()
Expect(ctr).Should(Exit(0))
// Generate kube yaml and it should have the infra name annotation set
gen := podmanTest.Podman([]string{"kube", "generate", "-f", outputFile, podName})
gen.WaitWithDefaultTimeout()
Expect(gen).Should(Exit(0))
// Remove the pod so it can be recreated via kube play
rm := podmanTest.Podman([]string{"pod", "rm", "-f", podName})
rm.WaitWithDefaultTimeout()
Expect(rm).Should(Exit(0))
kube := podmanTest.Podman([]string{"play", "kube", outputFile})
kube.WaitWithDefaultTimeout()
Expect(kube).Should(Exit(0))
// Expect the number of containers created to be 2, infra, and regular container
numOfCtrs := podmanTest.NumberOfContainers()
Expect(numOfCtrs).To(Equal(2))
podPs := podmanTest.Podman([]string{"pod", "ps", "-q"})
podPs.WaitWithDefaultTimeout()
Expect(podPs).Should(Exit(0))
podID := podPs.OutputToString()
ps := podmanTest.Podman([]string{"ps", "--format", "{{.Names}}"})
ps.WaitWithDefaultTimeout()
Expect(ps).Should(Exit(0))
Expect(ps.OutputToString()).To(ContainSubstring(podID[:12] + "-infra"))
})
})