gpt4 book ai didi

go - 为 Kubernetes go-client 使用 HTTP 代理

转载 作者:数据小太阳 更新时间:2023-10-29 03:10:49 26 4
gpt4 key购买 nike

我正在使用 go-client for kubernetes 来控制我的 GKE 集群上的部署,但此客户端将在代理后面运行,并且需要通过它发出所有与互联网绑定(bind)的请求。但我似乎无法找到一种方法来配置我的 KubeClient 以通过代理发出所有 http 请求。

我的代码与此处的示例差别不大 - https://github.com/kubernetes/client-go/blob/master/examples/out-of-cluster-client-configuration/main.go

最佳答案

当您使用配置 (kubernetes.NewForConfig(config)) 设置新客户端时,您可以自定义您的传输:

proxyURL := url.URL{Host: proxy}
transport := http.Transport{Proxy: http.ProxyURL(&proxyURL), ....}
config.Transport = config.Transport

或者您可以使用 config.WrapTransport:

  • 传输 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.


遗憾的是,让它工作并不简单,使用 HTTP_PROXYno_proxy 通常更容易。

关于go - 为 Kubernetes go-client 使用 HTTP 代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52218669/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com