Skip to content

Commit

Permalink
fix: resolve wireguard endpoint to IP
Browse files Browse the repository at this point in the history
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 #896.

Signed-off-by: Utku Ozdemir <utku.ozdemir@siderolabs.com>
(cherry picked from commit 4e714a1)
  • Loading branch information
utkuozdemir committed Jul 15, 2022
1 parent a5cd4e9 commit acba599
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion app/sidero-controller-manager/cmd/siderolink-manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit acba599

Please sign in to comment.