From 76c0bbd38ed848b2fcb7a47effeeda5a97750555 Mon Sep 17 00:00:00 2001 From: Alexey Makhov Date: Wed, 2 Oct 2024 15:00:01 +0300 Subject: [PATCH] Fix IP address detection for join token generation Signed-off-by: Alexey Makhov --- .../controlplane_bootstrap_controller.go | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/internal/controller/bootstrap/controlplane_bootstrap_controller.go b/internal/controller/bootstrap/controlplane_bootstrap_controller.go index 7df38e663..949e77332 100644 --- a/internal/controller/bootstrap/controlplane_bootstrap_controller.go +++ b/internal/controller/bootstrap/controlplane_bootstrap_controller.go @@ -20,7 +20,7 @@ import ( "context" "errors" "fmt" - "net" + "net/netip" "sort" "strings" "time" @@ -603,12 +603,17 @@ func (c *ControlPlaneController) findFirstControllerIP(ctx context.Context, firs break } if addr.Type == clusterv1.MachineInternalIP { - ip := net.ParseIP(addr.Address) - if len(ip) == net.IPv4len { - intIPv4Addr = ip.To4().String() + ip, err := netip.ParseAddr(addr.Address) + if err != nil { + continue + } + if ip.Is4() { + intIPv4Addr = ip.String() break } - intAddr = fmt.Sprintf("[%s]", ip.To16().String()) + if ip.Is6() { + intAddr = fmt.Sprintf("[%s]", ip.WithZone("").String()) + } } } @@ -632,12 +637,17 @@ func (c *ControlPlaneController) findFirstControllerIP(ctx context.Context, firs break } if addrMap["type"] == string(v1.NodeInternalIP) { - ip := net.ParseIP(addrMap["address"].(string)) - if len(ip) == net.IPv4len { - intIPv4Addr = ip.To4().String() + ip, err := netip.ParseAddr(addrMap["address"].(string)) + if err != nil { + continue + } + if ip.Is4() { + intIPv4Addr = ip.String() break } - intAddr = fmt.Sprintf("[%s]", ip.To16().String()) + if ip.Is6() { + intAddr = fmt.Sprintf("[%s]", ip.WithZone("").String()) + } } } }