diff --git a/common-lib/utils/k8s/bean.go b/common-lib/utils/k8s/bean.go index afd05a320..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,10 +44,16 @@ type ClusterConfig struct { RemoteConnectionConfig *bean.RemoteConnectionConfigBean } +var logger, _ = utils.NewSugardLogger() + 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 + 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 { - 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 +}