Skip to content

Commit

Permalink
annoate pods after mutation
Browse files Browse the repository at this point in the history
Signed-off-by: Austin Abro <AustinAbro321@gmail.com>
  • Loading branch information
AustinAbro321 committed Jul 24, 2024
1 parent 05ef439 commit c4c9cc7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/internal/agent/hooks/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ func mutatePod(ctx context.Context, r *v1.AdmissionRequest, cluster *cluster.Clu
zarfSecret := []corev1.LocalObjectReference{{Name: config.ZarfImagePullSecretName}}
patches = append(patches, operations.ReplacePatchOperation("/spec/imagePullSecrets", zarfSecret))

updatedAnnotations := pod.Annotations
if updatedAnnotations == nil {
updatedAnnotations = make(map[string]string)
}

// update the image host for each init container
for idx, container := range pod.Spec.InitContainers {
path := fmt.Sprintf("/spec/initContainers/%d/image", idx)
Expand All @@ -74,6 +79,7 @@ func mutatePod(ctx context.Context, r *v1.AdmissionRequest, cluster *cluster.Clu
message.Warnf(lang.AgentErrImageSwap, container.Image)
continue // Continue, because we might as well attempt to mutate the other containers for this pod
}
updatedAnnotations[fmt.Sprintf("zarf.dev/original-init-image-%d", idx)] = container.Image
patches = append(patches, operations.ReplacePatchOperation(path, replacement))
}

Expand All @@ -85,6 +91,7 @@ func mutatePod(ctx context.Context, r *v1.AdmissionRequest, cluster *cluster.Clu
message.Warnf(lang.AgentErrImageSwap, container.Image)
continue // Continue, because we might as well attempt to mutate the other containers for this pod
}
updatedAnnotations[fmt.Sprintf("zarf.dev/original-ephemeral-image-%d", idx)] = container.Image
patches = append(patches, operations.ReplacePatchOperation(path, replacement))
}

Expand All @@ -96,11 +103,14 @@ func mutatePod(ctx context.Context, r *v1.AdmissionRequest, cluster *cluster.Clu
message.Warnf(lang.AgentErrImageSwap, container.Image)
continue // Continue, because we might as well attempt to mutate the other containers for this pod
}
updatedAnnotations[fmt.Sprintf("zarf.dev/original-container-image-%d", idx)] = container.Image
patches = append(patches, operations.ReplacePatchOperation(path, replacement))
}

patches = append(patches, getLabelPatch(pod.Labels))

patches = append(patches, operations.ReplacePatchOperation("/metadata/annotations", updatedAnnotations))

return &operations.Result{
Allowed: true,
PatchOps: patches,
Expand Down
15 changes: 15 additions & 0 deletions src/internal/agent/hooks/pods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ func TestPodMutationWebhook(t *testing.T) {
"should-be": "mutated",
},
),
operations.ReplacePatchOperation(
"/metadata/annotations",
map[string]string{
"zarf.dev/original-container-image-0": "nginx",
"zarf.dev/original-ephemeral-image-0": "alpine",
"zarf.dev/original-init-image-0": "busybox",
"should-be": "mutated",
},
),
},
code: http.StatusOK,
},
Expand Down Expand Up @@ -124,6 +133,12 @@ func TestPodMutationWebhook(t *testing.T) {
"/metadata/labels",
map[string]string{"zarf-agent": "patched"},
),
operations.ReplacePatchOperation(
"/metadata/annotations",
map[string]string{
"zarf.dev/original-container-image-0": "nginx",
},
),
},
code: http.StatusOK,
},
Expand Down

0 comments on commit c4c9cc7

Please sign in to comment.