From 684ed1584f61f4848c56388c2f47df312c1815e9 Mon Sep 17 00:00:00 2001 From: Andy Roth Date: Mon, 21 Mar 2022 14:28:45 -0400 Subject: [PATCH 1/2] wip --- cli/internal/k8s/common.go | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/cli/internal/k8s/common.go b/cli/internal/k8s/common.go index 686c319c70..bc4872a98a 100644 --- a/cli/internal/k8s/common.go +++ b/cli/internal/k8s/common.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "io" - "os" "regexp" "time" @@ -153,21 +152,13 @@ func init() { func getRestConfig() *rest.Config { message.Debug("k8s.getRestConfig()") - // use the KUBECONFIG context if it exists - configPath := os.Getenv("KUBECONFIG") - if configPath == "" { - // use the current context in the default kubeconfig in the home path of the user - homePath, err := os.UserHomeDir() - if err != nil { - message.Fatal(nil, "Unable to load the current user's home directory") - } - configPath = homePath + "/.kube/config" - } - - config, err := clientcmd.BuildConfigFromFlags("", configPath) + config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( + clientcmd.NewDefaultClientConfigLoadingRules(), + &clientcmd.ConfigOverrides{}).ClientConfig() if err != nil { message.Fatalf(err, "Unable to connect to the K8s cluster") } + return config } From 1192512de2e447811ff2b89e90025329140250fd Mon Sep 17 00:00:00 2001 From: Andy Roth Date: Mon, 21 Mar 2022 14:46:59 -0400 Subject: [PATCH 2/2] add code comments --- cli/internal/k8s/common.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cli/internal/k8s/common.go b/cli/internal/k8s/common.go index bc4872a98a..c2f9cd51dd 100644 --- a/cli/internal/k8s/common.go +++ b/cli/internal/k8s/common.go @@ -149,9 +149,13 @@ func init() { klog.SetLogger(generateLogShim()) } +// getRestConfig uses the K8s "client-go" library to get the currently active kube context, in the same way that +// "kubectl" gets it if no extra config flags like "--kubeconfig" are passed func getRestConfig() *rest.Config { message.Debug("k8s.getRestConfig()") + // Build the config from the currently active kube context in the default way that the k8s client-go gets it, which + // is to look at the KUBECONFIG env var config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( clientcmd.NewDefaultClientConfigLoadingRules(), &clientcmd.ConfigOverrides{}).ClientConfig()