Skip to content

Commit 228d67b

Browse files
authored
on update of a LoadBalancer service, untag old ip addresses, if any exist (#111)
1 parent 924384b commit 228d67b

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

pkg/controllers/housekeeping/loadbalancer.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ import (
99
)
1010

1111
const (
12-
syncLoadBalancerInterval = 1 * time.Minute
13-
syncLoadBalancerMinimalInternval = 5 * time.Second
12+
syncLoadBalancerInterval = 1 * time.Minute
13+
syncLoadBalancerMinimalInterval = 5 * time.Second
1414
)
1515

1616
func (h *Housekeeper) startLoadBalancerConfigSynching() {
1717
go h.ticker.Start("load balancer syncher", syncLoadBalancerInterval, h.stop, h.updateLoadBalancerConfig)
1818
}
1919

2020
func (h *Housekeeper) updateLoadBalancerConfig() error {
21-
if time.Since(h.lastLoadBalancerConfigSync) < syncLoadBalancerMinimalInternval {
21+
if time.Since(h.lastLoadBalancerConfigSync) < syncLoadBalancerMinimalInterval {
2222
return nil
2323
}
2424
nodes, err := kubernetes.GetNodes(context.Background(), h.k8sClient)

pkg/controllers/loadbalancer/loadbalancer.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,22 @@ func (l *LoadBalancerController) EnsureLoadBalancer(ctx context.Context, cluster
107107
if err != nil {
108108
return nil, err
109109
}
110+
111+
serviceTag := tags.BuildClusterServiceFQNTag(l.clusterID, service.GetNamespace(), service.GetName())
112+
113+
// remove the service tag from other previous ip addresses in case the service had an ip address before
114+
ips, err := l.MetalService.FindProjectIPsWithTag(ctx, l.projectID, serviceTag)
115+
if err != nil {
116+
return nil, err
117+
}
118+
otherIPs := slices.DeleteFunc(ips, func(ip *models.V1IPResponse) bool {
119+
return ip.Ipaddress != nil && *ip.Ipaddress == fixedIP
120+
})
121+
err = l.removeServiceTagFromIPs(ctx, serviceTag, otherIPs)
122+
if err != nil {
123+
return nil, err
124+
}
125+
110126
newIP, err := l.useIPInCluster(ctx, *ip, l.clusterID, *service)
111127
if err != nil {
112128
klog.Errorf("could not associate fixed ip:%s, err: %v", fixedIP, err)
@@ -220,6 +236,20 @@ func (l *LoadBalancerController) EnsureLoadBalancerDeleted(ctx context.Context,
220236
return err
221237
}
222238

239+
err = l.removeServiceTagFromIPs(ctx, serviceTag, ips)
240+
if err != nil {
241+
return err
242+
}
243+
244+
// we do not update the metallb config here because then the metallb controller will report a stale config
245+
// this is because the service gets deleted after updating the metallb config map
246+
//
247+
// therefore, we let the housekeeping update the config map
248+
249+
return nil
250+
}
251+
252+
func (l *LoadBalancerController) removeServiceTagFromIPs(ctx context.Context, serviceTag string, ips []*models.V1IPResponse) error {
223253
for _, ip := range ips {
224254
ip := ip
225255
err := retrygo.Do(
@@ -254,12 +284,6 @@ func (l *LoadBalancerController) EnsureLoadBalancerDeleted(ctx context.Context,
254284
return err
255285
}
256286
}
257-
258-
// we do not update the metallb config here because then the metallb controller will report a stale config
259-
// this is because the service gets deleted after updating the metallb config map
260-
//
261-
// therefore, we let the housekeeping update the config map
262-
263287
return nil
264288
}
265289

0 commit comments

Comments
 (0)