Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Logstash service to preserve user defined labels #7895

Merged
merged 3 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/controller/logstash/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand Down
85 changes: 73 additions & 12 deletions pkg/controller/logstash/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -20,7 +21,6 @@ import (
)

func TestReconcileServices(t *testing.T) {
trueVal := true
testCases := []struct {
name string
logstash logstashv1alpha1.Logstash
Expand All @@ -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),
},
},
},
Expand Down Expand Up @@ -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),
},
},
},
Expand Down Expand Up @@ -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),
},
},
},
Expand All @@ -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),
},
},
},
Expand All @@ -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(),
},
},
}
Expand All @@ -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},
},
},
}
}