@@ -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