Skip to content

Commit 2b7ab2d

Browse files
authored
Merge pull request #1874 from viccuad/feat/add-labels-configmaps
feat(controller): Add tracking labels to ConfigMaps of PolicyServers
2 parents 959c544 + 93f81d0 commit 2b7ab2d

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

internal/controller/policyserver_controller_configmap.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8+
"maps"
89

910
corev1 "k8s.io/api/core/v1"
1011
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -165,9 +166,15 @@ func (r *PolicyServerReconciler) updateConfigMapData(cfg *corev1.ConfigMap, poli
165166
}
166167

167168
cfg.Data = data
168-
cfg.ObjectMeta.Labels = map[string]string{
169-
constants.PolicyServerLabelKey: policyServer.ObjectMeta.Name,
169+
labels := map[string]string{
170+
// PolicyServerLabelKey is used to map ConfigMap events to policy reconcile
171+
// requests
172+
constants.PolicyServerLabelKey: policyServer.Name,
170173
}
174+
// Use the same labels as other resources backing the PolicyServer, to track
175+
// for deletion later on
176+
maps.Copy(labels, policyServer.CommonLabels())
177+
cfg.Labels = labels
171178
if err = controllerutil.SetOwnerReference(policyServer, cfg, r.Client.Scheme()); err != nil {
172179
return errors.Join(errors.New("failed to set policy server configmap owner reference"), err)
173180
}

internal/controller/policyserver_controller_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,19 @@ var _ = Describe("PolicyServer controller", func() {
269269
})))
270270
})
271271

272+
It("should create the policy server configmap with the same labels as the other policy server resources", func() {
273+
policyServer := policiesv1.NewPolicyServerFactory().WithName(policyServerName).Build()
274+
createPolicyServerAndWaitForItsService(ctx, policyServer)
275+
276+
configmap, err := getTestPolicyServerConfigMap(ctx, policyServerName)
277+
Expect(err).ToNot(HaveOccurred())
278+
279+
Expect(configmap.Labels).To(HaveKeyWithValue(constants.PolicyServerLabelKey, policyServerName))
280+
for k, v := range policyServer.CommonLabels() {
281+
Expect(configmap.Labels).To(HaveKeyWithValue(k, v))
282+
}
283+
})
284+
272285
It("should create the policy server configmap with the assigned policies", func() {
273286
timeoutEvalSeconds := int32(5)
274287

0 commit comments

Comments
 (0)