Skip to content

Commit 7a06d20

Browse files
authored
Merge pull request #1037 from flavio/policy-server-use-kubernetes-recommended-labels
policy server use kubernetes recommended labels
2 parents 8843568 + 0f18201 commit 7a06d20

8 files changed

+40
-8
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ KUSTOMIZE_VERSION ?= v5.4.1
203203
CONTROLLER_TOOLS_VERSION ?= v0.16.1
204204
ENVTEST_VERSION ?= release-0.18
205205
GOLANGCI_LINT_VERSION ?= v1.64.5
206-
GINKGO_VERSION ?= v2.22.2
206+
GINKGO_VERSION ?= v2.23.0
207207

208208
.PHONY: kustomize
209209
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.

api/policies/v1/policyserver_types.go

+13
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package v1
1818

1919
import (
20+
"github.com/kubewarden/kubewarden-controller/internal/constants"
2021
corev1 "k8s.io/api/core/v1"
2122
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2223
"k8s.io/apimachinery/pkg/util/intstr"
@@ -190,6 +191,18 @@ func (ps *PolicyServer) AppLabel() string {
190191
return "kubewarden-" + ps.NameWithPrefix()
191192
}
192193

194+
// CommonLabels returns the common labels to be used with the resources
195+
// associated to a Policy Server. The labels defined follow
196+
// Kubernetes guidelines: https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/#labels
197+
func (ps *PolicyServer) CommonLabels() map[string]string {
198+
return map[string]string{
199+
constants.ComponentLabelKey: constants.ComponentPolicyServerLabelValue,
200+
constants.InstanceLabelKey: ps.NameWithPrefix(),
201+
constants.PartOfLabelKey: constants.PartOfLabelValue,
202+
constants.ManagedByKey: "kubewarden-controller",
203+
}
204+
}
205+
193206
//+kubebuilder:object:root=true
194207

195208
// PolicyServerList contains a list of PolicyServer.

internal/constants/constants.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,13 @@ const (
3232
// Labels.
3333
AppLabelKey = "app"
3434
PolicyServerLabelKey = "kubewarden/policy-server"
35+
ComponentPolicyServerLabelValue = "policy-server"
36+
NameLabelKey = "app.kubernetes.io/name"
37+
InstanceLabelKey = "app.kubernetes.io/instance"
38+
ComponentLabelKey = "app.kubernetes.io/component"
3539
PartOfLabelKey = "app.kubernetes.io/part-of"
3640
PartOfLabelValue = "kubewarden"
37-
ComponentLabelKey = "app.kubernetes.io/component"
38-
ComponentPolicyServerLabelValue = "policy-server"
41+
ManagedByKey = "app.kubernetes.io/managed-by"
3942

4043
// Index.
4144
PolicyServerIndexKey = ".spec.policyServer"

internal/controller/policyserver_controller_configmap.go

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ func (r *PolicyServerReconciler) reconcilePolicyServerConfigMap(
115115
ObjectMeta: metav1.ObjectMeta{
116116
Name: policyServer.NameWithPrefix(),
117117
Namespace: r.DeploymentsNamespace,
118+
Labels: policyServer.CommonLabels(),
118119
},
119120
}
120121
_, err := controllerutil.CreateOrPatch(ctx, r.Client, cfg, func() error {

internal/controller/policyserver_controller_deployment.go

+14-5
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,10 @@ func configureLabelsAndAnnotations(policyServerDeployment *appsv1.Deployment, po
297297
}
298298
policyServerDeployment.Labels[constants.AppLabelKey] = policyServer.AppLabel()
299299
policyServerDeployment.Labels[constants.PolicyServerLabelKey] = policyServer.Name
300+
301+
for key, value := range policyServer.CommonLabels() {
302+
policyServerDeployment.Labels[key] = value
303+
}
300304
}
301305

302306
func (r *PolicyServerReconciler) configureMutualTLS(ctx context.Context, policyServerDeployment *appsv1.Deployment) error {
@@ -373,6 +377,15 @@ func buildPolicyServerDeploymentSpec(
373377
templateAnnotations map[string]string,
374378
podSecurityContext *corev1.PodSecurityContext,
375379
) appsv1.DeploymentSpec {
380+
templateLabels := map[string]string{
381+
constants.AppLabelKey: policyServer.AppLabel(),
382+
constants.PolicyServerDeploymentPodSpecConfigVersionLabel: configMapVersion,
383+
constants.PolicyServerLabelKey: policyServer.Name,
384+
}
385+
for key, value := range policyServer.CommonLabels() {
386+
templateLabels[key] = value
387+
}
388+
376389
return appsv1.DeploymentSpec{
377390
Replicas: &policyServer.Spec.Replicas,
378391
Selector: &metav1.LabelSelector{
@@ -385,11 +398,7 @@ func buildPolicyServerDeploymentSpec(
385398
},
386399
Template: corev1.PodTemplateSpec{
387400
ObjectMeta: metav1.ObjectMeta{
388-
Labels: map[string]string{
389-
constants.AppLabelKey: policyServer.AppLabel(),
390-
constants.PolicyServerDeploymentPodSpecConfigVersionLabel: configMapVersion,
391-
constants.PolicyServerLabelKey: policyServer.Name,
392-
},
401+
Labels: templateLabels,
393402
Annotations: templateAnnotations,
394403
},
395404
Spec: corev1.PodSpec{

internal/controller/policyserver_controller_pdb.go

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func reconcilePodDisruptionBudget(ctx context.Context, policyServer *policiesv1.
4141
ObjectMeta: metav1.ObjectMeta{
4242
Name: policyServer.NameWithPrefix(),
4343
Namespace: namespace,
44+
Labels: policyServer.CommonLabels(),
4445
},
4546
}
4647
_, err := controllerutil.CreateOrPatch(ctx, k8s, pdb, func() error {

internal/controller/policyserver_controller_service.go

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func (r *PolicyServerReconciler) reconcilePolicyServerService(ctx context.Contex
3838
ObjectMeta: metav1.ObjectMeta{
3939
Name: policyServer.NameWithPrefix(),
4040
Namespace: r.DeploymentsNamespace,
41+
Labels: policyServer.CommonLabels(),
4142
},
4243
}
4344
_, err := controllerutil.CreateOrPatch(ctx, r.Client, &svc, func() error {

internal/controller/policyserver_controller_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ var _ = Describe("PolicyServer controller", func() {
8787
"TolerationSeconds": PointTo(Equal(tolerationSeconds)),
8888
}),
8989
}))
90+
91+
for k, v := range policyServer.CommonLabels() {
92+
Expect(deployment.Spec.Template.ObjectMeta.Labels).To(HaveKeyWithValue(k, v))
93+
}
9094
})
9195

9296
It("should use the policy server affinity configuration in the policy server deployment", func() {

0 commit comments

Comments
 (0)