Skip to content

Commit b90abc5

Browse files
committed
fix: skip gw IP check for DPU and improve gateway initialization readability
Skip GW IP check in case ovnkube is running in DPU mode. Extract interface address logic into dedicated helper function to improve code readability and maintainability in gateway initialization. - Add getInterfaceAddressesForNodeMode() helper function - Replace conditional logic with switch statement for better extensibility - Simplify initGatewayPreStart() by removing duplicate error handling - Improve code organization and reduce cognitive complexity Signed-off-by: Alin Gabriel Serdean <[email protected]> Signed-off-by: Alin Serdean <[email protected]>
1 parent 45bf0b3 commit b90abc5

File tree

1 file changed

+35
-37
lines changed

1 file changed

+35
-37
lines changed

go-controller/pkg/node/gateway_init.go

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
"github.com/vishvananda/netlink"
1111

12-
corev1 "k8s.io/api/core/v1"
1312
"k8s.io/apimachinery/pkg/util/sets"
1413
"k8s.io/klog/v2"
1514
utilnet "k8s.io/utils/net"
@@ -188,6 +187,39 @@ func configureSvcRouteViaInterface(routeManager *routemanager.Controller, iface
188187
return nil
189188
}
190189

190+
// getNodePrimaryIfAddrs returns the appropriate interface addresses based on the node mode
191+
func getNodePrimaryIfAddrs(watchFactory factory.NodeWatchFactory, nodeName string, gatewayIntf string) ([]*net.IPNet, error) {
192+
switch config.OvnKubeNode.Mode {
193+
case types.NodeModeDPU:
194+
// For DPU mode, use the host IP address from node annotation
195+
node, err := watchFactory.GetNode(nodeName)
196+
if err != nil {
197+
return nil, fmt.Errorf("error retrieving node %s: %v", nodeName, err)
198+
}
199+
200+
// Extract the primary DPU address annotation from the node
201+
nodeIfAddr, err := util.GetNodePrimaryDPUHostAddrAnnotation(node)
202+
if err != nil {
203+
return nil, err
204+
}
205+
206+
if nodeIfAddr.IPv4 == "" {
207+
return nil, fmt.Errorf("node primary DPU address annotation is empty for node %s", nodeName)
208+
}
209+
210+
nodeIP, nodeAddrs, err := net.ParseCIDR(nodeIfAddr.IPv4)
211+
if err != nil {
212+
return nil, fmt.Errorf("failed to parse node IP address %s: %v", nodeIfAddr.IPv4, err)
213+
}
214+
215+
nodeAddrs.IP = nodeIP
216+
return []*net.IPNet{nodeAddrs}, nil
217+
default:
218+
// For other modes, get network interface IP addresses directly
219+
return nodeutil.GetNetworkInterfaceIPAddresses(gatewayIntf)
220+
}
221+
}
222+
191223
// initGatewayPreStart executes the first part of the gateway initialization for the node.
192224
// It creates the gateway object, the node IP manager, openflow manager and node port watcher
193225
// once OVN controller is ready and the patch port exists for this node.
@@ -215,46 +247,12 @@ func (nc *DefaultNodeNetworkController) initGatewayPreStart(
215247
egressGWInterface = interfaceForEXGW(config.Gateway.EgressGWInterface)
216248
}
217249

218-
ifAddrs, err = nodeutil.GetNetworkInterfaceIPAddresses(gatewayIntf)
250+
// Get interface addresses based on node mode
251+
ifAddrs, err = getNodePrimaryIfAddrs(nc.watchFactory, nc.name, gatewayIntf)
219252
if err != nil {
220253
return nil, err
221254
}
222255

223-
// For DPU mode, we need to use the host IP address which is stored as a Kubernetes
224-
// node annotation rather than using the gateway interface IP addresses.
225-
if config.OvnKubeNode.Mode == types.NodeModeDPU {
226-
// Retrieve the current node object from the Kubernetes API
227-
var node *corev1.Node
228-
if node, err = nc.watchFactory.GetNode(nc.name); err != nil {
229-
return nil, fmt.Errorf("error retrieving node %s: %v", nc.name, err)
230-
}
231-
232-
// Extract the primary DPU address annotation from the node
233-
nodeIfAddr, err := util.GetNodePrimaryDPUHostAddrAnnotation(node)
234-
if err != nil {
235-
return nil, err
236-
}
237-
// For DPU mode, we only support IPv4 for now.
238-
nodeAddrStr := nodeIfAddr.IPv4
239-
if nodeAddrStr == "" {
240-
return nil, fmt.Errorf("node primary DPU address annotation is empty for node %s", nc.name)
241-
}
242-
243-
// Parse the IPv4 address string into IP and network components
244-
nodeIP, nodeAddrs, err := net.ParseCIDR(nodeAddrStr)
245-
if err != nil {
246-
return nil, fmt.Errorf("failed to parse node IP address %s: %v", nodeAddrStr, err)
247-
}
248-
249-
// Set the parsed IP as the network address
250-
nodeAddrs.IP = nodeIP
251-
252-
// Create a new slice and replace ifAddrs with the DPU host address
253-
// This overrides the gateway interface addresses for DPU mode
254-
var gwIps []*net.IPNet
255-
ifAddrs = append(gwIps, nodeAddrs)
256-
}
257-
258256
if err := util.SetNodePrimaryIfAddrs(nodeAnnotator, ifAddrs); err != nil {
259257
klog.Errorf("Unable to set primary IP net label on node, err: %v", err)
260258
}

0 commit comments

Comments
 (0)