Reputation: 365
I am using go-client for kubernetes to control deployments on my GKE cluster, but this client is to be run behind a proxy and needs to make all it's internet bound requests through this. But I cannot seem to find a way to configure my KubeClient to make all http requests through a proxy.
My code is not very different from the sample here - https://github.com/kubernetes/client-go/blob/master/examples/out-of-cluster-client-configuration/main.go
Upvotes: 1
Views: 3235
Reputation: 9
There are three options to “tell” a Go client to use a proxy:
a. Set the HTTP_PROXY environment variable:
$ export HTTP_PROXY="http://ProxyIP:ProxyPort"
HTTP_PROXY environment variable will be used as the proxy URL for HTTP requests and HTTPS requests, unless overridden by HTTPS_PROXY or NO_PROXY
b. Creating an HTTP client in Go that MUST use a proxy:
proxy, _ := url.Parse("http://ProxyIP:ProxyPort")
httpClient := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxy)}}
c. Change the default transport used by Go “net/http” package. This would affect the entire program (including the default HTTP client)
proxy, _ := url.Parse("http://ProxyIP:ProxyPort")
http.DefaultTransport := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxy)}}
For full details, see my blog post: https://medium.com/@tufin/how-to-use-a-proxy-with-go-http-client-cfc485e9f342
Upvotes: 0
Reputation: 5073
When you are setting up the new client with the config (kubernetes.NewForConfig(config)
) you can customize your transport:
proxyURL := url.URL{Host: proxy}
transport := http.Transport{Proxy: http.ProxyURL(&proxyURL), ....}
config.Transport = config.Transport
Or you can make use of config.WrapTransport:
Transport may be used for custom HTTP behavior. This attribute may not be specified with the TLS client certificate options. Use WrapTransport for most client level operations.
WrapTransport will be invoked for custom HTTP behavior after the underlying transport is initialized (either the transport created from TLSClientConfig, Transport, or http.DefaultTransport). The config may layer other RoundTrippers on top of the returned RoundTripper.
Sadly it is not straightforward to make it work, and making use of HTTP_PROXY
and no_proxy
often is easier.
Upvotes: 2