Skip to content

Commit

Permalink
Merge pull request #33 from BlackSmith/initCont2
Browse files Browse the repository at this point in the history
Fix of Config volume is not available in initContainer
  • Loading branch information
coufalja authored Aug 2, 2019
2 parents 75c5edd + 5df14d5 commit 1c91f86
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
28 changes: 19 additions & 9 deletions pkg/inject/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,15 +311,14 @@ func TestRunAndServe(t *testing.T) {
// nolint: lll
validPatch := []byte(`[
{
"op":"add",
"path":"/spec/initContainers/0",
"value":{
"name":"config-init",
"image":"wanderadock/scccmd",
"args":["get","values","--source","http://config-service.default.svc:8080","--application","c1","--profile","default","--label","master","--destination","config.yaml"],
"resources":{"limits":{"cpu":"50m","memory":"50M"},"requests":{"cpu":"10m","memory":"10M"}},
"volumeMounts":[{"name":"config-volume","mountPath":"/config"}]
}
"op": "add",
"path": "/spec/initContainers/0/volumeMounts",
"value": [
{
"name": "config-volume",
"mountPath": "/config"
}
]
},
{
"op":"add",
Expand Down Expand Up @@ -349,6 +348,17 @@ func TestRunAndServe(t *testing.T) {
"emptyDir":{}
}
},
{
"op":"add",
"path":"/spec/initContainers/0",
"value":{
"name":"config-init",
"image":"wanderadock/scccmd",
"args":["get","values","--source","http://config-service.default.svc:8080","--application","c1","--profile","default","--label","master","--destination","config.yaml"],
"resources":{"limits":{"cpu":"50m","memory":"50M"},"requests":{"cpu":"10m","memory":"10M"}},
"volumeMounts":[{"name":"config-volume","mountPath":"/config"}]
}
},
{
"op":"add",
"path":"/metadata/annotations/config.scccmd.github.com~1status",
Expand Down
9 changes: 5 additions & 4 deletions pkg/inject/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ func addVolumeMount(target, added []corev1.VolumeMount, basePath string) (patch
return patch
}

func addAllVolumeMounts(containers []corev1.Container, volumeMounts []corev1.VolumeMount) []rfc6902PatchOperation {
func addAllVolumeMounts(containers []corev1.Container, volumeMounts []corev1.VolumeMount, path string) []rfc6902PatchOperation {
var patch []rfc6902PatchOperation
for i, container := range containers {
patch = append(patch, addVolumeMount(container.VolumeMounts, volumeMounts, fmt.Sprintf("/spec/containers/%d/volumeMounts", i))...)
patch = append(patch, addVolumeMount(container.VolumeMounts, volumeMounts, fmt.Sprintf(path, i))...)
}
return patch
}
Expand Down Expand Up @@ -184,9 +184,10 @@ func createPatch(pod *corev1.Pod, prevStatus *SidecarInjectionStatus, annotation
patch = append(patch, removeAllVolumeMounts(pod.Spec.Containers, prevStatus.VolumeMounts)...)
patch = append(patch, removeVolumes(pod.Spec.Volumes, prevStatus.Volumes, "/spec/volumes")...)

patch = append(patch, insertContainer(pod.Spec.InitContainers, sic.InitContainers, "/spec/initContainers", "0")...)
patch = append(patch, addAllVolumeMounts(pod.Spec.Containers, sic.VolumeMounts)...)
patch = append(patch, addAllVolumeMounts(pod.Spec.InitContainers, sic.VolumeMounts, "/spec/initContainers/%d/volumeMounts")...)
patch = append(patch, addAllVolumeMounts(pod.Spec.Containers, sic.VolumeMounts, "/spec/containers/%d/volumeMounts")...)
patch = append(patch, addVolume(pod.Spec.Volumes, sic.Volumes, "/spec/volumes")...)
patch = append(patch, insertContainer(pod.Spec.InitContainers, sic.InitContainers, "/spec/initContainers", "0")...)

patch = append(patch, updateAnnotation(pod.Annotations, annotations)...)

Expand Down

0 comments on commit 1c91f86

Please sign in to comment.