diff --git a/pkg/cli/job/common.go b/pkg/cli/job/common.go index eab51394294..140dc45d128 100644 --- a/pkg/cli/job/common.go +++ b/pkg/cli/job/common.go @@ -17,9 +17,9 @@ limitations under the License. package job import ( - "path/filepath" - "github.com/spf13/cobra" + "os" + "path/filepath" ) type commonFlags struct { @@ -30,13 +30,11 @@ type commonFlags struct { func initFlags(cmd *cobra.Command, cf *commonFlags) { cmd.Flags().StringVarP(&cf.Master, "master", "s", "", "the address of apiserver") - if defaultKubeConfigFile := filepath.Join(homeDir(), ".kube", "config"); checkFileExist(defaultKubeConfigFile) { - cmd.Flags().StringVarP(&cf.Kubeconfig, "kubeconfig", "k", defaultKubeConfigFile, "(optional) absolute path to the kubeconfig file") - } else if kubeConfig := kubeConfig(); kubeConfig != "" { - //Default kubeconfig file is located in $HOME/.kube/config . In case this file does not exist, it will look for $KUBECONFIG instead. - cmd.Flags().StringVarP(&cf.Kubeconfig, "kubeconfig", "k", kubeConfig, "(optional) absolute path to the kubeconfig file") - } else { - cmd.Flags().StringVarP(&cf.Kubeconfig, "kubeconfig", "k", "", "(optional) absolute path to the kubeconfig file") - } + kubeConfFile := os.Getenv("KUBECONFIG") + defaultKubeConfigFile := filepath.Join(homeDir(), ".kube", "config") + if kubeConfFile == "" && fileExist(defaultKubeConfigFile) { + kubeConfFile = defaultKubeConfigFile + } + cmd.Flags().StringVarP(&cf.Kubeconfig, "kubeconfig", "k", kubeConfFile, "(optional) absolute path to the kubeconfig file") } diff --git a/pkg/cli/job/util.go b/pkg/cli/job/util.go index 9e5d77a6699..37c2beb96e0 100644 --- a/pkg/cli/job/util.go +++ b/pkg/cli/job/util.go @@ -35,18 +35,10 @@ import ( "volcano.sh/volcano/pkg/client/clientset/versioned" ) -func checkFileExist(filePath string) bool { - if file, err := os.Stat(filePath); err == nil && !file.IsDir() { - return true - } - return false -} +func fileExist(filePath string) bool { + fileInfo, err := os.Stat(filePath) -func kubeConfig() string { - if config := os.Getenv("KUBECONFIG"); config != "" { - return config - } - return "" + return err == nil && !fileInfo.IsDir() } func homeDir() string {