Skip to content
Snippets Groups Projects
Commit e4dfa34a authored by liuyan's avatar liuyan Committed by Sheng Yang
Browse files

Change kubeconfig priority

The priority of kubeconfig would be:
    1. Command args. We could specify the --kubeconfig.
    2. ENV. We read from RecommendedConfigPathEnvVar.
    3. In-cluster.
    4. Recommended. We got by RecommendedHomeDir and RecommendedFileName, which would be ~/.kube/config.
parent 1c34d7a2
No related branches found
No related tags found
No related merge requests found
......@@ -21,21 +21,20 @@ import (
var (
VERSION = "0.0.1"
FlagConfigFile = "config"
FlagProvisionerName = "provisioner-name"
EnvProvisionerName = "PROVISIONER_NAME"
DefaultProvisionerName = "rancher.io/local-path"
FlagNamespace = "namespace"
EnvNamespace = "POD_NAMESPACE"
DefaultNamespace = "local-path-storage"
FlagHelperImage = "helper-image"
EnvHelperImage = "HELPER_IMAGE"
DefaultHelperImage = "busybox"
FlagKubeconfig = "kubeconfig"
DefaultKubeConfigFilePath = ".kube/config"
DefaultConfigFileKey = "config.json"
DefaultConfigMapName = "local-path-config"
FlagConfigMapName = "configmap-name"
FlagConfigFile = "config"
FlagProvisionerName = "provisioner-name"
EnvProvisionerName = "PROVISIONER_NAME"
DefaultProvisionerName = "rancher.io/local-path"
FlagNamespace = "namespace"
EnvNamespace = "POD_NAMESPACE"
DefaultNamespace = "local-path-storage"
FlagHelperImage = "helper-image"
EnvHelperImage = "HELPER_IMAGE"
DefaultHelperImage = "busybox"
FlagKubeconfig = "kubeconfig"
DefaultConfigFileKey = "config.json"
DefaultConfigMapName = "local-path-config"
FlagConfigMapName = "configmap-name"
)
func cmdNotFound(c *cli.Context, command string) {
......@@ -110,17 +109,25 @@ func homeDir() string {
}
func loadConfig(kubeconfig string) (*rest.Config, error) {
if c, err := rest.InClusterConfig(); err == nil {
return c, nil
if len(kubeconfig) > 0 {
return clientcmd.BuildConfigFromFlags("", kubeconfig)
}
home := homeDir()
if kubeconfig == "" && home != "" {
kubeconfig = filepath.Join(home, DefaultKubeConfigFilePath)
kubeconfigPath := os.Getenv(clientcmd.RecommendedConfigPathEnvVar)
if len(kubeconfigPath) > 0 {
envVarFiles := filepath.SplitList(kubeconfigPath)
for _, f := range envVarFiles {
if _, err := os.Stat(f); err == nil {
return clientcmd.BuildConfigFromFlags("", f)
}
}
}
_, err := os.Stat(kubeconfig)
if err != nil {
return nil, err
if c, err := rest.InClusterConfig(); err == nil {
return c, nil
}
kubeconfig = filepath.Join(homeDir(), clientcmd.RecommendedHomeDir, clientcmd.RecommendedFileName)
return clientcmd.BuildConfigFromFlags("", kubeconfig)
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment