Skip to content
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
2 changes: 2 additions & 0 deletions api/v1beta1/rabbitmqcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,8 @@ type RabbitmqClusterServiceSpec struct {
// See also: https://pkg.go.dev/k8s.io/api/core/v1#IPFamilyPolicy
// +kubebuilder:validation:Enum=SingleStack;PreferDualStack;RequireDualStack
IPFamilyPolicy *corev1.IPFamilyPolicy `json:"ipFamilyPolicy,omitempty"`
// +optional
Labels map[string]string `json:"labels,omitempty"`
}

func (cluster *RabbitmqCluster) TLSEnabled() bool {
Expand Down
7 changes: 7 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions config/crd/bases/rabbitmq.com_rabbitmqclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5155,6 +5155,10 @@ spec:
- PreferDualStack
- RequireDualStack
type: string
labels:
additionalProperties:
type: string
type: object
type:
default: ClusterIP
description: |-
Expand Down
1 change: 1 addition & 0 deletions docs/api/rabbitmq.com.ref.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ For more info see https://pkg.go.dev/k8s.io/api/core/v1#ServiceType
| *`annotations`* __object (keys:string, values:string)__ | Annotations to add to the Service.
| *`ipFamilyPolicy`* __link:https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.23/#ipfamilypolicy-v1-core[$$IPFamilyPolicy$$]__ | IPFamilyPolicy represents the dual-stack-ness requested or required by a Service
See also: https://pkg.go.dev/k8s.io/api/core/v1#IPFamilyPolicy
| *`labels`* __object (keys:string, values:string)__ |
|===


Expand Down
9 changes: 9 additions & 0 deletions internal/resource/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ func (builder *ServiceBuilder) Update(object client.Object) error {
service := object.(*corev1.Service)
builder.setAnnotations(service)
service.Labels = metadata.GetLabels(builder.Instance.Name, builder.Instance.Labels)

if builder.Instance.Spec.Service.Labels != nil {
for k, v := range builder.Instance.Spec.Service.Labels {
if _, exists := service.Labels[k]; !exists {
service.Labels[k] = v
}
}
}

service.Spec.Type = builder.Instance.Spec.Service.Type
service.Spec.Selector = metadata.LabelSelector(builder.Instance.Name)
service.Spec.IPFamilyPolicy = builder.Instance.Spec.Service.IPFamilyPolicy
Expand Down
49 changes: 49 additions & 0 deletions internal/resource/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,25 @@ var _ = Context("Services", func() {
It("deletes the labels that are removed from the CR", func() {
Expect(svc.Labels).NotTo(HaveKey("this-was-the-previous-label"))
})

It("merges user provided service labels without overwriting internal labels", func() {
userServiceLabels := map[string]string{
"user-label": "user-value",
"app.kubernetes.io/part-of": "should-not-overwrite",
}

expectedLabels := map[string]string{
"app.kubernetes.io/name": "foo",
"app.kubernetes.io/component": "rabbitmq",
"app.kubernetes.io/part-of": "rabbitmq",
"user-label": "user-value",
}

service := updateServiceWithLabels(builder, nil, userServiceLabels)

Expect(service.ObjectMeta.Labels).To(Equal(expectedLabels))
})

})

Context("Service Type", func() {
Expand Down Expand Up @@ -902,3 +921,33 @@ func updateServiceWithAnnotations(rmqBuilder resource.RabbitmqResourceBuilder, i
Expect(serviceBuilder.Update(svc)).To(Succeed())
return svc
}

func updateServiceWithLabels(rmqBuilder resource.RabbitmqResourceBuilder, instanceLabels, serviceLabels map[string]string) *corev1.Service {
instance := &rabbitmqv1beta1.RabbitmqCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "foo-namespace",
Labels: instanceLabels,
},
Spec: rabbitmqv1beta1.RabbitmqClusterSpec{
Service: rabbitmqv1beta1.RabbitmqClusterServiceSpec{
Labels: serviceLabels,
},
},
}

rmqBuilder.Instance = instance
serviceBuilder := rmqBuilder.Service()
svc := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "foo-service",
Namespace: "foo-namespace",
Labels: map[string]string{
"app.kubernetes.io/name": "do-not-touch",
"app.kubernetes.io/part-of": "rabbitmq",
},
},
}
Expect(serviceBuilder.Update(svc)).To(Succeed())
return svc
}
Loading