Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(agent): missing path for pod without labels #2518

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/internal/agent/hooks/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 map[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,
Expand Down
35 changes: 35 additions & 0 deletions src/test/e2e/37_pod_without_labels_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
13 changes: 13 additions & 0 deletions src/test/packages/37-pod-without-labels/pod.yaml
Original file line number Diff line number Diff line change
@@ -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: {}
Loading