summaryrefslogtreecommitdiff
path: root/libpod
diff options
context:
space:
mode:
authorUrvashi Mohnani <umohnani@redhat.com>2021-08-25 12:37:51 -0400
committerUrvashi Mohnani <umohnani@redhat.com>2021-09-10 09:37:46 -0400
commitf5e4ffb5e46be03a81b4425d3fe080543fca7035 (patch)
tree979f0e8aee5aebaa8b41cf0620d311381e6a9f20 /libpod
parent580ac4c6abc336d984f3a09940a4ef3006f0e6a7 (diff)
downloadpodman-f5e4ffb5e46be03a81b4425d3fe080543fca7035.tar.gz
podman-f5e4ffb5e46be03a81b4425d3fe080543fca7035.tar.bz2
podman-f5e4ffb5e46be03a81b4425d3fe080543fca7035.zip
Add init containers to generate and play kube
Kubernetes has a concept of init containers that run and exit before the regular containers in a pod are started. We added init containers to podman pods as well. This patch adds support for generating init containers in the kube yaml when a pod we are converting had init containers. When playing a kube yaml, it detects an init container and creates such a container in podman accordingly. Note, only init containers created with the init type set to "always" will be generated as the "once" option deletes the init container after it has run and exited. Play kube will always creates init containers with the "always" init container type. Signed-off-by: Urvashi Mohnani <umohnani@redhat.com>
Diffstat (limited to 'libpod')
-rw-r--r--libpod/container.go5
-rw-r--r--libpod/kube.go33
2 files changed, 33 insertions, 5 deletions
diff --git a/libpod/container.go b/libpod/container.go
index 0986a0d80..a4bbb5dd0 100644
--- a/libpod/container.go
+++ b/libpod/container.go
@@ -1062,6 +1062,11 @@ func (c *Container) IsInfra() bool {
return c.config.IsInfra
}
+// IsInitCtr returns whether the container is an init container
+func (c *Container) IsInitCtr() bool {
+ return len(c.config.InitContainerType) > 0
+}
+
// IsReadOnly returns whether the container is running in read only mode
func (c *Container) IsReadOnly() bool {
return c.config.Spec.Root.Readonly
diff --git a/libpod/kube.go b/libpod/kube.go
index fff040adb..812bb101b 100644
--- a/libpod/kube.go
+++ b/libpod/kube.go
@@ -4,6 +4,7 @@ import (
"fmt"
"math/rand"
"os"
+ "sort"
"strconv"
"strings"
"time"
@@ -220,8 +221,14 @@ func (p *Pod) podWithContainers(containers []*Container, ports []v1.ContainerPor
deDupPodVolumes := make(map[string]*v1.Volume)
first := true
podContainers := make([]v1.Container, 0, len(containers))
+ podInitCtrs := []v1.Container{}
podAnnotations := make(map[string]string)
dnsInfo := v1.PodDNSConfig{}
+
+ // Let's sort the containers in order of created time
+ // This will ensure that the init containers are defined in the correct order in the kube yaml
+ sort.Slice(containers, func(i, j int) bool { return containers[i].CreatedTime().Before(containers[j].CreatedTime()) })
+
for _, ctr := range containers {
if !ctr.IsInfra() {
// Convert auto-update labels into kube annotations
@@ -229,6 +236,8 @@ func (p *Pod) podWithContainers(containers []*Container, ports []v1.ContainerPor
podAnnotations[k] = v
}
+ isInit := ctr.IsInitCtr()
+
ctr, volumes, _, err := containerToV1Container(ctr)
if err != nil {
return nil, err
@@ -245,6 +254,10 @@ func (p *Pod) podWithContainers(containers []*Container, ports []v1.ContainerPor
ctr.Ports = ports
first = false
}
+ if isInit {
+ podInitCtrs = append(podInitCtrs, ctr)
+ continue
+ }
podContainers = append(podContainers, ctr)
// Deduplicate volumes, so if containers in the pod share a volume, it's only
// listed in the volumes section once
@@ -278,13 +291,14 @@ func (p *Pod) podWithContainers(containers []*Container, ports []v1.ContainerPor
return newPodObject(
p.Name(),
podAnnotations,
+ podInitCtrs,
podContainers,
podVolumes,
&dnsInfo,
hostNetwork), nil
}
-func newPodObject(podName string, annotations map[string]string, containers []v1.Container, volumes []v1.Volume, dnsOptions *v1.PodDNSConfig, hostNetwork bool) *v1.Pod {
+func newPodObject(podName string, annotations map[string]string, initCtrs, containers []v1.Container, volumes []v1.Volume, dnsOptions *v1.PodDNSConfig, hostNetwork bool) *v1.Pod {
tm := v12.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
@@ -304,9 +318,10 @@ func newPodObject(podName string, annotations map[string]string, containers []v1
Annotations: annotations,
}
ps := v1.PodSpec{
- Containers: containers,
- Volumes: volumes,
- HostNetwork: hostNetwork,
+ Containers: containers,
+ HostNetwork: hostNetwork,
+ InitContainers: initCtrs,
+ Volumes: volumes,
}
if dnsOptions != nil {
ps.DNSConfig = dnsOptions
@@ -323,6 +338,7 @@ func newPodObject(podName string, annotations map[string]string, containers []v1
// for a single container. we "insert" that container description in a pod.
func simplePodWithV1Containers(ctrs []*Container) (*v1.Pod, error) {
kubeCtrs := make([]v1.Container, 0, len(ctrs))
+ kubeInitCtrs := []v1.Container{}
kubeVolumes := make([]v1.Volume, 0)
hostNetwork := true
podDNS := v1.PodDNSConfig{}
@@ -333,6 +349,8 @@ func simplePodWithV1Containers(ctrs []*Container) (*v1.Pod, error) {
kubeAnnotations[k] = v
}
+ isInit := ctr.IsInitCtr()
+
if !ctr.HostNetwork() {
hostNetwork = false
}
@@ -340,7 +358,11 @@ func simplePodWithV1Containers(ctrs []*Container) (*v1.Pod, error) {
if err != nil {
return nil, err
}
- kubeCtrs = append(kubeCtrs, kubeCtr)
+ if isInit {
+ kubeInitCtrs = append(kubeInitCtrs, kubeCtr)
+ } else {
+ kubeCtrs = append(kubeCtrs, kubeCtr)
+ }
kubeVolumes = append(kubeVolumes, kubeVols...)
// Combine DNS information in sum'd structure
@@ -379,6 +401,7 @@ func simplePodWithV1Containers(ctrs []*Container) (*v1.Pod, error) {
return newPodObject(
strings.ReplaceAll(ctrs[0].Name(), "_", ""),
kubeAnnotations,
+ kubeInitCtrs,
kubeCtrs,
kubeVolumes,
&podDNS,