Skip to content

Commit 31283d7

Browse files
ansdChunyiLyu
authored andcommitted
Avoid unnecessary object updates (#438)
* Avoid unnecessary update for headless service Before this commit the headless service was updated unnecessarily in every reconile loop. * Avoid unnecessary update for client service Before this commit the client service was updated unnecessarily in every reconile loop. * Avoid unnecessary update for StatefulSet Before this commit the stateful set was updated unnecessarily in every reconile loop. * Revert "Avoid unnecessary update for StatefulSet" This reverts commit 3c28bc5.
1 parent 2f54a43 commit 31283d7

File tree

5 files changed

+128
-91
lines changed

5 files changed

+128
-91
lines changed

docs/examples/additionalPorts/rabbitmq.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ spec:
1111
- name: additional-port # adds an additional port on the service
1212
protocol: TCP
1313
port: 12345
14+
targetPort: 12345
1415
statefulSet:
1516
spec:
1617
template:

internal/resource/headless_service.go

+14-8
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ package resource
1111

1212
import (
1313
"fmt"
14+
1415
rabbitmqv1beta1 "github.com/rabbitmq/cluster-operator/api/v1beta1"
1516
"github.com/rabbitmq/cluster-operator/internal/metadata"
1617
corev1 "k8s.io/api/core/v1"
1718
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1819
"k8s.io/apimachinery/pkg/runtime"
20+
"k8s.io/apimachinery/pkg/util/intstr"
1921
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
2022
)
2123

@@ -49,18 +51,22 @@ func (builder *HeadlessServiceBuilder) Update(object runtime.Object) error {
4951
service.Labels = metadata.GetLabels(builder.Instance.Name, builder.Instance.Labels)
5052
service.Annotations = metadata.ReconcileAndFilterAnnotations(service.GetAnnotations(), builder.Instance.Annotations)
5153
service.Spec = corev1.ServiceSpec{
52-
ClusterIP: "None",
53-
Selector: metadata.LabelSelector(builder.Instance.Name),
54+
Type: corev1.ServiceTypeClusterIP,
55+
ClusterIP: "None",
56+
SessionAffinity: corev1.ServiceAffinityNone,
57+
Selector: metadata.LabelSelector(builder.Instance.Name),
5458
Ports: []corev1.ServicePort{
5559
{
56-
Protocol: corev1.ProtocolTCP,
57-
Port: 4369,
58-
Name: "epmd",
60+
Protocol: corev1.ProtocolTCP,
61+
Port: 4369,
62+
TargetPort: intstr.FromInt(4369),
63+
Name: "epmd",
5964
},
6065
{
61-
Protocol: corev1.ProtocolTCP,
62-
Port: 25672,
63-
Name: "cluster-rpc", // aka distribution port
66+
Protocol: corev1.ProtocolTCP,
67+
Port: 25672,
68+
TargetPort: intstr.FromInt(25672),
69+
Name: "cluster-rpc", // aka distribution port
6470
},
6571
},
6672
PublishNotReadyAddresses: true,

internal/resource/headless_service_test.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
corev1 "k8s.io/api/core/v1"
1818
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1919
"k8s.io/apimachinery/pkg/runtime"
20+
"k8s.io/apimachinery/pkg/util/intstr"
2021
defaultscheme "k8s.io/client-go/kubernetes/scheme"
2122
)
2223

@@ -175,20 +176,24 @@ var _ = Describe("HeadlessService", func() {
175176

176177
It("sets the required Spec", func() {
177178
expectedSpec := corev1.ServiceSpec{
179+
Type: corev1.ServiceTypeClusterIP,
178180
ClusterIP: "None",
179181
Selector: map[string]string{
180182
"app.kubernetes.io/name": "rabbit-spec",
181183
},
184+
SessionAffinity: corev1.ServiceAffinityNone,
182185
Ports: []corev1.ServicePort{
183186
{
184-
Protocol: corev1.ProtocolTCP,
185-
Port: 4369,
186-
Name: "epmd",
187+
Protocol: corev1.ProtocolTCP,
188+
Port: 4369,
189+
TargetPort: intstr.FromInt(4369),
190+
Name: "epmd",
187191
},
188192
{
189-
Protocol: corev1.ProtocolTCP,
190-
Port: 25672,
191-
Name: "cluster-rpc",
193+
Protocol: corev1.ProtocolTCP,
194+
Port: 25672,
195+
TargetPort: intstr.FromInt(25672),
196+
Name: "cluster-rpc",
192197
},
193198
},
194199
PublishNotReadyAddresses: true,

internal/resource/service.go

+29-21
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"encoding/json"
1414
"fmt"
1515

16+
"k8s.io/apimachinery/pkg/util/intstr"
1617
"k8s.io/apimachinery/pkg/util/strategicpatch"
1718

1819
rabbitmqv1beta1 "github.com/rabbitmq/cluster-operator/api/v1beta1"
@@ -111,49 +112,56 @@ func applySvcOverride(svc *corev1.Service, override *rabbitmqv1beta1.Service) er
111112
func (builder *ServiceBuilder) updatePorts(servicePorts []corev1.ServicePort) []corev1.ServicePort {
112113
servicePortsMap := map[string]corev1.ServicePort{
113114
"amqp": {
114-
Protocol: corev1.ProtocolTCP,
115-
Port: 5672,
116-
Name: "amqp",
115+
Protocol: corev1.ProtocolTCP,
116+
Port: 5672,
117+
TargetPort: intstr.FromInt(5672),
118+
Name: "amqp",
117119
},
118120
"management": {
119-
Protocol: corev1.ProtocolTCP,
120-
Port: 15672,
121-
Name: "management",
121+
Protocol: corev1.ProtocolTCP,
122+
Port: 15672,
123+
TargetPort: intstr.FromInt(15672),
124+
Name: "management",
122125
},
123126
}
124127
if builder.Instance.AdditionalPluginEnabled("rabbitmq_mqtt") {
125128
servicePortsMap["mqtt"] = corev1.ServicePort{
126-
Protocol: corev1.ProtocolTCP,
127-
Port: 1883,
128-
Name: "mqtt",
129+
Protocol: corev1.ProtocolTCP,
130+
Port: 1883,
131+
TargetPort: intstr.FromInt(1883),
132+
Name: "mqtt",
129133
}
130134
}
131135
if builder.Instance.AdditionalPluginEnabled("rabbitmq_web_mqtt") {
132136
servicePortsMap["web-mqtt"] = corev1.ServicePort{
133-
Protocol: corev1.ProtocolTCP,
134-
Port: 15675,
135-
Name: "web-mqtt",
137+
Protocol: corev1.ProtocolTCP,
138+
Port: 15675,
139+
TargetPort: intstr.FromInt(15675),
140+
Name: "web-mqtt",
136141
}
137142
}
138143
if builder.Instance.AdditionalPluginEnabled("rabbitmq_stomp") {
139144
servicePortsMap["stomp"] = corev1.ServicePort{
140-
Protocol: corev1.ProtocolTCP,
141-
Port: 61613,
142-
Name: "stomp",
145+
Protocol: corev1.ProtocolTCP,
146+
Port: 61613,
147+
TargetPort: intstr.FromInt(61613),
148+
Name: "stomp",
143149
}
144150
}
145151
if builder.Instance.AdditionalPluginEnabled("rabbitmq_web_stomp") {
146152
servicePortsMap["web-stomp"] = corev1.ServicePort{
147-
Protocol: corev1.ProtocolTCP,
148-
Port: 15674,
149-
Name: "web-stomp",
153+
Protocol: corev1.ProtocolTCP,
154+
Port: 15674,
155+
TargetPort: intstr.FromInt(15674),
156+
Name: "web-stomp",
150157
}
151158
}
152159
if builder.Instance.TLSEnabled() {
153160
servicePortsMap["amqps"] = corev1.ServicePort{
154-
Protocol: corev1.ProtocolTCP,
155-
Port: 5671,
156-
Name: "amqps",
161+
Protocol: corev1.ProtocolTCP,
162+
Port: 5671,
163+
TargetPort: intstr.FromInt(5671),
164+
Name: "amqps",
157165
}
158166
}
159167

0 commit comments

Comments
 (0)