Skip to content

Commit c8d017f

Browse files
committed
Update service when possible to preserve IP
1 parent 4f4bb7e commit c8d017f

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

pkg/deployment/resources/services.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
package resources
2424

2525
import (
26+
"strings"
2627
"time"
2728

2829
v1 "k8s.io/api/core/v1"
@@ -143,7 +144,9 @@ func (r *Resources) ensureExternalAccessServices(svcs k8sutil.ServiceInterface,
143144
eaServiceType := spec.GetType().AsServiceType() // Note: Type auto defaults to ServiceTypeLoadBalancer
144145
if existing, err := svcs.Get(eaServiceName, metav1.GetOptions{}); err == nil {
145146
// External access service exists
147+
updateExternalAccessService := false
146148
loadBalancerIP := spec.GetLoadBalancerIP()
149+
loadBalancerSourceRanges := spec.LoadBalancerSourceRanges
147150
nodePort := spec.GetNodePort()
148151
if spec.GetType().IsNone() {
149152
if noneIsClusterIP {
@@ -179,12 +182,22 @@ func (r *Resources) ensureExternalAccessServices(svcs k8sutil.ServiceInterface,
179182
deleteExternalAccessService = true // Remove the current and replace with proper one
180183
createExternalAccessService = true
181184
}
185+
if strings.Join(existing.Spec.LoadBalancerSourceRanges, ",") != strings.Join(loadBalancerSourceRanges, ",") {
186+
updateExternalAccessService = true
187+
existing.Spec.LoadBalancerSourceRanges = loadBalancerSourceRanges
188+
}
182189
} else if spec.GetType().IsNodePort() {
183190
if existing.Spec.Type != v1.ServiceTypeNodePort || len(existing.Spec.Ports) != 1 || (nodePort != 0 && existing.Spec.Ports[0].NodePort != int32(nodePort)) {
184191
deleteExternalAccessService = true // Remove the current and replace with proper one
185192
createExternalAccessService = true
186193
}
187194
}
195+
if updateExternalAccessService && !createExternalAccessService && !deleteExternalAccessService {
196+
if _, err := svcs.Update(existing); err != nil {
197+
log.Debug().Err(err).Msgf("Failed to update %s external access service", title)
198+
return maskAny(err)
199+
}
200+
}
188201
} else if k8sutil.IsNotFound(err) {
189202
// External access service does not exist
190203
if !spec.GetType().IsNone() || noneIsClusterIP {

pkg/util/k8sutil/services.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
// ServiceInterface has methods to work with Service resources.
3737
type ServiceInterface interface {
3838
Create(*v1.Service) (*v1.Service, error)
39+
Update(*v1.Service) (*v1.Service, error)
3940
Delete(name string, options *metav1.DeleteOptions) error
4041
Get(name string, options metav1.GetOptions) (*v1.Service, error)
4142
}

pkg/util/k8sutil/services_cache.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ func (sc *servicesCache) Create(s *v1.Service) (*v1.Service, error) {
5858
return result, nil
5959
}
6060

61+
func (sc *servicesCache) Update(s *v1.Service) (*v1.Service, error) {
62+
sc.cache = nil
63+
result, err := sc.cli.Update(s)
64+
if err != nil {
65+
return nil, maskAny(err)
66+
}
67+
return result, nil
68+
}
69+
6170
func (sc *servicesCache) Delete(name string, options *metav1.DeleteOptions) error {
6271
sc.cache = nil
6372
if err := sc.cli.Delete(name, options); err != nil {

0 commit comments

Comments
 (0)