Skip to content

Commit

Permalink
Modify image in annotation to skip policyController modification
Browse files Browse the repository at this point in the history
  • Loading branch information
jcortejoso committed Oct 23, 2023
1 parent d8d5656 commit 5129da5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
})
}

Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 5129da5

Please sign in to comment.