Skip to content

Commit

Permalink
add route table option in static route for subnet (#2748)
Browse files Browse the repository at this point in the history
* add route table option in static route for subnet

* change log message

Co-authored-by: bobz965 <jmdxjsjgcxy@gmail.com>

* empty string as main table name

---------

Co-authored-by: bobz965 <jmdxjsjgcxy@gmail.com>
  • Loading branch information
mingoooo and bobz965 committed May 5, 2023
1 parent f6414ce commit 86a07a3
Show file tree
Hide file tree
Showing 14 changed files with 333 additions and 114 deletions.
4 changes: 4 additions & 0 deletions charts/templates/kube-ovn-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,8 @@ spec:
type: string
bfdId:
type: string
routeTable:
type: string
type: object
type: array
policyRoutes:
Expand Down Expand Up @@ -1709,6 +1711,8 @@ spec:
type: boolean
enableEcmp:
type: boolean
routeTable:
type: string
scope: Cluster
names:
plural: subnets
Expand Down
4 changes: 4 additions & 0 deletions dist/images/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1451,6 +1451,8 @@ spec:
type: string
bfdId:
type: string
routeTable:
type: string
type: object
type: array
policyRoutes:
Expand Down Expand Up @@ -1930,6 +1932,8 @@ spec:
type: boolean
enableEcmp:
type: boolean
routeTable:
type: string
scope: Cluster
names:
plural: subnets
Expand Down
13 changes: 8 additions & 5 deletions pkg/apis/kubeovn/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ type SubnetSpec struct {
U2OInterconnection bool `json:"u2oInterconnection,omitempty"`
EnableLb *bool `json:"enableLb,omitempty"`
EnableEcmp bool `json:"enableEcmp,omitempty"`

RouteTable string `json:"routeTable,omitempty"`
}

type Acl struct {
Expand Down Expand Up @@ -393,11 +395,12 @@ const (
)

type StaticRoute struct {
Policy RoutePolicy `json:"policy,omitempty"`
CIDR string `json:"cidr"`
NextHopIP string `json:"nextHopIP"`
ECMPMode string `json:"ecmpMode"`
BfdId string `json:"bfdId"`
Policy RoutePolicy `json:"policy,omitempty"`
CIDR string `json:"cidr"`
NextHopIP string `json:"nextHopIP"`
ECMPMode string `json:"ecmpMode"`
BfdId string `json:"bfdId"`
RouteTable string `json:"routeTable"`
}

type PolicyRouteAction string
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ func (c *Controller) gcStaticRoute() error {
for _, route := range routes {
keepStaticRoute = false
for _, item := range defaultVpc.Spec.StaticRoutes {
if route.CIDR == item.CIDR && route.NextHop == item.NextHopIP {
if route.CIDR == item.CIDR && route.NextHop == item.NextHopIP && route.RouteTable == item.RouteTable {
keepStaticRoute = true
break
}
Expand All @@ -642,8 +642,8 @@ func (c *Controller) gcStaticRoute() error {
klog.Errorf("failed to get NatRule by LogicalIP %s, %v", route.CIDR, err)
continue
}
klog.Infof("gc static route %s %s %s", route.Policy, route.CIDR, route.NextHop)
if err := c.ovnLegacyClient.DeleteStaticRoute(route.CIDR, c.config.ClusterRouter); err != nil {
klog.Infof("gc static route %s %s %s %s", route.RouteTable, route.Policy, route.CIDR, route.NextHop)
if err := c.ovnLegacyClient.DeleteStaticRoute(route.CIDR, c.config.ClusterRouter, route.RouteTable); err != nil {
klog.Errorf("failed to delete stale route %s, %v", route.NextHop, err)
}
}
Expand Down
12 changes: 10 additions & 2 deletions pkg/controller/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -760,11 +760,19 @@ func (c *Controller) migrateNodeRoute(af int, node, ip, nexthop string) error {
klog.V(3).Infof("node policy route migrated")
return nil
}
if err := c.ovnLegacyClient.DeleteStaticRoute(ip, c.config.ClusterRouter); err != nil {
klog.Errorf("failed to delete obsolete static route for node %s: %v", node, err)

routeTables, err := c.ovnLegacyClient.GetRouteTables(c.config.ClusterRouter)
if err != nil {
return err
}

for rtb := range routeTables {
if err := c.ovnLegacyClient.DeleteStaticRoute(ip, c.config.ClusterRouter, rtb); err != nil {
klog.Errorf("failed to delete obsolete static route for node %s: %v", node, err)
return err
}
}

asName := nodeUnderlayAddressSetName(node, af)
obsoleteMatch := fmt.Sprintf("ip%d.dst == %s && ip%d.src != $%s", af, ip, af, asName)
klog.Infof("delete policy route for router: %s, priority: %d, match %s", c.config.ClusterRouter, util.NodeRouterPolicyPriority, obsoleteMatch)
Expand Down
10 changes: 6 additions & 4 deletions pkg/controller/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ func (c *Controller) checkGatewayReady() error {
return nil
}

func (c *Controller) checkRouteExist(nextHop, cidrBlock, routePolicy string) (bool, error) {
func (c *Controller) checkRouteExist(nextHop, cidrBlock, routePolicy, routeTable string) (bool, error) {
routes, err := c.ovnLegacyClient.GetStaticRouteList(c.config.ClusterRouter)
if err != nil {
klog.Errorf("failed to list static route %v", err)
Expand All @@ -858,7 +858,7 @@ func (c *Controller) checkRouteExist(nextHop, cidrBlock, routePolicy string) (bo
continue
}

if route.CIDR == cidrBlock && route.NextHop == nextHop {
if route.CIDR == cidrBlock && route.NextHop == nextHop && route.RouteTable == routeTable {
klog.V(3).Infof("static route exists for cidr %s, nexthop %v", cidrBlock, nextHop)
return true, nil
}
Expand Down Expand Up @@ -1052,15 +1052,17 @@ func (c *Controller) addNodeGwStaticRoute() error {
if util.CheckProtocol(cidrBlock) != util.CheckProtocol(nextHop) {
continue
}
exist, err := c.checkRouteExist(nextHop, cidrBlock, ovs.PolicyDstIP)
exist, err := c.checkRouteExist(nextHop, cidrBlock, ovs.PolicyDstIP, util.MainRouteTable)
if err != nil {
klog.Errorf("get static route for node gw error %v", err)
return err
}

if !exist {
klog.Infof("add static route for node gw")
if err := c.ovnLegacyClient.AddStaticRoute("", cidrBlock, nextHop, "", "", c.config.ClusterRouter, util.NormalRouteType); err != nil {
if err := c.ovnLegacyClient.AddStaticRoute(
"", cidrBlock, nextHop, "", "",
c.config.ClusterRouter, util.MainRouteTable, util.NormalRouteType); err != nil {
klog.Errorf("failed to add static route for node gw: %v", err)
return err
}
Expand Down
13 changes: 11 additions & 2 deletions pkg/controller/ovn-ic.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,10 +379,19 @@ func (c *Controller) delLearnedRoute() error {
klog.Errorf("number wrong of logical router for static route %s, %v", aLdPort["_uuid"][0], itsRouter)
return nil
}
if err := c.ovnLegacyClient.DeleteStaticRoute(aLdPort["ip_prefix"][0], itsRouter[0]["name"][0]); err != nil {
klog.Errorf("failed to delete stale route %s, %v", aLdPort["ip_prefix"][0], err)

rtbs, err := c.ovnLegacyClient.GetRouteTables(itsRouter[0]["name"][0])
if err != nil {
klog.Errorf("failed to list route tables of logical router %s, %v", itsRouter[0]["name"][0], err)
return err
}

for rtb := range rtbs {
if err := c.ovnLegacyClient.DeleteStaticRoute(aLdPort["ip_prefix"][0], itsRouter[0]["name"][0], rtb); err != nil {
klog.Errorf("failed to delete static route %s, %v", aLdPort["ip_prefix"][0], err)
return err
}
}
}
klog.V(5).Infof("finish removing learned routes")
}
Expand Down
15 changes: 10 additions & 5 deletions pkg/controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,9 @@ func (c *Controller) reconcileRouteSubnets(cachedPod, pod *v1.Pod, needRoutePodN
nextHop = strings.Split(nextHop, "/")[0]
}

if err := c.ovnLegacyClient.AddStaticRoute(ovs.PolicySrcIP, podIP, nextHop, "", "", c.config.ClusterRouter, util.NormalRouteType); err != nil {
if err := c.ovnLegacyClient.AddStaticRoute(
ovs.PolicySrcIP, podIP, nextHop, "", "",
c.config.ClusterRouter, subnet.Spec.RouteTable, util.NormalRouteType); err != nil {
klog.Errorf("failed to add static route, %v", err)
return err
}
Expand Down Expand Up @@ -779,13 +781,15 @@ func (c *Controller) reconcileRouteSubnets(cachedPod, pod *v1.Pod, needRoutePodN
}

if pod.Annotations[util.NorthGatewayAnnotation] != "" {
if err := c.ovnLegacyClient.AddStaticRoute(ovs.PolicySrcIP, podIP, pod.Annotations[util.NorthGatewayAnnotation],
"", "", c.config.ClusterRouter, util.NormalRouteType); err != nil {
if err := c.ovnLegacyClient.AddStaticRoute(
ovs.PolicySrcIP, podIP, pod.Annotations[util.NorthGatewayAnnotation], "", "",
c.config.ClusterRouter, subnet.Spec.RouteTable, util.NormalRouteType); err != nil {
klog.Errorf("failed to add static route, %v", err)
return err
}
} else if c.config.EnableEipSnat {
if err := c.ovnLegacyClient.DeleteStaticRoute(podIP, c.config.ClusterRouter); err != nil {
if err := c.ovnLegacyClient.DeleteStaticRoute(
podIP, c.config.ClusterRouter, subnet.Spec.RouteTable); err != nil {
return err
}
}
Expand Down Expand Up @@ -868,7 +872,8 @@ func (c *Controller) handleDeletePod(pod *v1.Pod) error {
}
// If pod has snat or eip, also need delete staticRoute when delete pod
if vpc.Name == util.DefaultVpc {
if err := c.ovnLegacyClient.DeleteStaticRoute(address.Ip, vpc.Name); err != nil {
if err := c.ovnLegacyClient.DeleteStaticRoute(
address.Ip, vpc.Name, subnet.Spec.RouteTable); err != nil {
return err
}
}
Expand Down
Loading

0 comments on commit 86a07a3

Please sign in to comment.