Skip to content

Commit 0f308f3

Browse files
committed
fix service dual stack add/del cluster ips not change ovn nb
1 parent 4a70bae commit 0f308f3

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

pkg/controller/service.go

+36-8
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,22 @@ func (c *Controller) enqueueUpdateService(old, new interface{}) {
108108
utilruntime.HandleError(err)
109109
return
110110
}
111+
112+
oldClusterIps := getVipIps(oldSvc)
113+
newClusterIps := getVipIps(newSvc)
114+
var ipsToDel []string
115+
for _, oldClusterIp := range oldClusterIps {
116+
if !util.ContainsString(newClusterIps, oldClusterIp) {
117+
ipsToDel = append(ipsToDel, oldClusterIp)
118+
}
119+
}
120+
111121
klog.V(3).Infof("enqueue update service %s", key)
122+
if len(ipsToDel) != 0 {
123+
ipsToDelStr := strings.Join(ipsToDel, ",")
124+
key = strings.Join([]string{key, ipsToDelStr}, "#")
125+
}
126+
112127
c.updateServiceQueue.Add(key)
113128
}
114129

@@ -269,6 +284,14 @@ func (c *Controller) handleDeleteService(service *vpcService) error {
269284
}
270285

271286
func (c *Controller) handleUpdateService(key string) error {
287+
keys := strings.Split(key, "#")
288+
key = keys[0]
289+
var ipsToDel []string
290+
if len(keys) == 2 {
291+
ipsToDelStr := keys[1]
292+
ipsToDel = strings.Split(ipsToDelStr, ",")
293+
}
294+
272295
namespace, name, err := cache.SplitMetaNamespaceKey(key)
273296
if err != nil {
274297
utilruntime.HandleError(fmt.Errorf("invalid resource key: %s", key))
@@ -283,12 +306,7 @@ func (c *Controller) handleUpdateService(key string) error {
283306
return err
284307
}
285308

286-
var ips []string
287-
if vip, ok := svc.Annotations[util.SwitchLBRuleVipsAnnotation]; ok {
288-
ips = strings.Split(vip, ",")
289-
} else {
290-
ips = util.ServiceClusterIPs(*svc)
291-
}
309+
ips := getVipIps(svc)
292310

293311
vpcName := svc.Annotations[util.VpcAnnotation]
294312
if vpcName == "" {
@@ -340,7 +358,7 @@ func (c *Controller) handleUpdateService(key string) error {
340358
}
341359
}
342360
for vip := range vips {
343-
if ip := parseVipAddr(vip); util.ContainsString(ips, ip) && !util.IsStringIn(vip, svcVips) {
361+
if ip := parseVipAddr(vip); (util.ContainsString(ips, ip) && !util.IsStringIn(vip, svcVips)) || util.ContainsString(ipsToDel, ip) {
344362
klog.Infof("remove stale vip %s from LB %s", vip, lb)
345363
if err = c.ovnLegacyClient.DeleteLoadBalancerVip(vip, lb); err != nil {
346364
klog.Errorf("failed to delete vip %s from LB %s: %v", vip, lb, err)
@@ -354,7 +372,7 @@ func (c *Controller) handleUpdateService(key string) error {
354372
}
355373
klog.V(3).Infof("existing vips of LB %s: %v", oLb, vips)
356374
for vip := range vips {
357-
if ip := parseVipAddr(vip); util.ContainsString(ips, ip) {
375+
if ip := parseVipAddr(vip); util.ContainsString(ips, ip) || util.ContainsString(ipsToDel, ip) {
358376
klog.Infof("remove stale vip %s from LB %s", vip, oLb)
359377
if err = c.ovnLegacyClient.DeleteLoadBalancerVip(vip, oLb); err != nil {
360378
klog.Errorf("failed to delete vip %s from LB %s: %v", vip, oLb, err)
@@ -473,3 +491,13 @@ func (c *Controller) handleAddService(key string) error {
473491

474492
return nil
475493
}
494+
495+
func getVipIps(svc *v1.Service) []string {
496+
var ips []string
497+
if vip, ok := svc.Annotations[util.SwitchLBRuleVipsAnnotation]; ok {
498+
ips = strings.Split(vip, ",")
499+
} else {
500+
ips = util.ServiceClusterIPs(*svc)
501+
}
502+
return ips
503+
}

0 commit comments

Comments
 (0)