From 58de23fee4a1a6ee2dde86ad49278dc72c9d2339 Mon Sep 17 00:00:00 2001 From: Carlos Sanchez Date: Wed, 14 Aug 2019 08:57:59 +0200 Subject: [PATCH] Fix error when KUBECONFIG has empty entries (#4100) Signed-off-by: Carlos Sanchez --- pkg/minikube/kubeconfig/kubeconfig.go | 9 ++++++++- pkg/minikube/kubeconfig/kubeconfig_test.go | 14 +++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/pkg/minikube/kubeconfig/kubeconfig.go b/pkg/minikube/kubeconfig/kubeconfig.go index bfe6ce8ed0d0..5cedc698d205 100644 --- a/pkg/minikube/kubeconfig/kubeconfig.go +++ b/pkg/minikube/kubeconfig/kubeconfig.go @@ -87,7 +87,14 @@ func PathFromEnv() string { if kubeConfigEnv == "" { return constants.KubeconfigPath } - return filepath.SplitList(kubeConfigEnv)[0] + kubeConfigFiles := filepath.SplitList(kubeConfigEnv) + for _, kubeConfigFile := range kubeConfigFiles { + if kubeConfigFile != "" { + return kubeConfigFile + } + glog.Infof("Ignoring empty entry in %s env var", constants.KubeconfigEnvVar) + } + return constants.KubeconfigPath } // extractIP returns the IP address stored for minikube in the kubeconfig specified diff --git a/pkg/minikube/kubeconfig/kubeconfig_test.go b/pkg/minikube/kubeconfig/kubeconfig_test.go index 18be965b76a8..9e5d853546b5 100644 --- a/pkg/minikube/kubeconfig/kubeconfig_test.go +++ b/pkg/minikube/kubeconfig/kubeconfig_test.go @@ -555,11 +555,23 @@ func TestGetKubeConfigPath(t *testing.T) { input: "/home/fake/.kube/.kubeconfig:/home/fake2/.kubeconfig", want: "/home/fake/.kube/.kubeconfig", }, + { + input: ":/home/fake/.kube/.kubeconfig:/home/fake2/.kubeconfig", + want: "/home/fake/.kube/.kubeconfig", + }, + { + input: ":", + want: "$HOME/.kube/config", + }, + { + input: "", + want: "$HOME/.kube/config", + }, } for _, test := range tests { os.Setenv(clientcmd.RecommendedConfigPathEnvVar, test.input) - if result := PathFromEnv(); result != test.want { + if result := PathFromEnv(); result != os.ExpandEnv(test.want) { t.Errorf("Expected first splitted chunk, got: %s", result) } }