Skip to content

Commit c16f748

Browse files
armrumnencia
andauthored
fix(services): reconcile managed service spec changes with patch strategy (#10190)
Previously, the cluster and pooler service reconcilers only detected changes in selectors, labels, and annotations. Changes to other spec fields like loadBalancerSourceRanges were silently ignored when using the patch update strategy, requiring users to switch to the replace strategy (which causes downtime) to apply spec changes. Now the reconcilers compare the full service spec while preserving Kubernetes-managed and defaulted fields like ClusterIP, NodePort, and traffic policies. Closes #10132 Signed-off-by: Armando Ruocco <[email protected]> Signed-off-by: Marco Nenciarini <[email protected]> Co-authored-by: Marco Nenciarini <[email protected]>
1 parent f2434be commit c16f748

5 files changed

Lines changed: 588 additions & 7 deletions

File tree

‎internal/controller/cluster_create.go‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import (
4747
"github.com/cloudnative-pg/cloudnative-pg/pkg/certs"
4848
"github.com/cloudnative-pg/cloudnative-pg/pkg/postgres"
4949
"github.com/cloudnative-pg/cloudnative-pg/pkg/reconciler/persistentvolumeclaim"
50+
"github.com/cloudnative-pg/cloudnative-pg/pkg/servicespec"
5051
"github.com/cloudnative-pg/cloudnative-pg/pkg/specs"
5152
"github.com/cloudnative-pg/cloudnative-pg/pkg/utils"
5253
"github.com/cloudnative-pg/cloudnative-pg/pkg/versions"
@@ -421,12 +422,6 @@ func (r *ClusterReconciler) serviceReconciler(
421422
}
422423
var shouldUpdate bool
423424

424-
// we ensure that the selector perfectly match
425-
if !reflect.DeepEqual(proposed.Spec.Selector, livingService.Spec.Selector) {
426-
livingService.Spec.Selector = proposed.Spec.Selector
427-
shouldUpdate = true
428-
}
429-
430425
// we ensure we've some space to store the labels and the annotations
431426
if livingService.Labels == nil {
432427
livingService.Labels = make(map[string]string)
@@ -446,13 +441,20 @@ func (r *ClusterReconciler) serviceReconciler(
446441
shouldUpdate = true
447442
}
448443

444+
// we check if the service spec has changed, preserving Kubernetes-managed fields
445+
patchedSpec := proposed.Spec.DeepCopy()
446+
servicespec.PreserveKubernetesDefaults(patchedSpec, &livingService.Spec)
447+
if !reflect.DeepEqual(*patchedSpec, livingService.Spec) {
448+
livingService.Spec = *patchedSpec
449+
shouldUpdate = true
450+
}
451+
449452
if !shouldUpdate {
450453
return nil
451454
}
452455

453456
if strategy == apiv1.ServiceUpdateStrategyPatch {
454457
contextLogger.Info("reconciling service")
455-
// we update to ensure that we substitute the selectors
456458
return r.Update(ctx, &livingService)
457459
}
458460

‎internal/controller/cluster_create_test.go‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,6 +1306,80 @@ var _ = Describe("Service Reconciling", func() {
13061306
Expect(updatedService.Spec.Selector).To(Equal(proposedService.Spec.Selector))
13071307
})
13081308

1309+
It("should update the service spec when using patch strategy and spec fields change", func() {
1310+
existingService := proposedService.DeepCopy()
1311+
existingService.Annotations = map[string]string{
1312+
utils.UpdateStrategyAnnotation: string(apiv1.ServiceUpdateStrategyPatch),
1313+
}
1314+
existingService.Spec.Type = corev1.ServiceTypeLoadBalancer
1315+
existingService.Spec.LoadBalancerSourceRanges = []string{"10.0.0.0/8"}
1316+
err := serviceClient.Update(ctx, existingService)
1317+
Expect(err).NotTo(HaveOccurred())
1318+
1319+
proposedService.Annotations = map[string]string{
1320+
utils.UpdateStrategyAnnotation: string(apiv1.ServiceUpdateStrategyPatch),
1321+
}
1322+
proposedService.Spec.Type = corev1.ServiceTypeLoadBalancer
1323+
proposedService.Spec.LoadBalancerSourceRanges = []string{"10.0.0.0/8", "172.16.0.0/12"}
1324+
1325+
err = reconciler.serviceReconciler(ctx, &cluster, proposedService, true)
1326+
Expect(err).NotTo(HaveOccurred())
1327+
1328+
var updatedService corev1.Service
1329+
err = serviceClient.Get(ctx, types.NamespacedName{
1330+
Name: proposedService.Name,
1331+
Namespace: proposedService.Namespace,
1332+
}, &updatedService)
1333+
Expect(err).NotTo(HaveOccurred())
1334+
Expect(updatedService.Spec.LoadBalancerSourceRanges).To(Equal([]string{"10.0.0.0/8", "172.16.0.0/12"}))
1335+
})
1336+
1337+
It("should not trigger unnecessary updates when only Kubernetes-managed fields differ", func() {
1338+
singleStack := corev1.IPFamilyPolicySingleStack
1339+
clusterPolicy := corev1.ServiceInternalTrafficPolicyCluster
1340+
existingService := proposedService.DeepCopy()
1341+
existingService.Spec.Type = corev1.ServiceTypeLoadBalancer
1342+
existingService.Spec.ClusterIP = "10.96.0.1"
1343+
existingService.Spec.ClusterIPs = []string{"10.96.0.1"}
1344+
existingService.Spec.IPFamilies = []corev1.IPFamily{corev1.IPv4Protocol}
1345+
existingService.Spec.IPFamilyPolicy = &singleStack
1346+
existingService.Spec.InternalTrafficPolicy = &clusterPolicy
1347+
existingService.Spec.SessionAffinity = corev1.ServiceAffinityNone
1348+
existingService.Spec.ExternalTrafficPolicy = corev1.ServiceExternalTrafficPolicyLocal
1349+
existingService.Spec.Ports = []corev1.ServicePort{
1350+
{
1351+
Port: 80, Protocol: corev1.ProtocolTCP,
1352+
TargetPort: intstr.FromInt32(80), NodePort: 31234,
1353+
},
1354+
}
1355+
existingService.Spec.HealthCheckNodePort = 30000
1356+
err := serviceClient.Update(ctx, existingService)
1357+
Expect(err).NotTo(HaveOccurred())
1358+
1359+
proposedService.Spec.Type = corev1.ServiceTypeLoadBalancer
1360+
proposedService.Spec.Ports = []corev1.ServicePort{{Port: 80}}
1361+
proposedService.Spec.ExternalTrafficPolicy = corev1.ServiceExternalTrafficPolicyLocal
1362+
1363+
err = reconciler.serviceReconciler(ctx, &cluster, proposedService, true)
1364+
Expect(err).NotTo(HaveOccurred())
1365+
1366+
var updatedService corev1.Service
1367+
err = serviceClient.Get(ctx, types.NamespacedName{
1368+
Name: proposedService.Name,
1369+
Namespace: proposedService.Namespace,
1370+
}, &updatedService)
1371+
Expect(err).NotTo(HaveOccurred())
1372+
Expect(updatedService.Spec.ClusterIP).To(Equal("10.96.0.1"))
1373+
Expect(updatedService.Spec.Ports[0].NodePort).To(Equal(int32(31234)))
1374+
Expect(updatedService.Spec.Ports[0].Protocol).To(Equal(corev1.ProtocolTCP))
1375+
Expect(updatedService.Spec.Ports[0].TargetPort).To(Equal(intstr.FromInt32(80)))
1376+
Expect(updatedService.Spec.HealthCheckNodePort).To(Equal(int32(30000)))
1377+
Expect(updatedService.Spec.IPFamilies).To(Equal([]corev1.IPFamily{corev1.IPv4Protocol}))
1378+
Expect(updatedService.Spec.IPFamilyPolicy).To(Equal(&singleStack))
1379+
Expect(updatedService.Spec.InternalTrafficPolicy).To(Equal(&clusterPolicy))
1380+
Expect(updatedService.Spec.SessionAffinity).To(Equal(corev1.ServiceAffinityNone))
1381+
})
1382+
13091383
It("should preserve existing labels and annotations added by third parties", func() {
13101384
existingService := proposedService.DeepCopy()
13111385
existingService.Labels = map[string]string{"custom-label": "value"}

‎internal/controller/pooler_update.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333

3434
apiv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"
3535
"github.com/cloudnative-pg/cloudnative-pg/internal/configuration"
36+
"github.com/cloudnative-pg/cloudnative-pg/pkg/servicespec"
3637
"github.com/cloudnative-pg/cloudnative-pg/pkg/specs/pgbouncer"
3738
"github.com/cloudnative-pg/cloudnative-pg/pkg/utils"
3839
)
@@ -149,6 +150,7 @@ func (r *PoolerReconciler) reconcileService(
149150

150151
patchedService := resources.Service.DeepCopy()
151152
patchedService.Spec = expectedService.Spec
153+
servicespec.PreserveKubernetesDefaults(&patchedService.Spec, &resources.Service.Spec)
152154
utils.MergeObjectsMetadata(patchedService, expectedService)
153155

154156
if reflect.DeepEqual(patchedService.ObjectMeta, resources.Service.ObjectMeta) &&

‎pkg/servicespec/defaults.go‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
Copyright © contributors to CloudNativePG, established as
3+
CloudNativePG a Series of LF Projects, LLC.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
17+
SPDX-License-Identifier: Apache-2.0
18+
*/
19+
20+
package servicespec
21+
22+
import (
23+
"slices"
24+
25+
corev1 "k8s.io/api/core/v1"
26+
"k8s.io/apimachinery/pkg/util/intstr"
27+
)
28+
29+
// PreserveKubernetesDefaults copies Kubernetes-managed fields from the living
30+
// service spec into the proposed one, so that a DeepEqual comparison only
31+
// detects changes the operator actually controls.
32+
func PreserveKubernetesDefaults(proposed, living *corev1.ServiceSpec) {
33+
// Always assigned by Kubernetes
34+
proposed.ClusterIP = living.ClusterIP
35+
proposed.ClusterIPs = slices.Clone(living.ClusterIPs)
36+
37+
// Defaulted at creation time
38+
if len(proposed.IPFamilies) == 0 {
39+
proposed.IPFamilies = slices.Clone(living.IPFamilies)
40+
}
41+
if proposed.IPFamilyPolicy == nil {
42+
proposed.IPFamilyPolicy = living.IPFamilyPolicy
43+
}
44+
if proposed.InternalTrafficPolicy == nil {
45+
proposed.InternalTrafficPolicy = living.InternalTrafficPolicy
46+
}
47+
if proposed.SessionAffinity == "" {
48+
proposed.SessionAffinity = living.SessionAffinity
49+
}
50+
if proposed.SessionAffinityConfig == nil {
51+
proposed.SessionAffinityConfig = living.SessionAffinityConfig
52+
}
53+
if proposed.ExternalTrafficPolicy == "" {
54+
proposed.ExternalTrafficPolicy = living.ExternalTrafficPolicy
55+
}
56+
if proposed.HealthCheckNodePort == 0 {
57+
proposed.HealthCheckNodePort = living.HealthCheckNodePort
58+
}
59+
if proposed.AllocateLoadBalancerNodePorts == nil {
60+
proposed.AllocateLoadBalancerNodePorts = living.AllocateLoadBalancerNodePorts
61+
}
62+
if proposed.TrafficDistribution == nil {
63+
proposed.TrafficDistribution = living.TrafficDistribution
64+
}
65+
66+
preservePortDefaults(proposed.Ports, living.Ports)
67+
}
68+
69+
// preservePortDefaults preserves Kubernetes-defaulted and Kubernetes-assigned
70+
// fields in service ports, matching by the strategic merge key (Port, Protocol).
71+
func preservePortDefaults(proposed, living []corev1.ServicePort) {
72+
type portKey struct {
73+
port int32
74+
protocol corev1.Protocol
75+
}
76+
key := func(p corev1.ServicePort) portKey {
77+
protocol := p.Protocol
78+
if protocol == "" {
79+
protocol = corev1.ProtocolTCP
80+
}
81+
return portKey{port: p.Port, protocol: protocol}
82+
}
83+
84+
livingPorts := make(map[portKey]*corev1.ServicePort, len(living))
85+
for i := range living {
86+
livingPorts[key(living[i])] = &living[i]
87+
}
88+
89+
for i := range proposed {
90+
lp, ok := livingPorts[key(proposed[i])]
91+
if !ok {
92+
continue
93+
}
94+
if proposed[i].Protocol == "" {
95+
proposed[i].Protocol = lp.Protocol
96+
}
97+
if proposed[i].TargetPort == (intstr.IntOrString{}) {
98+
proposed[i].TargetPort = lp.TargetPort
99+
}
100+
if proposed[i].NodePort == 0 {
101+
proposed[i].NodePort = lp.NodePort
102+
}
103+
}
104+
}

0 commit comments

Comments
 (0)