Skip to content

Commit

Permalink
Merge pull request #32 from h0n9/feature/31-mount-volume-to-init-cont…
Browse files Browse the repository at this point in the history
…ainers

Mount volume to every init containers
  • Loading branch information
h0n9 authored Jun 4, 2024
2 parents 200454a + 223fabe commit d746ffa
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions webhook/mutator.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,28 @@ func (mutator *Mutator) Handle(ctx context.Context, req admission.Request) admis
VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}},
})

// prepare mount path for general use
mountPath := output
subPath := filepath.Base(mountPath)

// mount volume to every init containers
for i := range pod.Spec.InitContainers {
pod.Spec.InitContainers[i].VolumeMounts = append(pod.Spec.InitContainers[i].VolumeMounts, corev1.VolumeMount{
Name: injectorName,
MountPath: mountPath,
SubPath: subPath,
})
}

// mount volume to every containers
for i := range pod.Spec.Containers {
pod.Spec.Containers[i].VolumeMounts = append(pod.Spec.Containers[i].VolumeMounts, corev1.VolumeMount{
Name: injectorName,
MountPath: mountPath,
SubPath: subPath,
})
}

// create init container for injection
initContainer := corev1.Container{
Name: injectorName,
Expand All @@ -79,7 +101,7 @@ func (mutator *Mutator) Handle(ctx context.Context, req admission.Request) admis
fmt.Sprintf("--provider=%s", providerStr),
fmt.Sprintf("--secret-id=%s", secretID),
fmt.Sprintf("--template=%s", util.EncodeBase64(tmplStr)),
fmt.Sprintf("--output=%s", filepath.Join(csm.InjectorVolumeMountPath, filepath.Base(output))),
fmt.Sprintf("--output=%s", filepath.Join(csm.InjectorVolumeMountPath, subPath)),
},
VolumeMounts: []corev1.VolumeMount{
{
Expand All @@ -92,15 +114,6 @@ func (mutator *Mutator) Handle(ctx context.Context, req admission.Request) admis
// append init container to pod's init containers
pod.Spec.InitContainers = append([]corev1.Container{initContainer}, pod.Spec.InitContainers...)

// mount volume to every containers
for i := range pod.Spec.Containers {
pod.Spec.Containers[i].VolumeMounts = append(pod.Spec.Containers[i].VolumeMounts, corev1.VolumeMount{
Name: injectorName,
MountPath: output,
SubPath: filepath.Base(output),
})
}

// set annotation for injection to true
injected := "injected"
if secretName != "" {
Expand Down

0 comments on commit d746ffa

Please sign in to comment.