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:
@@ -15,6 +15,7 @@ package openpitrix
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"github.com/go-openapi/strfmt"
|
||||
"k8s.io/klog"
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user