Skip to content

Commit

Permalink
Merge pull request #97 from devtron-labs/k8s-util-change
Browse files Browse the repository at this point in the history
fix: K8s util change
  • Loading branch information
kartik-579 authored Aug 12, 2024
2 parents 75b1d36 + a996c33 commit f14be46
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
39 changes: 31 additions & 8 deletions utils/k8s/K8sUtil.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ import (
"k8s.io/client-go/kubernetes"
v12 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/rest"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/yaml"
)
Expand All @@ -72,7 +71,7 @@ type K8sServiceImpl struct {
}

type K8sService interface {
GetLogsForAPod(kubeClient *kubernetes.Clientset, namespace string, podName string, container string, follow bool) *restclient.Request
GetLogsForAPod(kubeClient *kubernetes.Clientset, namespace string, podName string, container string, follow bool) *rest.Request
GetMetricsClientSet(restConfig *rest.Config, k8sHttpClient *http.Client) (*metrics.Clientset, error)
GetNmByName(ctx context.Context, metricsClientSet *metrics.Clientset, name string) (*v1beta1.NodeMetrics, error)
GetNmList(ctx context.Context, metricsClientSet *metrics.Clientset) (*v1beta1.NodeMetricsList, error)
Expand Down Expand Up @@ -120,7 +119,7 @@ type K8sService interface {
GetK8sDiscoveryClient(clusterConfig *ClusterConfig) (*discovery.DiscoveryClient, error)
GetClientForInCluster() (*v12.CoreV1Client, error)
GetCoreV1Client(clusterConfig *ClusterConfig) (*v12.CoreV1Client, error)
GetRestConfigByCluster(clusterConfig *ClusterConfig) (*restclient.Config, error)
GetRestConfigByCluster(clusterConfig *ClusterConfig) (*rest.Config, error)
GetResource(ctx context.Context, namespace string, name string, gvk schema.GroupVersionKind, restConfig *rest.Config) (*ManifestResponse, error)
UpdateResource(ctx context.Context, restConfig *rest.Config, gvk schema.GroupVersionKind, namespace string, k8sRequestPatch string) (*ManifestResponse, error)
DeleteResource(ctx context.Context, restConfig *rest.Config, gvk schema.GroupVersionKind, namespace string, name string, forceDelete bool) (*ManifestResponse, error)
Expand All @@ -140,6 +139,11 @@ type K8sService interface {
CreateK8sClientSet(restConfig *rest.Config) (*kubernetes.Clientset, error)
CreateOrUpdateSecretByName(client *v12.CoreV1Client, namespace, uniqueSecretName string, secretLabel map[string]string, secretData map[string]string) error
//CreateK8sClientSetWithCustomHttpTransport(restConfig *rest.Config) (*kubernetes.Clientset, error)

//below functions are exposed for K8sUtilExtended
GetRestConfigByClusterWithoutCustomTransport(clusterConfig *ClusterConfig) (*rest.Config, error)
OverrideRestConfigWithCustomTransport(restConfig *rest.Config) (*rest.Config, error)
CreateNs(namespace string, client *v12.CoreV1Client) (ns *v1.Namespace, err error)
}

func NewK8sUtil(logger *zap.SugaredLogger, runTimeConfig *RuntimeConfig) *K8sServiceImpl {
Expand All @@ -157,7 +161,20 @@ func NewK8sUtil(logger *zap.SugaredLogger, runTimeConfig *RuntimeConfig) *K8sSer
return &K8sServiceImpl{logger: logger, runTimeConfig: runTimeConfig, kubeconfig: kubeconfig, httpClientConfig: httpClientConfig}
}

func (impl *K8sServiceImpl) GetRestConfigByCluster(clusterConfig *ClusterConfig) (*restclient.Config, error) {
func (impl *K8sServiceImpl) GetRestConfigByCluster(clusterConfig *ClusterConfig) (*rest.Config, error) {
restConfig, err := impl.GetRestConfigByClusterWithoutCustomTransport(clusterConfig)
if err != nil {
impl.logger.Errorw("error, GetRestConfigByClusterWithoutCustomTransport", "err", err)
return nil, err
}
restConfig, err = impl.OverrideRestConfigWithCustomTransport(restConfig)
if err != nil {
impl.logger.Errorw("error in overriding rest config with custom transport configurations", "err", err)
}
return restConfig, err
}

func (impl *K8sServiceImpl) GetRestConfigByClusterWithoutCustomTransport(clusterConfig *ClusterConfig) (*rest.Config, error) {
bearerToken := clusterConfig.BearerToken
var restConfig *rest.Config
var err error
Expand All @@ -171,11 +188,17 @@ func (impl *K8sServiceImpl) GetRestConfigByCluster(clusterConfig *ClusterConfig)
restConfig = &rest.Config{Host: clusterConfig.Host, BearerToken: bearerToken}
clusterConfig.PopulateTlsConfigurationsInto(restConfig)
}
return restConfig, nil
}

func (impl *K8sServiceImpl) OverrideRestConfigWithCustomTransport(restConfig *rest.Config) (*rest.Config, error) {
var err error
restConfig, err = impl.httpClientConfig.OverrideConfigWithCustomTransport(restConfig)
if err != nil {
impl.logger.Errorw("error in overriding rest config with custom transport configurations", "err", err)
return nil, err
}
return restConfig, err
return restConfig, nil
}

func (impl *K8sServiceImpl) GetCoreV1Client(clusterConfig *ClusterConfig) (*v12.CoreV1Client, error) {
Expand Down Expand Up @@ -283,7 +306,7 @@ func (impl *K8sServiceImpl) CreateNsIfNotExists(namespace string, clusterConfig
return nil
}
impl.logger.Infow("ns not exists creating", "ns", namespace)
_, err = impl.createNs(namespace, v12Client)
_, err = impl.CreateNs(namespace, v12Client)
return err
}

Expand All @@ -302,7 +325,7 @@ func (impl *K8sServiceImpl) CheckIfNsExists(namespace string, client *v12.CoreV1

}

func (impl *K8sServiceImpl) createNs(namespace string, client *v12.CoreV1Client) (ns *v1.Namespace, err error) {
func (impl *K8sServiceImpl) CreateNs(namespace string, client *v12.CoreV1Client) (ns *v1.Namespace, err error) {
nsSpec := &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace}}
ns, err = client.Namespaces().Create(context.Background(), nsSpec, metav1.CreateOptions{})
if err != nil {
Expand Down Expand Up @@ -1087,7 +1110,7 @@ func (impl *K8sServiceImpl) GetMetricsClientSet(restConfig *rest.Config, k8sHttp
}
return metricsClientSet, err
}
func (impl *K8sServiceImpl) GetLogsForAPod(kubeClient *kubernetes.Clientset, namespace string, podName string, container string, follow bool) *restclient.Request {
func (impl *K8sServiceImpl) GetLogsForAPod(kubeClient *kubernetes.Clientset, namespace string, podName string, container string, follow bool) *rest.Request {
podLogOpts := &v1.PodLogOptions{
Container: container,
Follow: follow,
Expand Down
2 changes: 1 addition & 1 deletion utils/k8s/bean.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func (impl *CustomK8sHttpTransportConfig) OverrideConfigWithCustomTransport(conf
}

transport := utilnet.SetTransportDefaults(&http.Transport{
Proxy: http.ProxyFromEnvironment,
Proxy: config.Proxy,
TLSHandshakeTimeout: time.Duration(impl.TLSHandshakeTimeout) * time.Second,
TLSClientConfig: tlsConfig,
MaxIdleConns: impl.MaxIdleConnsPerHost,
Expand Down

0 comments on commit f14be46

Please sign in to comment.