You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by zh...@apache.org on 2024/12/08 03:26:18 UTC

(dubbo-kubernetes) branch master updated: [operator] Add kube clientcmd util

This is an automated email from the ASF dual-hosted git repository.

zhongxjian pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo-kubernetes.git


The following commit(s) were added to refs/heads/master by this push:
     new c56d1887 [operator] Add kube clientcmd util
c56d1887 is described below

commit c56d1887adabde3e81651500851445a9ff79d246
Author: mfordjody <11...@qq.com>
AuthorDate: Sun Dec 8 11:26:08 2024 +0800

    [operator] Add kube clientcmd util
---
 pkg/util/kube/util.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)

diff --git a/pkg/util/kube/util.go b/pkg/util/kube/util.go
new file mode 100644
index 00000000..971f0ecb
--- /dev/null
+++ b/pkg/util/kube/util.go
@@ -0,0 +1,56 @@
+package kube
+
+import (
+	corev1 "k8s.io/api/core/v1"
+	"k8s.io/client-go/rest"
+	"k8s.io/client-go/tools/clientcmd"
+
+	"os"
+)
+
+func DefaultRestConfig(kubeconfig, context string, fns ...func(config *rest.Config)) (*rest.Config, error) {
+}
+
+func BuildClientConfig(kubeconfig, context string) (*rest.Config, error) {
+	c, err := BuildClientCmd(kubeconfig, context).ClientConfig()
+	if err != nil {
+		return nil, err
+	}
+	return
+}
+
+func SetRestDefaults(config *rest.Config) (*rest.Config) {
+	if config.GroupVersion == nil || config.GroupVersion.Empty() {
+		config.GroupVersion = &corev1.SchemeGroupVersion
+	}
+	if len(config.APIPath) == 0 {
+		if len(config.GroupVersion.Group) == 0 {
+			config.APIPath = "/api"
+		} else {
+			config.APIPath = "/apis"
+		}
+	}
+	if config.NegotiatedSerializer == nil {
+	}
+	return config
+}
+
+func BuildClientCmd(kubeconfig, context string, overrides ...func(configOverrides *clientcmd.ConfigOverrides)) clientcmd.ClientConfig {
+	if kubeconfig != "" {
+		info, err := os.Stat(kubeconfig)
+		if err != nil || info.Size() == 0 {
+			kubeconfig = ""
+		}
+	}
+	loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
+	loadingRules.DefaultClientConfig = &clientcmd.DefaultClientConfig
+	loadingRules.ExplicitPath = kubeconfig
+	configOverrides := &clientcmd.ConfigOverrides{
+		ClusterDefaults: clientcmd.ClusterDefaults,
+		CurrentContext:  context,
+	}
+	for _, fn := range overrides {
+		fn(configOverrides)
+	}
+	return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)
+}
\ No newline at end of file