Skip to content

Commit

Permalink
Merge pull request #478 from Rui-Tang/bug-config
Browse files Browse the repository at this point in the history
check  while ~/.kube/config is missing
  • Loading branch information
k82cn authored Oct 19, 2019
2 parents 03b3053 + d680dfe commit afc20c7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pkg/cli/job/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ type commonFlags struct {
func initFlags(cmd *cobra.Command, cf *commonFlags) {
cmd.Flags().StringVarP(&cf.Master, "master", "s", "", "the address of apiserver")

if home := homeDir(); home != "" {
cmd.Flags().StringVarP(&cf.Kubeconfig, "kubeconfig", "k", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
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")
}

}
14 changes: 14 additions & 0 deletions pkg/cli/job/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ 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 kubeConfig() string {
if config := os.Getenv("KUBECONFIG"); config != "" {
return config
}
return ""
}

func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
Expand Down

0 comments on commit afc20c7

Please sign in to comment.