Skip to content

Commit bf5d4cd

Browse files
authored
Merge pull request #1054 from viccuad/fix-service-labels
fix: Correctly set service labels
2 parents 3d92220 + fa56533 commit bf5d4cd

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

internal/controller/policyserver_controller_service.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ func (r *PolicyServerReconciler) reconcilePolicyServerService(ctx context.Contex
3838
ObjectMeta: metav1.ObjectMeta{
3939
Name: policyServer.NameWithPrefix(),
4040
Namespace: r.DeploymentsNamespace,
41-
Labels: policyServer.CommonLabels(),
4241
},
4342
}
4443
_, err := controllerutil.CreateOrPatch(ctx, r.Client, &svc, func() error {
@@ -53,9 +52,14 @@ func (r *PolicyServerReconciler) reconcilePolicyServerService(ctx context.Contex
5352
func (r *PolicyServerReconciler) updateService(svc *corev1.Service, policyServer *policiesv1.PolicyServer) error {
5453
svc.Name = policyServer.NameWithPrefix()
5554
svc.Namespace = r.DeploymentsNamespace
56-
svc.Labels = map[string]string{
57-
constants.AppLabelKey: policyServer.AppLabel(),
55+
templateLabels := map[string]string{
56+
constants.AppLabelKey: policyServer.AppLabel(),
57+
constants.PolicyServerLabelKey: policyServer.Name,
5858
}
59+
for key, value := range policyServer.CommonLabels() {
60+
templateLabels[key] = value
61+
}
62+
svc.Labels = templateLabels
5963
svc.Spec = corev1.ServiceSpec{
6064
Ports: []corev1.ServicePort{
6165
{

internal/controller/policyserver_controller_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ var _ = Describe("PolicyServer controller", func() {
608608
}).Should(Succeed())
609609
})
610610

611-
It("should create service with owner reference", func() {
611+
It("should create service with owner reference and correct labels", func() {
612612
policyServer := policiesv1.NewPolicyServerFactory().WithName(policyServerName).Build()
613613
createPolicyServerAndWaitForItsService(ctx, policyServer)
614614

@@ -640,6 +640,11 @@ var _ = Describe("PolicyServer controller", func() {
640640
"Port": Equal(int32(constants.PolicyServerPort)),
641641
"TargetPort": Equal(intstr.IntOrString{IntVal: int32(constants.PolicyServerPort)}),
642642
})))
643+
// This checks that the service has the legacy and recommended labels
644+
// TODO simplify this with a future release
645+
for k, v := range policyServer.CommonLabels() {
646+
Expect(service.ObjectMeta.Labels).To(HaveKeyWithValue(k, v))
647+
}
643648
return nil
644649
}).Should(Succeed())
645650
})

0 commit comments

Comments
 (0)