Created
April 23, 2022 14:48
-
-
Save stevehenderson/36420085765057f0824d99653ef84bca to your computer and use it in GitHub Desktop.
Replace K8s namespace with IP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Looks up a host using the resolver and returns an IP. | |
// Utility function for testing against K8s over VPN. | |
// Resolver should be the POD ip of kube-dns | |
// | |
func hostToIP(host string, resolver string) string { | |
fmt.Printf("Looking up %s using resolver %s\n", host, resolver) | |
r := &net.Resolver{ | |
PreferGo: true, | |
Dial: func(ctx context.Context, network, address string) (net.Conn, error) { | |
d := net.Dialer{ | |
Timeout: time.Millisecond * time.Duration(10000), | |
} | |
resolverFull := fmt.Sprintf("%s:53", resolver) | |
return d.DialContext(ctx, network, resolverFull) | |
}, | |
} | |
ip, _ := r.LookupHost(context.Background(), host) | |
return ip[0] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment