Skip to content

Instantly share code, notes, and snippets.

@stevehenderson
Created April 23, 2022 14:48
Show Gist options
  • Save stevehenderson/36420085765057f0824d99653ef84bca to your computer and use it in GitHub Desktop.
Save stevehenderson/36420085765057f0824d99653ef84bca to your computer and use it in GitHub Desktop.
Replace K8s namespace with IP
//
// 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