Skip to content

Commit

Permalink
Support KUBECON env.
Browse files Browse the repository at this point in the history
Signed-off-by: Klaus Ma <mada3@huawei.com>
  • Loading branch information
Klaus Ma committed Oct 19, 2019
1 parent afc20c7 commit 2856dfc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 21 deletions.
18 changes: 8 additions & 10 deletions pkg/cli/job/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ limitations under the License.
package job

import (
"path/filepath"

"github.com/spf13/cobra"
"os"
"path/filepath"
)

type commonFlags struct {
Expand All @@ -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")
}
14 changes: 3 additions & 11 deletions pkg/cli/job/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 2856dfc

Please sign in to comment.