From 01893a7a6ffa8abca3b4ff1fad9e325768fe428c Mon Sep 17 00:00:00 2001 From: Eytan Avisror Date: Thu, 1 Apr 2021 10:20:37 -0700 Subject: [PATCH] fix: Allow kubeconfig chaining for local-mode (#268) Signed-off-by: Eytan Avisror --- controllers/providers/kubernetes/utils.go | 33 +++-------------------- 1 file changed, 4 insertions(+), 29 deletions(-) diff --git a/controllers/providers/kubernetes/utils.go b/controllers/providers/kubernetes/utils.go index 7756938a..fbba540f 100644 --- a/controllers/providers/kubernetes/utils.go +++ b/controllers/providers/kubernetes/utils.go @@ -19,10 +19,7 @@ import ( "bytes" "context" "encoding/json" - "fmt" "html/template" - "os" - "os/user" "reflect" "strings" @@ -189,11 +186,7 @@ func GetKubernetesConfig() (*rest.Config, error) { var config *rest.Config config, err := rest.InClusterConfig() if err != nil { - config, err = GetKubernetesLocalConfig() - if err != nil { - return nil, err - } - return config, nil + return GetKubernetesLocalConfig() } return config, nil } @@ -212,27 +205,9 @@ func RenderCustomResource(tpl string, params interface{}) (string, error) { } func GetKubernetesLocalConfig() (*rest.Config, error) { - var kubePath string - if os.Getenv("KUBECONFIG") != "" { - kubePath = os.Getenv("KUBECONFIG") - } else { - usr, err := user.Current() - if err != nil { - return nil, err - } - kubePath = usr.HomeDir + "/.kube/config" - } - - if kubePath == "" { - err := fmt.Errorf("failed to get kubeconfig path") - return nil, err - } - - config, err := clientcmd.BuildConfigFromFlags("", kubePath) - if err != nil { - return nil, err - } - return config, nil + loadingRules := clientcmd.NewDefaultClientConfigLoadingRules() + clientCfg := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{}) + return clientCfg.ClientConfig() } func CRDExists(kubeClient dynamic.Interface, name string) bool {