Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 33 additions & 18 deletions pkg/ovs/ovn-nb-load_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,25 +531,40 @@ func (c *OVNNbClient) LoadBalancerDeleteIPPortMapping(lbName, vipEndpoint string

// LoadBalancerUpdateIPPortMapping update load balancer ip port mapping
func (c *OVNNbClient) LoadBalancerUpdateIPPortMapping(lbName, vipEndpoint string, ipPortMappings map[string]string) error {
if len(ipPortMappings) != 0 {
ops, err := c.LoadBalancerOp(
lbName,
func(lb *ovnnb.LoadBalancer) []model.Mutation {
return []model.Mutation{
{
Field: &lb.IPPortMappings,
Value: ipPortMappings,
Mutator: ovsdb.MutateOperationInsert,
},
ops, err := c.LoadBalancerOp(
lbName,
func(lb *ovnnb.LoadBalancer) []model.Mutation {
// Delete from the IPPortMappings any outdated mapping
mappingToDelete := make(map[string]string)
for portIP, portMapVip := range lb.IPPortMappings {
if _, ok := ipPortMappings[portIP]; !ok {
mappingToDelete[portIP] = portMapVip
}
},
)
if err != nil {
return fmt.Errorf("failed to generate operations when adding ip port mapping with vip %v to load balancers %s: %w", vipEndpoint, lbName, err)
}
if err = c.Transact("lb-add", ops); err != nil {
return fmt.Errorf("failed to add ip port mapping with vip %v to load balancers %s: %w", vipEndpoint, lbName, err)
}
}

if len(mappingToDelete) > 0 {
klog.Infof("deleting outdated entry from ipportmapping %v", mappingToDelete)
}

return []model.Mutation{
{
Field: &lb.IPPortMappings,
Value: mappingToDelete,
Mutator: ovsdb.MutateOperationDelete,
},
{
Field: &lb.IPPortMappings,
Value: ipPortMappings,
Mutator: ovsdb.MutateOperationInsert,
},
}
Comment on lines +549 to +560

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider optimizing by only adding mutations if they cause an actual change. If mappingToDelete is empty and ipPortMappings is effectively the same as lb.IPPortMappings, you could return nil from the mutationsFunc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this matters too much, there's other similiar constructs elsewhere, they don't really do much if values are empty

},
)
if err != nil {
return fmt.Errorf("failed to generate operations when adding ip port mapping with vip %v to load balancers %s: %w", vipEndpoint, lbName, err)
}
if err = c.Transact("lb-add", ops); err != nil {
return fmt.Errorf("failed to add ip port mapping with vip %v to load balancers %s: %w", vipEndpoint, lbName, err)
}
return nil
}
Expand Down
Loading