Skip to content

Commit

Permalink
fix: merge init container definition of pod manifest template with ou…
Browse files Browse the repository at this point in the history
…r init container
  • Loading branch information
pascalbreuninger committed Jul 23, 2024
1 parent b83a4c3 commit ae4c9d5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 22 deletions.
64 changes: 50 additions & 14 deletions pkg/kubernetes/init_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,21 @@ import (
corev1 "k8s.io/api/core/v1"
)

func (k *KubernetesDriver) getInitContainer(options *driver.RunOptions) ([]corev1.Container, error) {
commands := []string{}
func (k *KubernetesDriver) getInitContainers(options *driver.RunOptions, pod *corev1.Pod, initialize bool) ([]corev1.Container, error) {
if !initialize {
retContainers := []corev1.Container{}
// don't build init container and clean up existing one if defined
for _, container := range pod.Spec.InitContainers {
if container.Name == InitContainerName {
continue
}
retContainers = append(retContainers, container)
}

return retContainers, nil
}

commands := []string{}
// find the volume type mounts
volumeMounts := []corev1.VolumeMount{}
for idx, mount := range options.Mounts {
Expand All @@ -25,9 +37,20 @@ func (k *KubernetesDriver) getInitContainer(options *driver.RunOptions) ([]corev
commands = append(commands, fmt.Sprintf(`cp -a %s/. %s/ || true`, strings.TrimRight(copyFrom, "/"), strings.TrimRight(volumeMount.MountPath, "/")))
}

retContainers := []corev1.Container{}
// merge with existing init container if it exists
var existingInitContainer *corev1.Container
for i, container := range pod.Spec.InitContainers {
if container.Name == InitContainerName {
existingInitContainer = &pod.Spec.InitContainers[i]
} else {
retContainers = append(retContainers, container)
}
}

// check if there is at least one mount
if len(volumeMounts) == 0 {
return nil, nil
return retContainers, nil
}

securityContext := &corev1.SecurityContext{
Expand All @@ -39,16 +62,29 @@ func (k *KubernetesDriver) getInitContainer(options *driver.RunOptions) ([]corev
securityContext = nil
}

return []corev1.Container{
{
Name: "devpod-init",
Image: options.Image,
Command: []string{"sh"},
Args: []string{"-c", strings.Join(commands, "\n") + "\n"},
Resources: parseResources(k.options.HelperResources, k.Log),
VolumeMounts: volumeMounts,
SecurityContext: securityContext,
},
}, nil
initContainer := corev1.Container{
Name: InitContainerName,
Image: options.Image,
Command: []string{"sh"},
Args: []string{"-c", strings.Join(commands, "\n") + "\n"},
Resources: parseResources(k.options.HelperResources, k.Log),
VolumeMounts: volumeMounts,
SecurityContext: securityContext,
}

if existingInitContainer != nil {
initContainer.Env = append(existingInitContainer.Env, initContainer.Env...)
initContainer.EnvFrom = existingInitContainer.EnvFrom
initContainer.Ports = existingInitContainer.Ports
initContainer.VolumeMounts = append(existingInitContainer.VolumeMounts, initContainer.VolumeMounts...)
initContainer.ImagePullPolicy = existingInitContainer.ImagePullPolicy

if initContainer.SecurityContext == nil && existingInitContainer.SecurityContext != nil {
initContainer.SecurityContext = existingInitContainer.SecurityContext
}
}
retContainers = append(retContainers, initContainer)

return retContainers, nil

}
14 changes: 6 additions & 8 deletions pkg/kubernetes/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
)

const DevContainerName = "devpod"
const InitContainerName = "devpod-init"

const (
DevPodCreatedLabel = "devpod.sh/created"
Expand Down Expand Up @@ -127,13 +128,10 @@ func (k *KubernetesDriver) runContainer(
}
}

// get init container
var initContainer []corev1.Container
if initialize {
initContainer, err = k.getInitContainer(options)
if err != nil {
return errors.Wrap(err, "build init container")
}
// get init containers
initContainers, err := k.getInitContainers(options, pod, initialize)
if err != nil {
return errors.Wrap(err, "build init container")
}

// loop over volume mounts
Expand Down Expand Up @@ -214,7 +212,7 @@ func (k *KubernetesDriver) runContainer(

pod.Spec.ServiceAccountName = serviceAccount
pod.Spec.NodeSelector = nodeSelector
pod.Spec.InitContainers = append(initContainer, pod.Spec.InitContainers...)
pod.Spec.InitContainers = initContainers
pod.Spec.Containers = getContainers(pod, options.Image, options.Entrypoint, options.Cmd, envVars, volumeMounts, capabilities, resources, options.Privileged, k.options.DangerouslyOverrideImage, k.options.StrictSecurity)
pod.Spec.Volumes = getVolumes(pod, id)
pod.Spec.RestartPolicy = corev1.RestartPolicyNever
Expand Down

0 comments on commit ae4c9d5

Please sign in to comment.