From 2a0af85aff37531bffedc90c40e44735d5e073ad Mon Sep 17 00:00:00 2001 From: Tiago Angelo Date: Sun, 9 Oct 2022 17:42:20 -0300 Subject: [PATCH 1/3] Using immutable labels as service selectors --- pkg/collector/reconcile/service.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pkg/collector/reconcile/service.go b/pkg/collector/reconcile/service.go index 896c99a356..11036f9d9b 100644 --- a/pkg/collector/reconcile/service.go +++ b/pkg/collector/reconcile/service.go @@ -71,9 +71,8 @@ func desiredService(ctx context.Context, params Params) *corev1.Service { labels := collector.Labels(params.Instance, []string{}) labels["app.kubernetes.io/name"] = naming.Service(params.Instance) - // by coincidence, the selector is the same as the label, but note that the selector points to the deployment - // whereas 'labels' refers to the service - selector := labels + selector := collector.SelectorLabels(params.Instance) + selector["app.kubernetes.io/name"] = naming.Service(params.Instance) config, err := adapters.ConfigFromString(params.Instance.Spec.Config) if err != nil { @@ -176,7 +175,7 @@ func monitoringService(ctx context.Context, params Params) *corev1.Service { labels := collector.Labels(params.Instance, []string{}) labels["app.kubernetes.io/name"] = naming.MonitoringService(params.Instance) - selector := collector.Labels(params.Instance, []string{}) + selector := collector.SelectorLabels(params.Instance) selector["app.kubernetes.io/name"] = fmt.Sprintf("%s-collector", params.Instance.Name) return &corev1.Service{ From 7bc097ef20229d67bc2b493dca79bff644da4702 Mon Sep 17 00:00:00 2001 From: Tiago Angelo Date: Mon, 10 Oct 2022 21:01:58 -0300 Subject: [PATCH 2/3] Fixing expected service selectors --- pkg/collector/reconcile/service_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/collector/reconcile/service_test.go b/pkg/collector/reconcile/service_test.go index f200a166bc..8dec90bcf5 100644 --- a/pkg/collector/reconcile/service_test.go +++ b/pkg/collector/reconcile/service_test.go @@ -233,7 +233,8 @@ func service(name string, ports []v1.ServicePort) v1.Service { labels := collector.Labels(params().Instance, []string{}) labels["app.kubernetes.io/name"] = name - selector := labels + selector := collector.SelectorLabels(params().Instance) + selector["app.kubernetes.io/name"] = name return v1.Service{ ObjectMeta: metav1.ObjectMeta{ Name: name, From aae4f8daf951b141e1b569e83f961f9d2a28f87c Mon Sep 17 00:00:00 2001 From: Tiago Angelo Date: Sat, 15 Oct 2022 10:21:54 -0300 Subject: [PATCH 3/3] Removing unnecessary selector --- pkg/collector/reconcile/service.go | 10 ++-------- pkg/collector/reconcile/service_test.go | 4 +--- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/pkg/collector/reconcile/service.go b/pkg/collector/reconcile/service.go index 11036f9d9b..86a84f2002 100644 --- a/pkg/collector/reconcile/service.go +++ b/pkg/collector/reconcile/service.go @@ -71,9 +71,6 @@ func desiredService(ctx context.Context, params Params) *corev1.Service { labels := collector.Labels(params.Instance, []string{}) labels["app.kubernetes.io/name"] = naming.Service(params.Instance) - selector := collector.SelectorLabels(params.Instance) - selector["app.kubernetes.io/name"] = naming.Service(params.Instance) - config, err := adapters.ConfigFromString(params.Instance.Spec.Config) if err != nil { params.Log.Error(err, "couldn't extract the configuration from the context") @@ -119,7 +116,7 @@ func desiredService(ctx context.Context, params Params) *corev1.Service { Annotations: params.Instance.Annotations, }, Spec: corev1.ServiceSpec{ - Selector: selector, + Selector: collector.SelectorLabels(params.Instance), ClusterIP: "", Ports: ports, }, @@ -175,9 +172,6 @@ func monitoringService(ctx context.Context, params Params) *corev1.Service { labels := collector.Labels(params.Instance, []string{}) labels["app.kubernetes.io/name"] = naming.MonitoringService(params.Instance) - selector := collector.SelectorLabels(params.Instance) - selector["app.kubernetes.io/name"] = fmt.Sprintf("%s-collector", params.Instance.Name) - return &corev1.Service{ ObjectMeta: metav1.ObjectMeta{ Name: naming.MonitoringService(params.Instance), @@ -186,7 +180,7 @@ func monitoringService(ctx context.Context, params Params) *corev1.Service { Annotations: params.Instance.Annotations, }, Spec: corev1.ServiceSpec{ - Selector: selector, + Selector: collector.SelectorLabels(params.Instance), ClusterIP: "", Ports: []corev1.ServicePort{{ Name: "monitoring", diff --git a/pkg/collector/reconcile/service_test.go b/pkg/collector/reconcile/service_test.go index 8dec90bcf5..c98553b208 100644 --- a/pkg/collector/reconcile/service_test.go +++ b/pkg/collector/reconcile/service_test.go @@ -233,8 +233,6 @@ func service(name string, ports []v1.ServicePort) v1.Service { labels := collector.Labels(params().Instance, []string{}) labels["app.kubernetes.io/name"] = name - selector := collector.SelectorLabels(params().Instance) - selector["app.kubernetes.io/name"] = name return v1.Service{ ObjectMeta: metav1.ObjectMeta{ Name: name, @@ -243,7 +241,7 @@ func service(name string, ports []v1.ServicePort) v1.Service { Annotations: params().Instance.Annotations, }, Spec: v1.ServiceSpec{ - Selector: selector, + Selector: collector.SelectorLabels(params().Instance), ClusterIP: "", Ports: ports, },