diff --git a/pkg/controller/logstash/service.go b/pkg/controller/logstash/service.go index cbe9e32ca6..b802dde531 100644 --- a/pkg/controller/logstash/service.go +++ b/pkg/controller/logstash/service.go @@ -13,6 +13,7 @@ import ( "github.com/elastic/cloud-on-k8s/v2/pkg/controller/common/defaults" "github.com/elastic/cloud-on-k8s/v2/pkg/controller/logstash/labels" "github.com/elastic/cloud-on-k8s/v2/pkg/controller/logstash/network" + "github.com/elastic/cloud-on-k8s/v2/pkg/utils/maps" ) const ( @@ -70,8 +71,7 @@ func newService(service logstashv1alpha1.LogstashService, logstash logstashv1alp svc.ObjectMeta.Name = logstashv1alpha1.UserServiceName(logstash.Name, service.Name) labels := labels.NewLabels(logstash) - - svc.Labels = labels + svc.Labels = maps.MergePreservingExistingKeys(svc.Labels, labels) if svc.Spec.Selector == nil { svc.Spec.Selector = labels diff --git a/pkg/controller/logstash/service_test.go b/pkg/controller/logstash/service_test.go index 5fd2acb8a5..25e0a169a0 100644 --- a/pkg/controller/logstash/service_test.go +++ b/pkg/controller/logstash/service_test.go @@ -9,6 +9,7 @@ import ( "testing" "github.com/stretchr/testify/require" + "k8s.io/utils/ptr" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -20,7 +21,6 @@ import ( ) func TestReconcileServices(t *testing.T) { - trueVal := true testCases := []struct { name string logstash logstashv1alpha1.Logstash @@ -47,8 +47,8 @@ func TestReconcileServices(t *testing.T) { APIVersion: "logstash.k8s.elastic.co/v1alpha1", Kind: "Logstash", Name: "logstash", - Controller: &trueVal, - BlockOwnerDeletion: &trueVal, + Controller: ptr.To(true), + BlockOwnerDeletion: ptr.To(true), }, }, }, @@ -97,8 +97,8 @@ func TestReconcileServices(t *testing.T) { APIVersion: "logstash.k8s.elastic.co/v1alpha1", Kind: "Logstash", Name: "logstash", - Controller: &trueVal, - BlockOwnerDeletion: &trueVal, + Controller: ptr.To(true), + BlockOwnerDeletion: ptr.To(true), }, }, }, @@ -148,8 +148,8 @@ func TestReconcileServices(t *testing.T) { APIVersion: "logstash.k8s.elastic.co/v1alpha1", Kind: "Logstash", Name: "logstash", - Controller: &trueVal, - BlockOwnerDeletion: &trueVal, + Controller: ptr.To(true), + BlockOwnerDeletion: ptr.To(true), }, }, }, @@ -163,21 +163,49 @@ func TestReconcileServices(t *testing.T) { }, }, }, + DefaultAPIService(), + }, + }, + { + name: "Preserve user defined labels", + logstash: logstashv1alpha1.Logstash{ + ObjectMeta: metav1.ObjectMeta{ + Name: "logstash", + Namespace: "test", + }, + Spec: logstashv1alpha1.LogstashSpec{ + Services: []logstashv1alpha1.LogstashService{{ + Name: "test", + Service: commonv1.ServiceTemplate{ + ObjectMeta: metav1.ObjectMeta{ + Labels: map[string]string{"some.label": "abc"}, + }, + Spec: corev1.ServiceSpec{ + Ports: []corev1.ServicePort{ + {Protocol: "TCP", Port: 9200}, + }, + }, + }, + }}, + }, + }, + wantSvc: []corev1.Service{ { ObjectMeta: metav1.ObjectMeta{ - Name: "logstash-ls-api", + Name: "logstash-ls-test", Namespace: "test", Labels: map[string]string{ "common.k8s.elastic.co/type": "logstash", "logstash.k8s.elastic.co/name": "logstash", + "some.label": "abc", }, OwnerReferences: []metav1.OwnerReference{ { APIVersion: "logstash.k8s.elastic.co/v1alpha1", Kind: "Logstash", Name: "logstash", - Controller: &trueVal, - BlockOwnerDeletion: &trueVal, + Controller: ptr.To(true), + BlockOwnerDeletion: ptr.To(true), }, }, }, @@ -186,12 +214,13 @@ func TestReconcileServices(t *testing.T) { "common.k8s.elastic.co/type": "logstash", "logstash.k8s.elastic.co/name": "logstash", }, - ClusterIP: "None", + ClusterIP: "", Ports: []corev1.ServicePort{ - {Name: "api", Protocol: "TCP", Port: 9600}, + {Protocol: "TCP", Port: 9200}, }, }, }, + DefaultAPIService(), }, }, } @@ -218,3 +247,35 @@ func TestReconcileServices(t *testing.T) { }) } } + +func DefaultAPIService() corev1.Service { + return corev1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "logstash-ls-api", + Namespace: "test", + Labels: map[string]string{ + "common.k8s.elastic.co/type": "logstash", + "logstash.k8s.elastic.co/name": "logstash", + }, + OwnerReferences: []metav1.OwnerReference{ + { + APIVersion: "logstash.k8s.elastic.co/v1alpha1", + Kind: "Logstash", + Name: "logstash", + Controller: ptr.To(true), + BlockOwnerDeletion: ptr.To(true), + }, + }, + }, + Spec: corev1.ServiceSpec{ + Selector: map[string]string{ + "common.k8s.elastic.co/type": "logstash", + "logstash.k8s.elastic.co/name": "logstash", + }, + ClusterIP: "None", + Ports: []corev1.ServicePort{ + {Name: "api", Protocol: "TCP", Port: 9600}, + }, + }, + } +}