From d1829bdf300695e8e251ff5e161a5357fdd0b26f Mon Sep 17 00:00:00 2001 From: Shivam-nagar23 Date: Mon, 16 Jun 2025 12:47:53 +0530 Subject: [PATCH 1/2] chore-port-forward-debug --- common-lib/utils/k8s/bean.go | 8 ++++++-- common-lib/utils/k8s/helper.go | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/common-lib/utils/k8s/bean.go b/common-lib/utils/k8s/bean.go index afd05a320..043f8ddec 100644 --- a/common-lib/utils/k8s/bean.go +++ b/common-lib/utils/k8s/bean.go @@ -44,9 +44,13 @@ type ClusterConfig struct { } func (clusterConfig *ClusterConfig) PopulateTlsConfigurationsInto(restConfig *rest.Config) { - restConfig.TLSClientConfig = rest.TLSClientConfig{Insecure: clusterConfig.InsecureSkipTLSVerify} + serverName, err := GetServerNameFromServerUrl(clusterConfig.Host) + if err != nil { + // making it non-blocking to avoid blocking the flow + fmt.Println("Error parsing server URL:", err, "clusterConfig.Host", clusterConfig.Host) + } + restConfig.TLSClientConfig = rest.TLSClientConfig{Insecure: clusterConfig.InsecureSkipTLSVerify, ServerName: serverName} if clusterConfig.InsecureSkipTLSVerify == false { - restConfig.TLSClientConfig.ServerName = restConfig.ServerName restConfig.TLSClientConfig.KeyData = []byte(clusterConfig.KeyData) restConfig.TLSClientConfig.CertData = []byte(clusterConfig.CertData) restConfig.TLSClientConfig.CAData = []byte(clusterConfig.CAData) diff --git a/common-lib/utils/k8s/helper.go b/common-lib/utils/k8s/helper.go index 892979931..157f3edd2 100644 --- a/common-lib/utils/k8s/helper.go +++ b/common-lib/utils/k8s/helper.go @@ -36,7 +36,9 @@ import ( "k8s.io/client-go/discovery" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" + "net" "net/http" + "net/url" "strings" ) @@ -226,3 +228,19 @@ func OverrideK8sHttpClientWithTracer(restConfig *rest.Config) (*http.Client, err httpClientFor.Transport = otelhttp.NewTransport(httpClientFor.Transport) return httpClientFor, nil } + +func GetServerNameFromServerUrl(serverURL string) (string, error) { + u, err := url.Parse(serverURL) + if err != nil { + return "", err + } + + host := u.Host + if strings.Contains(host, ":") { + host, _, err = net.SplitHostPort(u.Host) + if err != nil { + return "", err + } + } + return host, nil +} From c1d38aa3a6b5abaaa9fbd65525affaffeaa680ae Mon Sep 17 00:00:00 2001 From: Shivam-nagar23 Date: Tue, 17 Jun 2025 12:33:05 +0530 Subject: [PATCH 2/2] review comments resolved --- common-lib/utils/k8s/bean.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/common-lib/utils/k8s/bean.go b/common-lib/utils/k8s/bean.go index 043f8ddec..c5dc1a033 100644 --- a/common-lib/utils/k8s/bean.go +++ b/common-lib/utils/k8s/bean.go @@ -20,6 +20,7 @@ import ( "errors" "fmt" "github.com/caarlos0/env" + "github.com/devtron-labs/common-lib/utils" "github.com/devtron-labs/common-lib/utils/k8sObjectsUtil" "github.com/devtron-labs/common-lib/utils/remoteConnection/bean" v1 "k8s.io/api/core/v1" @@ -43,11 +44,13 @@ type ClusterConfig struct { RemoteConnectionConfig *bean.RemoteConnectionConfigBean } +var logger, _ = utils.NewSugardLogger() + func (clusterConfig *ClusterConfig) PopulateTlsConfigurationsInto(restConfig *rest.Config) { serverName, err := GetServerNameFromServerUrl(clusterConfig.Host) if err != nil { // making it non-blocking to avoid blocking the flow - fmt.Println("Error parsing server URL:", err, "clusterConfig.Host", clusterConfig.Host) + logger.Errorw("Error parsing server URL:", "err", err, "clusterConfig.Host", clusterConfig.Host) } restConfig.TLSClientConfig = rest.TLSClientConfig{Insecure: clusterConfig.InsecureSkipTLSVerify, ServerName: serverName} if clusterConfig.InsecureSkipTLSVerify == false {