From c0e94a4c1a4b562f48c9ca317c78b64d1a09434d Mon Sep 17 00:00:00 2001 From: Justin Nauman Date: Mon, 28 Aug 2017 07:55:03 -0500 Subject: [PATCH] Allow default kubeconfig resolution - Changed the default kubeconfig loading to utilize the client-go's loader strategy. This allows users to use the Ark client without having to explicitly define a KUBECONFIG env var or argument. This more closely resemebles how Kubectl works and users are probably more used to while preserving the current rules. Signed-off-by: Justin Nauman --- pkg/client/client.go | 9 ++++++--- pkg/client/factory.go | 10 +--------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index cc0155be7..7e76da1ee 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -24,8 +24,11 @@ import ( // Config returns a *rest.Config, using either the kubeconfig (if specified) or an in-cluster // configuration. func Config(kubeconfig string) (*rest.Config, error) { - if len(kubeconfig) > 0 { - return clientcmd.BuildConfigFromFlags("", kubeconfig) + loader := clientcmd.NewDefaultClientConfigLoadingRules() + loader.ExplicitPath = kubeconfig + clientConfig, err := clientcmd.BuildConfigFromKubeconfigGetter("", loader.Load) + if err != nil { + return nil, err } - return rest.InClusterConfig() + return clientConfig, nil } diff --git a/pkg/client/factory.go b/pkg/client/factory.go index 7fd599a4a..c52297248 100644 --- a/pkg/client/factory.go +++ b/pkg/client/factory.go @@ -17,8 +17,6 @@ limitations under the License. package client import ( - "os" - "github.com/spf13/pflag" "github.com/heptio/ark/pkg/generated/clientset" @@ -53,13 +51,7 @@ func (f *factory) BindFlags(flags *pflag.FlagSet) { } func (f *factory) Client() (clientset.Interface, error) { - kubeconfig := f.kubeconfig - if kubeconfig == "" { - // if the command line flag was not specified, try the environment variable - kubeconfig = os.Getenv("KUBECONFIG") - } - - clientConfig, err := Config(kubeconfig) + clientConfig, err := Config(f.kubeconfig) if err != nil { return nil, err }