Update init ctr default for play kube

Update the init container type default to once instead
of always to match k8s behavior.
Add a new annotation that can be used to change the init
ctr type in the kube yaml.

Signed-off-by: Urvashi Mohnani <umohnani@redhat.com>
This commit is contained in:
Urvashi Mohnani
2022-07-19 11:58:27 -04:00
committed by Matthew Heon
parent 6057db75d8
commit c3c07ed09e
4 changed files with 38 additions and 4 deletions

View File

@ -22,7 +22,7 @@ Currently, the supported Kubernetes kinds are:
Only two volume types are supported by play kube, the *hostPath* and *persistentVolumeClaim* volume types. For the *hostPath* volume type, only the *default (empty)*, *DirectoryOrCreate*, *Directory*, *FileOrCreate*, *File*, *Socket*, *CharDevice* and *BlockDevice* subtypes are supported. Podman interprets the value of *hostPath* *path* as a file path when it contains at least one forward slash, otherwise Podman treats the value as the name of a named volume. When using a *persistentVolumeClaim*, the value for *claimName* is the name for the Podman named volume. Only two volume types are supported by play kube, the *hostPath* and *persistentVolumeClaim* volume types. For the *hostPath* volume type, only the *default (empty)*, *DirectoryOrCreate*, *Directory*, *FileOrCreate*, *File*, *Socket*, *CharDevice* and *BlockDevice* subtypes are supported. Podman interprets the value of *hostPath* *path* as a file path when it contains at least one forward slash, otherwise Podman treats the value as the name of a named volume. When using a *persistentVolumeClaim*, the value for *claimName* is the name for the Podman named volume.
Note: When playing a kube YAML with init containers, the init container will be created with init type value `always`. Note: When playing a kube YAML with init containers, the init container will be created with init type value `once`. To change the default type, use the `io.podman.annotations.init.container.type` annotation to set the type to `always`.
Note: *hostPath* volume types created by play kube will be given an SELinux shared label (z), bind mounts are not relabeled (use `chcon -t container_file_t -R <directory>`). Note: *hostPath* volume types created by play kube will be given an SELinux shared label (z), bind mounts are not relabeled (use `chcon -t container_file_t -R <directory>`).

View File

@ -135,6 +135,11 @@ const (
// creating a checkpoint image to specify the name of host distribution on // creating a checkpoint image to specify the name of host distribution on
// which the checkpoint was created. // which the checkpoint was created.
CheckpointAnnotationDistributionName = "io.podman.annotations.checkpoint.distribution.name" CheckpointAnnotationDistributionName = "io.podman.annotations.checkpoint.distribution.name"
// InitContainerType is used by play kube when playing a kube yaml to specify the type
// of the init container.
InitContainerType = "io.podman.annotations.init.container.type"
// MaxKubeAnnotation is the max length of annotations allowed by Kubernetes. // MaxKubeAnnotation is the max length of annotations allowed by Kubernetes.
MaxKubeAnnotation = 63 MaxKubeAnnotation = 63
) )

View File

@ -507,13 +507,17 @@ func (ic *ContainerEngine) playKubePod(ctx context.Context, podName string, podY
for k, v := range podSpec.PodSpecGen.Labels { // add podYAML labels for k, v := range podSpec.PodSpecGen.Labels { // add podYAML labels
labels[k] = v labels[k] = v
} }
initCtrType := annotations[define.InitContainerType]
if initCtrType == "" {
initCtrType = define.OneShotInitContainer
}
specgenOpts := kube.CtrSpecGenOptions{ specgenOpts := kube.CtrSpecGenOptions{
Annotations: annotations, Annotations: annotations,
ConfigMaps: configMaps, ConfigMaps: configMaps,
Container: initCtr, Container: initCtr,
Image: pulledImage, Image: pulledImage,
InitContainerType: define.AlwaysInitContainer, InitContainerType: initCtrType,
Labels: labels, Labels: labels,
LogDriver: options.LogDriver, LogDriver: options.LogDriver,
LogOptions: options.LogOptions, LogOptions: options.LogOptions,

View File

@ -1559,8 +1559,10 @@ var _ = Describe("Podman play kube", func() {
}) })
// If you have an init container in the pod yaml, podman should create and run the init container with play kube // If you have an init container in the pod yaml, podman should create and run the init container with play kube
It("podman play kube test with init containers", func() { // With annotation set to always
pod := getPod(withPodInitCtr(getCtr(withImage(ALPINE), withCmd([]string{"echo", "hello"}), withInitCtr(), withName("init-test"))), withCtr(getCtr(withImage(ALPINE), withCmd([]string{"top"})))) It("podman play kube test with init containers and annotation set", func() {
// With the init container type annotation set to always
pod := getPod(withAnnotation("io.podman.annotations.init.container.type", "always"), withPodInitCtr(getCtr(withImage(ALPINE), withCmd([]string{"echo", "hello"}), withInitCtr(), withName("init-test"))), withCtr(getCtr(withImage(ALPINE), withCmd([]string{"top"}))))
err := generateKubeYaml("pod", pod, kubeYaml) err := generateKubeYaml("pod", pod, kubeYaml)
Expect(err).To(BeNil()) Expect(err).To(BeNil())
@ -1585,6 +1587,29 @@ var _ = Describe("Podman play kube", func() {
Expect(inspect.OutputToString()).To(ContainSubstring("running")) Expect(inspect.OutputToString()).To(ContainSubstring("running"))
}) })
// If you have an init container in the pod yaml, podman should create and run the init container with play kube
// Using default init container type (once)
It("podman play kube test with init container type set to default value", func() {
// Using the default init container type (once)
pod := getPod(withPodInitCtr(getCtr(withImage(ALPINE), withCmd([]string{"echo", "hello"}), withInitCtr(), withName("init-test"))), withCtr(getCtr(withImage(ALPINE), withCmd([]string{"top"}))))
err := generateKubeYaml("pod", pod, kubeYaml)
Expect(err).To(BeNil())
kube := podmanTest.Podman([]string{"play", "kube", kubeYaml})
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))
// Regular container should be in running state
inspect := podmanTest.Podman([]string{"inspect", "--format", "{{.State.Status}}", "testPod-" + defaultCtrName})
inspect.WaitWithDefaultTimeout()
Expect(inspect).Should(Exit(0))
Expect(inspect.OutputToString()).To(ContainSubstring("running"))
})
// If you supply only args for a Container, the default Entrypoint defined in the Docker image is run with the args that you supplied. // If you supply only args for a Container, the default Entrypoint defined in the Docker image is run with the args that you supplied.
It("podman play kube test correct command with only set args in yaml file", func() { It("podman play kube test correct command with only set args in yaml file", func() {
pod := getPod(withCtr(getCtr(withImage(REGISTRY_IMAGE), withCmd(nil), withArg([]string{"echo", "hello"})))) pod := getPod(withCtr(getCtr(withImage(REGISTRY_IMAGE), withCmd(nil), withArg([]string{"echo", "hello"}))))