From a0d56b35cd025ffaa2f793091815c2bccf9c3908 Mon Sep 17 00:00:00 2001 From: Brandt Keller Date: Fri, 17 May 2024 00:28:19 +0000 Subject: [PATCH 1/3] fix(agent): missing path for pod without labels --- src/internal/agent/hooks/pods.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/internal/agent/hooks/pods.go b/src/internal/agent/hooks/pods.go index 2e90709bfd..3ee8f8e6ee 100644 --- a/src/internal/agent/hooks/pods.go +++ b/src/internal/agent/hooks/pods.go @@ -99,7 +99,15 @@ func mutatePod(r *v1.AdmissionRequest) (*operations.Result, error) { } // Add a label noting the zarf mutation - patchOperations = append(patchOperations, operations.ReplacePatchOperation("/metadata/labels/zarf-agent", "patched")) + if pod.Labels == nil { + // If the labels path does not exist - create with nap[string]string value + patchOperations = append(patchOperations, operations.AddPatchOperation("/metadata/labels", + map[string]string{ + "zarf-agent": "patched", + })) + } else { + patchOperations = append(patchOperations, operations.ReplacePatchOperation("/metadata/labels/zarf-agent", "patched")) + } return &operations.Result{ Allowed: true, From dafe6efdce74a1c98178243bd3d6238ceba95d2a Mon Sep 17 00:00:00 2001 From: Brandt Keller Date: Fri, 17 May 2024 16:19:51 +0000 Subject: [PATCH 2/3] fix(agent): Add test for pod without labels --- src/test/e2e/37_pod_without_labels_test.go | 35 +++++++++++++++++++ .../packages/37-pod-without-labels/pod.yaml | 13 +++++++ 2 files changed, 48 insertions(+) create mode 100644 src/test/e2e/37_pod_without_labels_test.go create mode 100644 src/test/packages/37-pod-without-labels/pod.yaml diff --git a/src/test/e2e/37_pod_without_labels_test.go b/src/test/e2e/37_pod_without_labels_test.go new file mode 100644 index 0000000000..4d249dd3ee --- /dev/null +++ b/src/test/e2e/37_pod_without_labels_test.go @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2021-Present The Zarf Authors + +// Package test provides e2e tests for Zarf. +package test + +import ( + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestPodWithoutLabels(t *testing.T) { + t.Log("E2E: Pod Without Labels") + e2e.SetupWithCluster(t) + + // Path to pod manifest containing 0 lavbels + buildPath := filepath.Join("src", "test", "packages", "37-pod-without-labels", "pod.yaml") + + // Create the testing namespace + _, _, err := e2e.Kubectl("create", "ns", "pod-label") + require.NoError(t, err) + + // Create the pod without labels + // This is not an image zarf will have in the registry - but the agent was failing to admit on an internal server error before completing admission + _, _, err = e2e.Kubectl("create", "-f", buildPath, "-n", "pod-label") + require.NoError(t, err) + + // Cleanup + _, _, err = e2e.Kubectl("delete", "-f", buildPath, "-n", "pod-label") + require.NoError(t, err) + _, _, err = e2e.Kubectl("delete", "ns", "pod-label") + require.NoError(t, err) +} diff --git a/src/test/packages/37-pod-without-labels/pod.yaml b/src/test/packages/37-pod-without-labels/pod.yaml new file mode 100644 index 0000000000..a1ea2ac32b --- /dev/null +++ b/src/test/packages/37-pod-without-labels/pod.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + creationTimestamp: null + name: test +spec: + containers: + - image: nginx + name: test + resources: {} + dnsPolicy: ClusterFirst + restartPolicy: Always +status: {} \ No newline at end of file From a2070ff512d6786f76cfceee1cefc26d60110e5f Mon Sep 17 00:00:00 2001 From: Brandt Keller Date: Fri, 17 May 2024 16:49:54 +0000 Subject: [PATCH 3/3] fix(agent): comment typo for conditional block --- src/internal/agent/hooks/pods.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/internal/agent/hooks/pods.go b/src/internal/agent/hooks/pods.go index 3ee8f8e6ee..a7b6911991 100644 --- a/src/internal/agent/hooks/pods.go +++ b/src/internal/agent/hooks/pods.go @@ -100,7 +100,7 @@ func mutatePod(r *v1.AdmissionRequest) (*operations.Result, error) { // Add a label noting the zarf mutation if pod.Labels == nil { - // If the labels path does not exist - create with nap[string]string value + // If the labels path does not exist - create with map[string]string value patchOperations = append(patchOperations, operations.AddPatchOperation("/metadata/labels", map[string]string{ "zarf-agent": "patched",