diff --git a/cli/cmd/utils.go b/cli/cmd/utils.go index ff7455566..503d8d43a 100644 --- a/cli/cmd/utils.go +++ b/cli/cmd/utils.go @@ -29,6 +29,11 @@ import ( "github.com/kinvolk/lokomotive/pkg/platform" ) +const ( + kubeconfigEnvVariable = "KUBECONFIG" + defaultKubeconfigPath = "~/.kube/config" +) + // getConfiguredBackend loads a backend from the given configuration file. func getConfiguredBackend(lokoConfig *config.Config) (backend.Backend, hcl.Diagnostics) { if lokoConfig.RootConfig.Backend == nil { @@ -101,10 +106,11 @@ func expandKubeconfigPath(path string) string { return path } -// getKubeconfig finds the kubeconfig to be used. Precedence takes a specified -// flag or environment variable. Then the asset directory of the cluster is searched -// and finally the global default value is used. This cannot be done in Viper -// because we need the other values from Viper to find the asset directory. +// getKubeconfig finds the kubeconfig to be used. The preference is following: +// - --kubeconfig-file flag OR KUBECONFIG_FILE environent variable +// - asset directory from cluster configuration +// - KUBECONFIG environment variable +// - ~/.kube/config path, which is kubectl default one func getKubeconfig() (string, error) { kubeconfig := viper.GetString(kubeconfigFlag) if kubeconfig != "" { @@ -116,11 +122,18 @@ func getKubeconfig() (string, error) { return "", err } - if assetDir != "" { - return expandKubeconfigPath(assetsKubeconfig(assetDir)), nil + paths := []string{ + assetDir, + os.Getenv(kubeconfigEnvVariable), + } + + for _, path := range paths { + if path != "" { + return expandKubeconfigPath(path), nil + } } - return expandKubeconfigPath("~/.kube/config"), nil + return expandKubeconfigPath(defaultKubeconfigPath), nil } func assetsKubeconfig(assetDir string) string {