Skip to content

Commit

Permalink
libovsdb: cleanup ModelClient usage for router policies & routes
Browse files Browse the repository at this point in the history
Signed-off-by: Jaime Caamaño Ruiz <jcaamano@redhat.com>
  • Loading branch information
jcaamano committed Apr 19, 2022
1 parent 707c0ee commit 4e3c26a
Show file tree
Hide file tree
Showing 13 changed files with 806 additions and 941 deletions.
64 changes: 11 additions & 53 deletions go-controller/hybrid-overlay/pkg/controller/master.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package controller
import (
"fmt"
"net"
"strconv"
"sync"

libovsdbclient "github.com/ovn-org/libovsdb/client"
Expand Down Expand Up @@ -338,45 +337,21 @@ func (m *MasterController) setupHybridLRPolicySharedGw(nodeSubnets []*net.IPNet,
matchStr := fmt.Sprintf(`inport == "%s%s" && %s.dst == %s`,
ovntypes.RouterToSwitchPrefix, nodeName, L3Prefix, hybridCIDR)

intPriority, _ := strconv.Atoi(ovntypes.HybridOverlaySubnetPriority)

logicalRouter := nbdb.LogicalRouter{}
logicalRouterPolicy := nbdb.LogicalRouterPolicy{
Priority: intPriority,
Priority: ovntypes.HybridOverlaySubnetPriority,
ExternalIDs: map[string]string{
"name": ovntypes.HybridSubnetPrefix + nodeName,
},
Action: nbdb.LogicalRouterPolicyActionReroute,
Nexthops: []string{drIP.String()},
Match: matchStr,
}
logicalRouterPolicyRes := []nbdb.LogicalRouterPolicy{}
opModels := []libovsdbops.OperationModel{
{
Model: &logicalRouterPolicy,
ModelPredicate: func(lrp *nbdb.LogicalRouterPolicy) bool {
return lrp.Priority == intPriority &&
lrp.ExternalIDs["name"] == ovntypes.HybridSubnetPrefix+nodeName
},
OnModelUpdates: []interface{}{
&logicalRouterPolicy.Nexthops,
&logicalRouterPolicy.Match,
},
ExistingResult: &logicalRouterPolicyRes,
DoAfter: func() {
logicalRouter.Policies = []string{logicalRouterPolicy.UUID}
},
},
{
Model: &logicalRouter,
ModelPredicate: func(lr *nbdb.LogicalRouter) bool { return lr.Name == ovntypes.OVNClusterRouter },
OnModelMutations: []interface{}{
&logicalRouter.Policies,
},
ErrNotFound: true,
},
p := func(item *nbdb.LogicalRouterPolicy) bool {
return item.Priority == logicalRouterPolicy.Priority &&
item.ExternalIDs["name"] == logicalRouterPolicy.ExternalIDs["name"]
}
if _, err := m.modelClient.CreateOrUpdate(opModels...); err != nil {

if err := libovsdbops.CreateOrUpdateLogicalRouterPolicyWithPredicate(m.nbClient, ovntypes.OVNClusterRouter, &logicalRouterPolicy, p); err != nil {
return fmt.Errorf("failed to add policy route '%s' for host %q on %s , error: %v", matchStr, nodeName, ovntypes.OVNClusterRouter, err)
}
klog.Infof("Created hybrid overlay logical route policy for node %s", nodeName)
Expand All @@ -390,29 +365,12 @@ func (m *MasterController) setupHybridLRPolicySharedGw(nodeSubnets []*net.IPNet,
}

func (m *MasterController) removeHybridLRPolicySharedGW(nodeName string) error {
logicalRouter := nbdb.LogicalRouter{}
logicalRouterPolicyRes := []nbdb.LogicalRouterPolicy{}
opModels := []libovsdbops.OperationModel{
{
ModelPredicate: func(lrp *nbdb.LogicalRouterPolicy) bool {
return lrp.ExternalIDs["name"] == ovntypes.HybridSubnetPrefix+nodeName
},
ExistingResult: &logicalRouterPolicyRes,
DoAfter: func() {
logicalRouter.Policies = libovsdbops.ExtractUUIDsFromModels(&logicalRouterPolicyRes)
},
BulkOp: true,
},
{
Model: &logicalRouter,
ModelPredicate: func(lr *nbdb.LogicalRouter) bool { return lr.Name == ovntypes.OVNClusterRouter },
OnModelMutations: []interface{}{
&logicalRouter.Policies,
},
},
policyName := ovntypes.HybridSubnetPrefix + nodeName
p := func(item *nbdb.LogicalRouterPolicy) bool {
return item.ExternalIDs["name"] == policyName
}
if err := m.modelClient.Delete(opModels...); err != nil {
return fmt.Errorf("failed to delete policy from %s, error: %v", ovntypes.OVNClusterRouter, err)
if err := libovsdbops.DeleteLogicalRouterPoliciesWithPredicate(m.nbClient, ovntypes.OVNClusterRouter, p); err != nil {
return fmt.Errorf("failed to delete policy %s from %s, error: %v", policyName, ovntypes.OVNClusterRouter, err)
}
return nil
}

0 comments on commit 4e3c26a

Please sign in to comment.