Use HTTP proxy for Kubernetes go-client

9/7/2018

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

-- harpratap
go
kubernetes
kubernetes-go-client

1 Answer

7/17/2019

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 http.RoundTripper

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 func(rt http.RoundTripper) http.RoundTripper

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.

-- GalloCedrone
Source: StackOverflow