Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions common-lib/utils/k8s/bean.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions common-lib/utils/k8s/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}