@@ -483,25 +483,40 @@ func (c *OVNNbClient) LoadBalancerDeleteIPPortMapping(lbName, vipEndpoint string
483
483
484
484
// LoadBalancerUpdateIPPortMapping update load balancer ip port mapping
485
485
func (c * OVNNbClient ) LoadBalancerUpdateIPPortMapping (lbName , vipEndpoint string , ipPortMappings map [string ]string ) error {
486
- if len (ipPortMappings ) != 0 {
487
- ops , err := c .LoadBalancerOp (
488
- lbName ,
489
- func (lb * ovnnb.LoadBalancer ) []model.Mutation {
490
- return []model.Mutation {
491
- {
492
- Field : & lb .IPPortMappings ,
493
- Value : ipPortMappings ,
494
- Mutator : ovsdb .MutateOperationInsert ,
495
- },
486
+ ops , err := c .LoadBalancerOp (
487
+ lbName ,
488
+ func (lb * ovnnb.LoadBalancer ) []model.Mutation {
489
+ // Delete from the IPPortMappings any outdated mapping
490
+ mappingToDelete := make (map [string ]string )
491
+ for portIP , portMapVip := range lb .IPPortMappings {
492
+ if _ , ok := ipPortMappings [portIP ]; ! ok {
493
+ mappingToDelete [portIP ] = portMapVip
496
494
}
497
- },
498
- )
499
- if err != nil {
500
- return fmt .Errorf ("failed to generate operations when adding ip port mapping with vip %v to load balancers %s: %v" , vipEndpoint , lbName , err )
501
- }
502
- if err = c .Transact ("lb-add" , ops ); err != nil {
503
- return fmt .Errorf ("failed to add ip port mapping with vip %v to load balancers %s: %v" , vipEndpoint , lbName , err )
504
- }
495
+ }
496
+
497
+ if len (mappingToDelete ) > 0 {
498
+ klog .Infof ("deleting outdated entry from ipportmapping %v" , mappingToDelete )
499
+ }
500
+
501
+ return []model.Mutation {
502
+ {
503
+ Field : & lb .IPPortMappings ,
504
+ Value : mappingToDelete ,
505
+ Mutator : ovsdb .MutateOperationDelete ,
506
+ },
507
+ {
508
+ Field : & lb .IPPortMappings ,
509
+ Value : ipPortMappings ,
510
+ Mutator : ovsdb .MutateOperationInsert ,
511
+ },
512
+ }
513
+ },
514
+ )
515
+ if err != nil {
516
+ return fmt .Errorf ("failed to generate operations when adding ip port mapping with vip %v to load balancers %s: %w" , vipEndpoint , lbName , err )
517
+ }
518
+ if err = c .Transact ("lb-add" , ops ); err != nil {
519
+ return fmt .Errorf ("failed to add ip port mapping with vip %v to load balancers %s: %w" , vipEndpoint , lbName , err )
505
520
}
506
521
return nil
507
522
}
0 commit comments