Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e755c1f

Browse files
committedMar 6, 2025··
refactor(policy-server): get rid of useless labels
Use standard naming convention for our labels (use DNS name as part of key). Also, remove non-standard labels when possible and just use the common Kubernetes labels we are now using. Signed-off-by: Flavio Castelli <[email protected]>
1 parent 0f18201 commit e755c1f

6 files changed

+18
-18
lines changed
 

‎api/policies/v1/policyserver_types.go

-4
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,6 @@ func (ps *PolicyServer) NameWithPrefix() string {
187187
return "policy-server-" + ps.Name
188188
}
189189

190-
func (ps *PolicyServer) AppLabel() string {
191-
return "kubewarden-" + ps.NameWithPrefix()
192-
}
193-
194190
// CommonLabels returns the common labels to be used with the resources
195191
// associated to a Policy Server. The labels defined follow
196192
// Kubernetes guidelines: https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/#labels

‎internal/constants/constants.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ const (
3030
PolicyServerVerificationConfigContainerPath = "/verification"
3131

3232
// Labels.
33-
AppLabelKey = "app"
34-
PolicyServerLabelKey = "kubewarden/policy-server"
33+
PolicyServerLabelKey = "kubewarden.io/policy-server"
3534
ComponentPolicyServerLabelValue = "policy-server"
36-
NameLabelKey = "app.kubernetes.io/name"
3735
InstanceLabelKey = "app.kubernetes.io/instance"
3836
ComponentLabelKey = "app.kubernetes.io/component"
3937
PartOfLabelKey = "app.kubernetes.io/part-of"

‎internal/controller/policyserver_controller_deployment.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,6 @@ func configureLabelsAndAnnotations(policyServerDeployment *appsv1.Deployment, po
295295
if policyServerDeployment.Labels == nil {
296296
policyServerDeployment.Labels = make(map[string]string)
297297
}
298-
policyServerDeployment.Labels[constants.AppLabelKey] = policyServer.AppLabel()
299298
policyServerDeployment.Labels[constants.PolicyServerLabelKey] = policyServer.Name
300299

301300
for key, value := range policyServer.CommonLabels() {
@@ -378,19 +377,21 @@ func buildPolicyServerDeploymentSpec(
378377
podSecurityContext *corev1.PodSecurityContext,
379378
) appsv1.DeploymentSpec {
380379
templateLabels := map[string]string{
381-
constants.AppLabelKey: policyServer.AppLabel(),
382380
constants.PolicyServerDeploymentPodSpecConfigVersionLabel: configMapVersion,
383381
constants.PolicyServerLabelKey: policyServer.Name,
384382
}
385-
for key, value := range policyServer.CommonLabels() {
383+
384+
commonLabels := policyServer.CommonLabels()
385+
for key, value := range commonLabels {
386386
templateLabels[key] = value
387387
}
388388

389389
return appsv1.DeploymentSpec{
390390
Replicas: &policyServer.Spec.Replicas,
391391
Selector: &metav1.LabelSelector{
392392
MatchLabels: map[string]string{
393-
constants.AppLabelKey: policyServer.AppLabel(),
393+
constants.PartOfLabelKey: commonLabels[constants.PartOfLabelKey],
394+
constants.InstanceLabelKey: commonLabels[constants.InstanceLabelKey],
394395
},
395396
},
396397
Strategy: appsv1.DeploymentStrategy{

‎internal/controller/policyserver_controller_pdb.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ func deletePodDisruptionBudget(ctx context.Context, policyServer *policiesv1.Pol
3737
}
3838

3939
func reconcilePodDisruptionBudget(ctx context.Context, policyServer *policiesv1.PolicyServer, k8s client.Client, namespace string) error {
40+
commonLabels := policyServer.CommonLabels()
4041
pdb := &k8spoliciesv1.PodDisruptionBudget{
4142
ObjectMeta: metav1.ObjectMeta{
4243
Name: policyServer.NameWithPrefix(),
4344
Namespace: namespace,
44-
Labels: policyServer.CommonLabels(),
45+
Labels: commonLabels,
4546
},
4647
}
4748
_, err := controllerutil.CreateOrPatch(ctx, k8s, pdb, func() error {
@@ -53,7 +54,8 @@ func reconcilePodDisruptionBudget(ctx context.Context, policyServer *policiesv1.
5354

5455
pdb.Spec.Selector = &metav1.LabelSelector{
5556
MatchLabels: map[string]string{
56-
constants.AppLabelKey: policyServer.AppLabel(),
57+
constants.InstanceLabelKey: commonLabels[constants.InstanceLabelKey],
58+
constants.PartOfLabelKey: commonLabels[constants.PartOfLabelKey],
5759
constants.PolicyServerLabelKey: policyServer.GetName(),
5860
},
5961
}

‎internal/controller/policyserver_controller_service.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,12 @@ func (r *PolicyServerReconciler) reconcilePolicyServerService(ctx context.Contex
5151
}
5252

5353
func (r *PolicyServerReconciler) updateService(svc *corev1.Service, policyServer *policiesv1.PolicyServer) error {
54+
commonLabels := policyServer.CommonLabels()
55+
5456
svc.Name = policyServer.NameWithPrefix()
5557
svc.Namespace = r.DeploymentsNamespace
56-
svc.Labels = map[string]string{
57-
constants.AppLabelKey: policyServer.AppLabel(),
58-
}
58+
svc.Labels = commonLabels
59+
5960
svc.Spec = corev1.ServiceSpec{
6061
Ports: []corev1.ServicePort{
6162
{
@@ -75,7 +76,8 @@ func (r *PolicyServerReconciler) updateService(svc *corev1.Service, policyServer
7576
},
7677
},
7778
Selector: map[string]string{
78-
constants.AppLabelKey: policyServer.AppLabel(),
79+
constants.InstanceLabelKey: commonLabels[constants.InstanceLabelKey],
80+
constants.PartOfLabelKey: commonLabels[constants.PartOfLabelKey],
7981
},
8082
}
8183
if r.MetricsEnabled {

‎internal/controller/utils_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ func policyServerPodDisruptionBudgetMatcher(policyServer *policiesv1.PolicyServe
193193
"MinAvailable": minAvailableMatcher,
194194
"Selector": PointTo(MatchAllFields(Fields{
195195
"MatchLabels": MatchAllKeys(Keys{
196-
constants.AppLabelKey: Equal(policyServer.AppLabel()),
196+
constants.InstanceLabelKey: Equal(policyServer.CommonLabels()[constants.InstanceLabelKey]),
197+
constants.PartOfLabelKey: Equal(policyServer.CommonLabels()[constants.PartOfLabelKey]),
197198
constants.PolicyServerLabelKey: Equal(policyServer.GetName()),
198199
}),
199200
"MatchExpressions": Ignore(),

0 commit comments

Comments
 (0)
Please sign in to comment.