From 2d57e00d5d2c36d0ff8ea8462aad7fd763be2cb3 Mon Sep 17 00:00:00 2001 From: Wu zhengdong Date: Sun, 29 Aug 2021 00:16:06 +0800 Subject: [PATCH] Fix init node transport interface bug 1.Fix adding node routes crash bug, which caused by uninitialized DualStackIPs param. 2.Fix ipv4 address and ipv6 address confused bug and return the first ipv4 and ipv6 ips for transport interface. Signed-off-by: Wu zhengdong --- pkg/agent/controller/noderoute/node_route_controller.go | 2 +- pkg/agent/util/net.go | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/pkg/agent/controller/noderoute/node_route_controller.go b/pkg/agent/controller/noderoute/node_route_controller.go index 4d8ecf1e967..c2feea33eed 100644 --- a/pkg/agent/controller/noderoute/node_route_controller.go +++ b/pkg/agent/controller/noderoute/node_route_controller.go @@ -739,7 +739,7 @@ func getNodeMAC(node *corev1.Node) (net.HardwareAddr, error) { } func (c *Controller) getNodeTransportAddrs(node *corev1.Node) (*utilip.DualStackIPs, error) { - var transportAddrs *utilip.DualStackIPs + transportAddrs := &utilip.DualStackIPs{} if c.networkConfig.TransportIface != "" { transportAddrsStr := node.Annotations[types.NodeTransportAddressAnnotationKey] if transportAddrsStr != "" { diff --git a/pkg/agent/util/net.go b/pkg/agent/util/net.go index abaee70f683..940440ae7e9 100644 --- a/pkg/agent/util/net.go +++ b/pkg/agent/util/net.go @@ -33,6 +33,8 @@ const ( FamilyIPv4 uint8 = 4 FamilyIPv6 uint8 = 6 + // IP address lengths (bytes). + IPv6len = 16 ) func generateInterfaceName(key string, name string, useHead bool) string { @@ -165,9 +167,11 @@ func GetIPNetDeviceByName(ifaceName string) (v4IPNet *net.IPNet, v6IPNet *net.IP if ipNet, ok := addr.(*net.IPNet); ok { if ipNet.IP.IsGlobalUnicast() { if ipNet.IP.To4() != nil { + if v4IPNet == nil { + v4IPNet = ipNet + } + } else if v6IPNet == nil { v6IPNet = ipNet - } else { - v4IPNet = ipNet } } }