From 8e2316ba62ab83e9b8b28300f005a6777d442714 Mon Sep 17 00:00:00 2001 From: Hidde Beydals Date: Sun, 4 Oct 2020 18:22:03 +0200 Subject: [PATCH] Support multi-path KUBECONFIG Ref: https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/#the-kubeconfig-environment-variable --- cmd/gotk/utils.go | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/cmd/gotk/utils.go b/cmd/gotk/utils.go index e6a2ef2f..e8cec919 100644 --- a/cmd/gotk/utils.go +++ b/cmd/gotk/utils.go @@ -26,12 +26,13 @@ import ( "os" "os/exec" "path/filepath" + "runtime" "strings" "text/template" corev1 "k8s.io/api/core/v1" rbacv1 "k8s.io/api/rbac/v1" - "k8s.io/apimachinery/pkg/runtime" + apiruntime "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/tools/clientcmd" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/kustomize/api/filesys" @@ -120,13 +121,16 @@ func (*Utils) execTemplate(obj interface{}, tmpl, filename string) error { return file.Sync() } -func (*Utils) kubeClient(config string) (client.Client, error) { - cfg, err := clientcmd.BuildConfigFromFlags("", config) +func (*Utils) kubeClient(kubeConfigPath string) (client.Client, error) { + configFiles := utils.splitKubeConfigPath(kubeConfigPath) + cfg, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( + &clientcmd.ClientConfigLoadingRules{Precedence: configFiles}, + &clientcmd.ConfigOverrides{}).ClientConfig() if err != nil { - return nil, fmt.Errorf("Kubernetes client initialization failed: %w", err) + return nil, fmt.Errorf("kubernetes client initialization failed: %w", err) } - scheme := runtime.NewScheme() + scheme := apiruntime.NewScheme() _ = corev1.AddToScheme(scheme) _ = rbacv1.AddToScheme(scheme) _ = sourcev1.AddToScheme(scheme) @@ -137,12 +141,27 @@ func (*Utils) kubeClient(config string) (client.Client, error) { Scheme: scheme, }) if err != nil { - return nil, fmt.Errorf("Kubernetes client initialization failed: %w", err) + return nil, fmt.Errorf("kubernetes client initialization failed: %w", err) } return kubeClient, nil } +// splitKubeConfigPath splits the given KUBECONFIG path based on the runtime OS +// target. +// +// Ref: https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/#the-kubeconfig-environment-variable +func (*Utils) splitKubeConfigPath(path string) []string { + var sep string + switch runtime.GOOS { + case "windows": + sep = ";" + default: + sep = ":" + } + return strings.Split(path, sep) +} + func (*Utils) writeFile(content, filename string) error { file, err := os.Create(filename) if err != nil {