Skip to content

Commit

Permalink
feat: annotate image mutation (#2755)
Browse files Browse the repository at this point in the history
Signed-off-by: Austin Abro <[email protected]>
Signed-off-by: Philip Laine <[email protected]>
Co-authored-by: Philip Laine <[email protected]>
  • Loading branch information
AustinAbro321 and phillebaba authored Jul 25, 2024
1 parent b964467 commit 5058a67
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/internal/agent/hooks/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
corev1 "k8s.io/api/core/v1"
)

const annotationPrefix = "zarf.dev"

// NewPodMutationHook creates a new instance of pods mutation hook.
func NewPodMutationHook(ctx context.Context, cluster *cluster.Cluster) operations.Hook {
return operations.Hook{
Expand All @@ -40,6 +42,10 @@ func parsePod(object []byte) (*corev1.Pod, error) {
return &pod, nil
}

func getImageAnnotationKey(containerName string) string {
return fmt.Sprintf("%s/original-image-%s", annotationPrefix, containerName)
}

func mutatePod(ctx context.Context, r *v1.AdmissionRequest, cluster *cluster.Cluster) (*operations.Result, error) {
pod, err := parsePod(r.Object.Raw)
if err != nil {
Expand All @@ -66,6 +72,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 +85,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[getImageAnnotationKey(container.Name)] = container.Image
patches = append(patches, operations.ReplacePatchOperation(path, replacement))
}

Expand All @@ -85,6 +97,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[getImageAnnotationKey(container.Name)] = container.Image
patches = append(patches, operations.ReplacePatchOperation(path, replacement))
}

Expand All @@ -96,11 +109,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[getImageAnnotationKey(container.Name)] = 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
22 changes: 19 additions & 3 deletions src/internal/agent/hooks/pods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ func TestPodMutationWebhook(t *testing.T) {
Annotations: map[string]string{"should-be": "mutated"},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{{Image: "nginx"}},
InitContainers: []corev1.Container{{Image: "busybox"}},
Containers: []corev1.Container{{Name: "nginx", Image: "nginx"}},
InitContainers: []corev1.Container{{Name: "different", Image: "busybox"}},
EphemeralContainers: []corev1.EphemeralContainer{
{
EphemeralContainerCommon: corev1.EphemeralContainerCommon{
Name: "alpine",
Image: "alpine",
},
},
Expand Down Expand Up @@ -85,6 +86,15 @@ func TestPodMutationWebhook(t *testing.T) {
"should-be": "mutated",
},
),
operations.ReplacePatchOperation(
"/metadata/annotations",
map[string]string{
"zarf.dev/original-image-nginx": "nginx",
"zarf.dev/original-image-alpine": "alpine",
"zarf.dev/original-image-different": "busybox",
"should-be": "mutated",
},
),
},
code: http.StatusOK,
},
Expand All @@ -108,7 +118,7 @@ func TestPodMutationWebhook(t *testing.T) {
Labels: nil,
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{{Image: "nginx"}},
Containers: []corev1.Container{{Name: "nginx", Image: "nginx"}},
},
}),
patch: []operations.PatchOperation{
Expand All @@ -124,6 +134,12 @@ func TestPodMutationWebhook(t *testing.T) {
"/metadata/labels",
map[string]string{"zarf-agent": "patched"},
),
operations.ReplacePatchOperation(
"/metadata/annotations",
map[string]string{
"zarf.dev/original-image-nginx": "nginx",
},
),
},
code: http.StatusOK,
},
Expand Down

0 comments on commit 5058a67

Please sign in to comment.