Skip to content

Commit

Permalink
feat: adding labels to all resources mutated by the agent (#2557)
Browse files Browse the repository at this point in the history
## Description

Adds labels to any resource mutated by the agent

## Checklist before merging

- [ ] Test, docs, adr added or updated as needed
- [ ] [Contributor Guide
Steps](https://github.com/defenseunicorns/zarf/blob/main/.github/CONTRIBUTING.md#developer-workflow)
followed
  • Loading branch information
AustinAbro321 committed May 30, 2024
1 parent 9da7406 commit f69e42d
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 21 deletions.
4 changes: 4 additions & 0 deletions src/internal/agent/hooks/argocd-application.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/defenseunicorns/zarf/src/pkg/transform"
"github.com/defenseunicorns/zarf/src/types"
v1 "k8s.io/api/admission/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// Application is a definition of an ArgoCD Application resource.
Expand All @@ -29,6 +30,7 @@ import (
// For more information: https://argo-cd.readthedocs.io/en/stable/user-guide/import/
type Application struct {
Spec ApplicationSpec `json:"spec"`
metav1.ObjectMeta
}

// ApplicationSpec represents desired application state. Contains link to repository with application definition.
Expand Down Expand Up @@ -93,6 +95,8 @@ func mutateApplication(ctx context.Context, r *v1.AdmissionRequest, cluster *clu
}
}

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

return &operations.Result{
Allowed: true,
PatchOps: patches,
Expand Down
6 changes: 6 additions & 0 deletions src/internal/agent/hooks/argocd-application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ func TestArgoAppWebhook(t *testing.T) {
"/spec/sources/1/repoURL",
"https://git-server.com/a-push-user/almonds-640159520",
),
operations.ReplacePatchOperation(
"/metadata/labels",
map[string]string{
"zarf-agent": "patched",
},
),
},
code: http.StatusOK,
},
Expand Down
5 changes: 4 additions & 1 deletion src/internal/agent/hooks/argocd-repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,12 @@ func mutateRepositorySecret(ctx context.Context, r *v1.AdmissionRequest, cluster
message.Debugf("original url of (%s) got mutated to (%s)", repoCreds.URL, patchedURL)
}

patches := populateArgoRepositoryPatchOperations(patchedURL, state.GitServer)
patches = append(patches, getLabelPatch(secret.Labels))

return &operations.Result{
Allowed: true,
PatchOps: populateArgoRepositoryPatchOperations(patchedURL, state.GitServer),
PatchOps: patches,
}, nil
}

Expand Down
14 changes: 14 additions & 0 deletions src/internal/agent/hooks/argocd-repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ func TestArgoRepoWebhook(t *testing.T) {
"/data/password",
b64.StdEncoding.EncodeToString([]byte(state.GitServer.PullPassword)),
),
operations.ReplacePatchOperation(
"/metadata/labels",
map[string]string{
"argocd.argoproj.io/secret-type": "repository",
"zarf-agent": "patched",
},
),
},
code: http.StatusOK,
},
Expand Down Expand Up @@ -103,6 +110,13 @@ func TestArgoRepoWebhook(t *testing.T) {
"/data/password",
b64.StdEncoding.EncodeToString([]byte(state.GitServer.PullPassword)),
),
operations.ReplacePatchOperation(
"/metadata/labels",
map[string]string{
"argocd.argoproj.io/secret-type": "repository",
"zarf-agent": "patched",
},
),
},
code: http.StatusOK,
},
Expand Down
15 changes: 15 additions & 0 deletions src/internal/agent/hooks/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors

// Package hooks contains the mutation hooks for the Zarf agent.
package hooks

import "github.com/defenseunicorns/zarf/src/internal/agent/operations"

func getLabelPatch(currLabels map[string]string) operations.PatchOperation {
if currLabels == nil {
currLabels = make(map[string]string)
}
currLabels["zarf-agent"] = "patched"
return operations.ReplacePatchOperation("/metadata/labels", currLabels)
}
1 change: 1 addition & 0 deletions src/internal/agent/hooks/flux.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func mutateGitRepo(ctx context.Context, r *v1.AdmissionRequest, cluster *cluster

// Patch updates of the repo spec
patches = populatePatchOperations(patchedURL)
patches = append(patches, getLabelPatch(repo.Labels))

return &operations.Result{
Allowed: true,
Expand Down
12 changes: 12 additions & 0 deletions src/internal/agent/hooks/flux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ func TestFluxMutationWebhook(t *testing.T) {
"/spec/secretRef",
fluxmeta.LocalObjectReference{Name: config.ZarfGitServerSecretName},
),
operations.ReplacePatchOperation(
"/metadata/labels",
map[string]string{
"zarf-agent": "patched",
},
),
},
code: http.StatusOK,
},
Expand Down Expand Up @@ -100,6 +106,12 @@ func TestFluxMutationWebhook(t *testing.T) {
"/spec/secretRef",
fluxmeta.LocalObjectReference{Name: config.ZarfGitServerSecretName},
),
operations.ReplacePatchOperation(
"/metadata/labels",
map[string]string{
"zarf-agent": "patched",
},
),
},
code: http.StatusOK,
},
Expand Down
23 changes: 7 additions & 16 deletions src/internal/agent/hooks/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ func mutatePod(ctx context.Context, r *v1.AdmissionRequest, cluster *cluster.Clu
}
registryURL := state.RegistryInfo.Address

var patchOperations []operations.PatchOperation
var patches []operations.PatchOperation

// Add the zarf secret to the podspec
zarfSecret := []corev1.LocalObjectReference{{Name: config.ZarfImagePullSecretName}}
patchOperations = append(patchOperations, operations.ReplacePatchOperation("/spec/imagePullSecrets", zarfSecret))
patches = append(patches, operations.ReplacePatchOperation("/spec/imagePullSecrets", zarfSecret))

// update the image host for each init container
for idx, container := range pod.Spec.InitContainers {
Expand All @@ -78,7 +78,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
}
patchOperations = append(patchOperations, operations.ReplacePatchOperation(path, replacement))
patches = append(patches, operations.ReplacePatchOperation(path, replacement))
}

// update the image host for each ephemeral container
Expand All @@ -89,7 +89,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
}
patchOperations = append(patchOperations, operations.ReplacePatchOperation(path, replacement))
patches = append(patches, operations.ReplacePatchOperation(path, replacement))
}

// update the image host for each normal container
Expand All @@ -100,22 +100,13 @@ 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
}
patchOperations = append(patchOperations, operations.ReplacePatchOperation(path, replacement))
patches = append(patches, operations.ReplacePatchOperation(path, replacement))
}

// Add a label noting the zarf mutation
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"))
}
patches = append(patches, getLabelPatch(pod.Labels))

return &operations.Result{
Allowed: true,
PatchOps: patchOperations,
PatchOps: patches,
}, nil
}
12 changes: 8 additions & 4 deletions src/internal/agent/hooks/pods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ func TestPodMutationWebhook(t *testing.T) {
name: "pod with label should be mutated",
admissionReq: createPodAdmissionRequest(t, v1.Create, &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"should-be": "mutated"},
Labels: map[string]string{"should-be": "mutated"},
Annotations: map[string]string{"should-be": "mutated"},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{{Image: "nginx"}},
Expand Down Expand Up @@ -78,8 +79,11 @@ func TestPodMutationWebhook(t *testing.T) {
"127.0.0.1:31999/library/nginx:latest-zarf-3793515731",
),
operations.ReplacePatchOperation(
"/metadata/labels/zarf-agent",
"patched",
"/metadata/labels",
map[string]string{
"zarf-agent": "patched",
"should-be": "mutated",
},
),
},
code: http.StatusOK,
Expand Down Expand Up @@ -116,7 +120,7 @@ func TestPodMutationWebhook(t *testing.T) {
"/spec/containers/0/image",
"127.0.0.1:31999/library/nginx:latest-zarf-3793515731",
),
operations.AddPatchOperation(
operations.ReplacePatchOperation(
"/metadata/labels",
map[string]string{"zarf-agent": "patched"},
),
Expand Down

0 comments on commit f69e42d

Please sign in to comment.