From 5129da5861d587dad1fbb366ec48fa71b66a5456 Mon Sep 17 00:00:00 2001 From: Javier Cortejoso Date: Mon, 23 Oct 2023 11:38:33 +0200 Subject: [PATCH] Modify image in annotation to skip policyController modification --- main.go | 8 +++++++- main_test.go | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 888c5f5..07a3775 100644 --- a/main.go +++ b/main.go @@ -147,7 +147,7 @@ func patchPodSpec(podSpec *v1.PodSpec, createAnnotationField bool) ([]byte, erro patch = append(patch, map[string]interface{}{ "op": "add", "path": "/metadata/annotations/image-" + sanitize(container.Name), - "value": container.Image, + "value": renameImage(container.Image), }) } @@ -157,3 +157,9 @@ func patchPodSpec(podSpec *v1.PodSpec, createAnnotationField bool) ([]byte, erro func sanitize(name string) string { return strings.Replace(name, "-", "_", -1) } + +// Rename image name to be used as annotation key +// The reason is that PolicyController also replaces image refereces in annotations :( +func renameImage(name string) string { + return strings.Replace(name, ":", ";", -1) +} diff --git a/main_test.go b/main_test.go index 779c18d..a0afda8 100644 --- a/main_test.go +++ b/main_test.go @@ -23,7 +23,7 @@ func TestPatchPodSpec(t *testing.T) { }, } - expectedPatch := `[{"op":"add","path":"/metadata/annotations/image-container_1","value":"nginx:1.14"},{"op":"add","path":"/metadata/annotations/image-init_container_1","value":"busybox:1.28"}]` + expectedPatch := `[{"op":"add","path":"/metadata/annotations/image-container_1","value":"nginx;1.14"},{"op":"add","path":"/metadata/annotations/image-init_container_1","value":"busybox;1.28"}]` patch, err := patchPodSpec(podSpec, false) if err != nil {