From 1aff21ce6c3c9555bc5bd0eb9a32a6748d068c06 Mon Sep 17 00:00:00 2001 From: Martin Korbel Date: Thu, 1 Aug 2019 13:37:00 +0200 Subject: [PATCH 1/2] This closes #30 --- pkg/inject/hook_test.go | 28 +++++++++++++++++++--------- pkg/inject/patch.go | 13 +++++++++---- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/pkg/inject/hook_test.go b/pkg/inject/hook_test.go index 0b49a77..237b72d 100644 --- a/pkg/inject/hook_test.go +++ b/pkg/inject/hook_test.go @@ -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", @@ -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", diff --git a/pkg/inject/patch.go b/pkg/inject/patch.go index 47e2254..d37490a 100644 --- a/pkg/inject/patch.go +++ b/pkg/inject/patch.go @@ -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 } @@ -184,11 +184,16 @@ 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)...) + res, _ := json.Marshal(patch) + + fmt.Println(fmt.Sprintf("%s", res)) + return json.Marshal(patch) } From 5df14d5d7e10fc282edf51b8fc1ece62e80a2df6 Mon Sep 17 00:00:00 2001 From: Martin Korbel Date: Fri, 2 Aug 2019 13:35:08 +0200 Subject: [PATCH 2/2] removing of debugging print --- pkg/inject/patch.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pkg/inject/patch.go b/pkg/inject/patch.go index d37490a..814521d 100644 --- a/pkg/inject/patch.go +++ b/pkg/inject/patch.go @@ -191,9 +191,5 @@ func createPatch(pod *corev1.Pod, prevStatus *SidecarInjectionStatus, annotation patch = append(patch, updateAnnotation(pod.Annotations, annotations)...) - res, _ := json.Marshal(patch) - - fmt.Println(fmt.Sprintf("%s", res)) - return json.Marshal(patch) }