Skip to content

Commit f54436c

Browse files
committed
[gateway] move gw router and its port creation to functions.
Localize joinSwitch-related name fetching in methods Signed-off-by: Nadia Pinaeva <[email protected]>
1 parent 6e38032 commit f54436c

File tree

1 file changed

+92
-70
lines changed

1 file changed

+92
-70
lines changed

go-controller/pkg/ovn/gateway.go

Lines changed: 92 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -310,67 +310,39 @@ func (gw *GatewayManager) createGWRouter(l3GatewayConfig *util.L3GatewayConfig,
310310
return &gwRouter, nil
311311
}
312312

313-
// GatewayInit creates a gateway router for the local chassis.
314-
// enableGatewayMTU enables options:gateway_mtu for gateway routers.
315-
func (gw *GatewayManager) GatewayInit(
316-
nodeName string,
317-
clusterIPSubnet []*net.IPNet,
318-
hostSubnets []*net.IPNet,
319-
l3GatewayConfig *util.L3GatewayConfig,
320-
gwLRPJoinIPs, drLRPIfAddrs []*net.IPNet,
321-
externalIPs []net.IP,
322-
enableGatewayMTU bool,
323-
) error {
324-
325-
// If l3gatewayAnnotation.IPAddresses changed, we need to update the perPodSNATs,
326-
// so let's save the old value before we update the router for later use
327-
var oldExtIPs []net.IP
328-
oldLogicalRouter, err := libovsdbops.GetLogicalRouter(gw.nbClient,
329-
&nbdb.LogicalRouter{
330-
Name: gw.gwRouterName,
331-
})
332-
if err != nil && !errors.Is(err, libovsdbclient.ErrNotFound) {
333-
return fmt.Errorf("failed in retrieving %s, error: %v", gw.gwRouterName, err)
334-
}
335-
336-
if oldLogicalRouter != nil && oldLogicalRouter.ExternalIDs != nil {
337-
if physicalIPs, ok := oldLogicalRouter.ExternalIDs["physical_ips"]; ok {
338-
oldExternalIPs := strings.Split(physicalIPs, ",")
339-
oldExtIPs = make([]net.IP, len(oldExternalIPs))
340-
for i, oldExternalIP := range oldExternalIPs {
341-
cidr := oldExternalIP + util.GetIPFullMaskString(oldExternalIP)
342-
ip, _, err := net.ParseCIDR(cidr)
343-
if err != nil {
344-
return fmt.Errorf("invalid cidr:%s error: %v", cidr, err)
345-
}
346-
oldExtIPs[i] = ip
347-
}
348-
}
349-
}
350-
351-
gwRouter, err := gw.createGWRouter(l3GatewayConfig, gwLRPJoinIPs)
352-
if err != nil {
353-
return err
313+
func (gw *GatewayManager) getGWRouterPeerPortName() string {
314+
// In Layer2 networks there is no join switch and the gw.joinSwitchName points to the cluster switch.
315+
// Ensure that the ports are named appropriately, this is important for the logical router policies
316+
// created for local node access.
317+
// TODO(kyrtapz): Clean this up for clarity as part of https://github.com/ovn-org/ovn-kubernetes/issues/4689
318+
if gw.netInfo.TopologyType() == types.Layer2Topology {
319+
return types.SwitchToRouterPrefix + gw.joinSwitchName
354320
}
355321

356-
gwSwitchPort := types.JoinSwitchToGWRouterPrefix + gw.gwRouterName
357-
gwRouterPort := types.GWRouterToJoinSwitchPrefix + gw.gwRouterName
322+
return types.JoinSwitchToGWRouterPrefix + gw.gwRouterName
323+
}
358324

325+
func (gw *GatewayManager) getGWRouterPortName() string {
359326
// In Layer2 networks there is no join switch and the gw.joinSwitchName points to the cluster switch.
360327
// Ensure that the ports are named appropriately, this is important for the logical router policies
361328
// created for local node access.
362329
// TODO(kyrtapz): Clean this up for clarity as part of https://github.com/ovn-org/ovn-kubernetes/issues/4689
363330
if gw.netInfo.TopologyType() == types.Layer2Topology {
364-
gwSwitchPort = types.SwitchToRouterPrefix + gw.joinSwitchName
365-
gwRouterPort = types.RouterToSwitchPrefix + gw.joinSwitchName
331+
return types.RouterToSwitchPrefix + gw.joinSwitchName
366332
}
333+
return types.GWRouterToJoinSwitchPrefix + gw.gwRouterName
334+
}
335+
336+
func (gw *GatewayManager) createGWRouterPeerPort(nodeName string) error {
337+
gwSwitchPort := gw.getGWRouterPeerPortName()
338+
gwRouterPortName := gw.getGWRouterPortName()
367339

368340
logicalSwitchPort := nbdb.LogicalSwitchPort{
369341
Name: gwSwitchPort,
370342
Type: "router",
371343
Addresses: []string{"router"},
372344
Options: map[string]string{
373-
"router-port": gwRouterPort,
345+
"router-port": gwRouterPortName,
374346
},
375347
}
376348
if gw.netInfo.IsSecondary() {
@@ -397,11 +369,15 @@ func (gw *GatewayManager) GatewayInit(
397369
logicalSwitchPort.Options["requested-tnl-key"] = strconv.Itoa(tunnelID)
398370
}
399371
sw := nbdb.LogicalSwitch{Name: gw.joinSwitchName}
400-
err = libovsdbops.CreateOrUpdateLogicalSwitchPortsOnSwitch(gw.nbClient, &sw, &logicalSwitchPort)
372+
err := libovsdbops.CreateOrUpdateLogicalSwitchPortsOnSwitch(gw.nbClient, &sw, &logicalSwitchPort)
401373
if err != nil {
402374
return fmt.Errorf("failed to create port %v on logical switch %q: %v", gwSwitchPort, sw.Name, err)
403375
}
376+
return err
377+
}
404378

379+
func (gw *GatewayManager) createGWRouterPort(hostSubnets []*net.IPNet, gwLRPJoinIPs []*net.IPNet,
380+
enableGatewayMTU bool, gwRouter *nbdb.LogicalRouter) ([]net.IP, error) {
405381
gwLRPIPs := make([]net.IP, 0)
406382
gwLRPNetworks := []string{}
407383
for _, gwLRPJoinIP := range gwLRPJoinIPs {
@@ -426,38 +402,94 @@ func (gw *GatewayManager) GatewayInit(
426402
"gateway_mtu": strconv.Itoa(config.Default.MTU),
427403
}
428404
}
429-
logicalRouterPort := nbdb.LogicalRouterPort{
430-
Name: gwRouterPort,
405+
406+
gwRouterPort := nbdb.LogicalRouterPort{
407+
Name: gw.getGWRouterPortName(),
431408
MAC: gwLRPMAC.String(),
432409
Networks: gwLRPNetworks,
433410
Options: options,
434411
}
435412
if gw.netInfo.IsSecondary() {
436-
logicalRouterPort.ExternalIDs = map[string]string{
413+
gwRouterPort.ExternalIDs = map[string]string{
437414
types.NetworkExternalID: gw.netInfo.GetNetworkName(),
438415
types.TopologyExternalID: gw.netInfo.TopologyType(),
439416
}
440417
_, isNetIPv6 := gw.netInfo.IPMode()
441418
if gw.netInfo.TopologyType() == types.Layer2Topology && isNetIPv6 && config.IPv6Mode {
442-
logicalRouterPort.Ipv6RaConfigs = map[string]string{
419+
gwRouterPort.Ipv6RaConfigs = map[string]string{
443420
"address_mode": "dhcpv6_stateful",
444421
"send_periodic": "true",
445422
"max_interval": "900", // 15 minutes
446423
"min_interval": "300", // 5 minutes
447424
"router_preference": "LOW", // The static gateway configured by CNI is MEDIUM, so make this SLOW so it has less effect for pods
448425
}
449426
if gw.netInfo.MTU() > 0 {
450-
logicalRouterPort.Ipv6RaConfigs["mtu"] = fmt.Sprintf("%d", gw.netInfo.MTU())
427+
gwRouterPort.Ipv6RaConfigs["mtu"] = fmt.Sprintf("%d", gw.netInfo.MTU())
451428
}
452429
}
453430
}
454431

455-
err = libovsdbops.CreateOrUpdateLogicalRouterPort(gw.nbClient, gwRouter,
456-
&logicalRouterPort, nil, &logicalRouterPort.MAC, &logicalRouterPort.Networks,
457-
&logicalRouterPort.Options)
432+
err := libovsdbops.CreateOrUpdateLogicalRouterPort(gw.nbClient, gwRouter,
433+
&gwRouterPort, nil, &gwRouterPort.MAC, &gwRouterPort.Networks,
434+
&gwRouterPort.Options)
458435
if err != nil {
459-
return fmt.Errorf("failed to create port %+v on router %+v: %v", logicalRouterPort, gwRouter, err)
436+
return nil, fmt.Errorf("failed to create port %+v on router %+v: %v", gwRouterPort, gwRouter, err)
460437
}
438+
return gwLRPIPs, nil
439+
}
440+
441+
// GatewayInit creates a gateway router for the local chassis.
442+
// enableGatewayMTU enables options:gateway_mtu for gateway routers.
443+
func (gw *GatewayManager) GatewayInit(
444+
nodeName string,
445+
clusterIPSubnet []*net.IPNet,
446+
hostSubnets []*net.IPNet,
447+
l3GatewayConfig *util.L3GatewayConfig,
448+
gwLRPJoinIPs, drLRPIfAddrs []*net.IPNet,
449+
externalIPs []net.IP,
450+
enableGatewayMTU bool,
451+
) error {
452+
453+
// If l3gatewayAnnotation.IPAddresses changed, we need to update the perPodSNATs,
454+
// so let's save the old value before we update the router for later use
455+
var oldExtIPs []net.IP
456+
oldLogicalRouter, err := libovsdbops.GetLogicalRouter(gw.nbClient,
457+
&nbdb.LogicalRouter{
458+
Name: gw.gwRouterName,
459+
})
460+
if err != nil && !errors.Is(err, libovsdbclient.ErrNotFound) {
461+
return fmt.Errorf("failed in retrieving %s, error: %v", gw.gwRouterName, err)
462+
}
463+
464+
if oldLogicalRouter != nil && oldLogicalRouter.ExternalIDs != nil {
465+
if physicalIPs, ok := oldLogicalRouter.ExternalIDs["physical_ips"]; ok {
466+
oldExternalIPs := strings.Split(physicalIPs, ",")
467+
oldExtIPs = make([]net.IP, len(oldExternalIPs))
468+
for i, oldExternalIP := range oldExternalIPs {
469+
cidr := oldExternalIP + util.GetIPFullMaskString(oldExternalIP)
470+
ip, _, err := net.ParseCIDR(cidr)
471+
if err != nil {
472+
return fmt.Errorf("invalid cidr:%s error: %v", cidr, err)
473+
}
474+
oldExtIPs[i] = ip
475+
}
476+
}
477+
}
478+
479+
gwRouter, err := gw.createGWRouter(l3GatewayConfig, gwLRPJoinIPs)
480+
if err != nil {
481+
return err
482+
}
483+
484+
if err = gw.createGWRouterPeerPort(nodeName); err != nil {
485+
return err
486+
}
487+
488+
gwLRPIPs, err := gw.createGWRouterPort(hostSubnets, gwLRPJoinIPs, enableGatewayMTU, gwRouter)
489+
if err != nil {
490+
return err
491+
}
492+
461493
if len(drLRPIfAddrs) > 0 {
462494
for _, entry := range clusterIPSubnet {
463495
drLRPIfAddr, err := util.MatchFirstIPNetFamily(utilnet.IsIPv6CIDR(entry), drLRPIfAddrs)
@@ -525,9 +557,6 @@ func (gw *GatewayManager) GatewayInit(
525557
}
526558

527559
externalRouterPort := types.GWRouterToExtSwitchPrefix + gw.gwRouterName
528-
529-
nextHops := l3GatewayConfig.NextHops
530-
531560
// Remove stale OVN resources with any old masquerade IP
532561
if err := deleteStaleMasqueradeResources(gw.nbClient, gw.gwRouterName, nodeName, gw.watchFactory); err != nil {
533562
return fmt.Errorf("failed to remove stale masquerade resources from northbound database: %w", err)
@@ -565,6 +594,8 @@ func (gw *GatewayManager) GatewayInit(
565594
return fmt.Errorf("error creating service static route %+v in GR %s: %v", lrsr, gw.gwRouterName, err)
566595
}
567596
}
597+
598+
nextHops := l3GatewayConfig.NextHops
568599
// Add default gateway routes in GR
569600
for _, nextHop := range nextHops {
570601
var allIPs string
@@ -1078,17 +1109,8 @@ func (gw *GatewayManager) Cleanup() error {
10781109
// Get the gateway router port's IP address (connected to join switch)
10791110
var nextHops []net.IP
10801111

1081-
gwRouterToJoinSwitchPortName := types.GWRouterToJoinSwitchPrefix + gw.gwRouterName
1082-
portName := types.JoinSwitchToGWRouterPrefix + gw.gwRouterName
1083-
1084-
// In Layer2 networks there is no join switch and the gw.joinSwitchName points to the cluster switch.
1085-
// Ensure that the ports are named appropriately, this is important for the logical router policies
1086-
// created for local node access.
1087-
// TODO(kyrtapz): Clean this up for clarity as part of https://github.com/ovn-org/ovn-kubernetes/issues/4689
1088-
if gw.netInfo.TopologyType() == types.Layer2Topology {
1089-
gwRouterToJoinSwitchPortName = types.RouterToSwitchPrefix + gw.joinSwitchName
1090-
portName = types.SwitchToRouterPrefix + gw.joinSwitchName
1091-
}
1112+
gwRouterToJoinSwitchPortName := gw.getGWRouterPortName()
1113+
portName := gw.getGWRouterPeerPortName()
10921114

10931115
gwIPAddrs, err := libovsdbutil.GetLRPAddrs(gw.nbClient, gwRouterToJoinSwitchPortName)
10941116
if err != nil && !errors.Is(err, libovsdbclient.ErrNotFound) {

0 commit comments

Comments
 (0)