try os/user.HomeDir when $HOME is unset when find kubeconfig file

Signed-off-by: rick <linuxsuren@users.noreply.github.com>
This commit is contained in:
rick
2021-06-18 11:28:10 +08:00
parent 90f5a44911
commit 8e40702b75
2 changed files with 12 additions and 2 deletions

View File

@@ -15,6 +15,7 @@ package openpitrix
import (
"bytes"
"github.com/go-openapi/strfmt"
"k8s.io/klog"

View File

@@ -18,6 +18,7 @@ package k8s
import (
"os"
"os/user"
"path"
"k8s.io/client-go/util/homedir"
@@ -54,8 +55,16 @@ func NewKubernetesOptions() (option *KubernetesOptions) {
}
// make it be easier for those who wants to run api-server locally
userHomeConfig := path.Join(homedir.HomeDir(), ".kube/config")
if _, err := os.Stat(userHomeConfig); !os.IsNotExist(err) {
homePath := homedir.HomeDir()
if homePath == "" {
// try os/user.HomeDir when $HOME is unset.
if u, err := user.Current(); err == nil {
homePath = u.HomeDir
}
}
userHomeConfig := path.Join(homePath, ".kube/config")
if _, err := os.Stat(userHomeConfig); err == nil {
option.KubeConfig = userHomeConfig
}
return