From 4e714a1e4d657e01fcfe0f7a3e64ca58d6af89f5 Mon Sep 17 00:00:00 2001 From: Utku Ozdemir Date: Fri, 15 Jul 2022 13:30:04 +0200 Subject: [PATCH] fix: resolve wireguard endpoint to IP If wireguard endpoint is set to be a hostname and not an IP, attempt to resolve it to an IP instead of failing immediately. Closes siderolabs/sidero#896. Signed-off-by: Utku Ozdemir --- .../cmd/siderolink-manager/main.go | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/app/sidero-controller-manager/cmd/siderolink-manager/main.go b/app/sidero-controller-manager/cmd/siderolink-manager/main.go index 8173090e7..1b5f73244 100644 --- a/app/sidero-controller-manager/cmd/siderolink-manager/main.go +++ b/app/sidero-controller-manager/cmd/siderolink-manager/main.go @@ -96,7 +96,16 @@ func run() error { return fmt.Errorf("error listening for gRPC API: %w", err) } - siderolink.Cfg.WireguardEndpoint = fmt.Sprintf("%s:%d", wireguardEndpoint, wireguardPort) + wireguardEndpointIP, err := getIPForHost(wireguardEndpoint) + if err != nil { + return err + } + + if wireguardEndpoint != wireguardEndpointIP { + logger.Sugar().Infof("resolved wireguard endpoint %s to %s", wireguardEndpoint, wireguardEndpointIP) + } + + siderolink.Cfg.WireguardEndpoint = fmt.Sprintf("%s:%d", wireguardEndpointIP, wireguardPort) if err = siderolink.Cfg.LoadOrCreate(ctx, metalclient); err != nil { return err @@ -164,3 +173,21 @@ func run() error { return nil } + +func getIPForHost(host string) (string, error) { + parsedIP, err := netaddr.ParseIP(host) + if err == nil { + return parsedIP.String(), nil + } + + resolvedIPs, err := net.LookupIP(host) + if err != nil { + return "", err + } + + if len(resolvedIPs) == 0 { + return "", fmt.Errorf("no IPs found for %s", host) + } + + return resolvedIPs[0].String(), nil +}