From 39d0b58d7849304e3f5310fa36b2a8fec7c1f98c Mon Sep 17 00:00:00 2001 From: Michael Burman Date: Fri, 5 Apr 2024 15:56:47 +0300 Subject: [PATCH] Ignore HTTP_PROXY / HTTPS_PROXY when targetting the kube-apiserver --- pkg/kubernetes/client.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/pkg/kubernetes/client.go b/pkg/kubernetes/client.go index 28f6b8a..955e5fe 100644 --- a/pkg/kubernetes/client.go +++ b/pkg/kubernetes/client.go @@ -2,8 +2,11 @@ package kubernetes import ( "context" + "net/http" + "net/url" cassdcapi "github.com/k8ssandra/cass-operator/apis/cassandra/v1beta1" + "golang.org/x/net/http/httpproxy" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -11,6 +14,10 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" ) +var ( + kubeRestConfig *rest.Config +) + // NamespacedClient encapsulates namespacedClient with public namespace and restConfig type NamespacedClient struct { client.Client @@ -20,6 +27,8 @@ type NamespacedClient struct { // GetClient returns a controller-runtime client with cass-operator API defined func GetClient(restConfig *rest.Config) (client.Client, error) { + kubeRestConfig = restConfig + restConfig.Proxy = ProxyFunc c, err := client.New(restConfig, client.Options{}) if err != nil { return nil, err @@ -41,7 +50,6 @@ func GetClientInNamespace(restConfig *rest.Config, namespace string) (Namespaced Config: restConfig, Client: c, }, nil - // return c, nil } func CreateNamespaceIfNotExists(client client.Client, namespace string) error { @@ -57,3 +65,18 @@ func CreateNamespaceIfNotExists(client client.Client, namespace string) error { return nil } + +func envProxyFunc() func(*url.URL) (*url.URL, error) { + envProxyFuncValue := httpproxy.FromEnvironment().ProxyFunc() + ourProxy := func(u *url.URL) (*url.URL, error) { + if u.Hostname() == kubeRestConfig.Host { + return u, nil + } + return envProxyFuncValue(u) + } + return ourProxy +} + +func ProxyFunc(req *http.Request) (*url.URL, error) { + return envProxyFunc()(req.URL) +}