Use less viper discovery

Tweaks so that we are stricter about how we use viper config file
discovery.  By being more explicit we are less dependent on its
specific behaviour.
This commit is contained in:
Justin Santa Barbara 2018-06-13 23:51:12 -04:00
parent 6e9a315ac9
commit fdc358f903
1 changed files with 32 additions and 32 deletions

View File

@ -21,12 +21,15 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"path/filepath"
"strings"
"github.com/golang/glog" "github.com/golang/glog"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"k8s.io/apimachinery/pkg/util/validation/field" "k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
"k8s.io/kops/cmd/kops/util" "k8s.io/kops/cmd/kops/util"
kopsapi "k8s.io/kops/pkg/apis/kops" kopsapi "k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/client/simple" "k8s.io/kops/pkg/client/simple"
@ -125,6 +128,7 @@ func NewCmdRoot(f *util.Factory, out io.Writer) *cobra.Command {
cmd.PersistentFlags().StringVar(&rootCommand.RegistryPath, "state", "", "Location of state storage (kops 'config' file). Overrides KOPS_STATE_STORE environment variable") cmd.PersistentFlags().StringVar(&rootCommand.RegistryPath, "state", "", "Location of state storage (kops 'config' file). Overrides KOPS_STATE_STORE environment variable")
viper.BindPFlag("KOPS_STATE_STORE", cmd.PersistentFlags().Lookup("state")) viper.BindPFlag("KOPS_STATE_STORE", cmd.PersistentFlags().Lookup("state"))
viper.BindEnv("KOPS_STATE_STORE")
defaultClusterName := os.Getenv("KOPS_CLUSTER_NAME") defaultClusterName := os.Getenv("KOPS_CLUSTER_NAME")
cmd.PersistentFlags().StringVarP(&rootCommand.clusterName, "name", "", defaultClusterName, "Name of cluster. Overrides KOPS_CLUSTER_NAME environment variable") cmd.PersistentFlags().StringVarP(&rootCommand.clusterName, "name", "", defaultClusterName, "Name of cluster. Overrides KOPS_CLUSTER_NAME environment variable")
@ -148,42 +152,38 @@ func NewCmdRoot(f *util.Factory, out io.Writer) *cobra.Command {
// initConfig reads in config file and ENV variables if set. // initConfig reads in config file and ENV variables if set.
func initConfig() { func initConfig() {
// Config file precedence: --config flag, ${HOME}/.kops.yaml ${HOME}/.kops/config
// Take in order Flag --state, ENV, .kops.yaml, .kops/config configFile := rootCommand.configFile
if configFile == "" {
if os.Getenv("KOPS_STATE_STORE") != "" && rootCommand.RegistryPath == "" { home := homedir.HomeDir()
rootCommand.RegistryPath = os.Getenv("KOPS_STATE_STORE") configPaths := []string{
viper.BindEnv("KOPS_STATE_STORE") filepath.Join(home, ".kops.yaml"),
viper.AutomaticEnv() filepath.Join(home, ".kops", "config"),
}
for _, p := range configPaths {
_, err := os.Stat(p)
if err == nil {
configFile = p
break
} else if !os.IsNotExist(err) {
glog.V(2).Infof("error checking for file %s: %v", p, err)
}
}
}
if configFile != "" {
viper.SetConfigFile(configFile)
viper.SetConfigType("yaml")
if err := viper.ReadInConfig(); err != nil {
glog.Warningf("error reading config: %v", err)
}
} }
if rootCommand.configFile != "" {
// enable ability to specify config file via flag
viper.SetConfigFile(rootCommand.configFile)
if err := viper.ReadInConfig(); err == nil {
viper.BindEnv("KOPS_STATE_STORE")
rootCommand.RegistryPath = viper.GetString("KOPS_STATE_STORE") rootCommand.RegistryPath = viper.GetString("KOPS_STATE_STORE")
viper.AutomaticEnv()
} // Tolerate multiple slashes at end
} rootCommand.RegistryPath = strings.TrimSuffix(rootCommand.RegistryPath, "/")
if rootCommand.configFile == "" && rootCommand.RegistryPath == "" {
viper.SetConfigName(".kops") // name of config file (without extension)
viper.AddConfigPath("$HOME") // adding home directory as first search path. Assume that exist. Need to be fix for windows
viper.ReadInConfig()
if err := viper.ReadInConfig(); err == nil {
viper.BindEnv("KOPS_STATE_STORE")
rootCommand.RegistryPath = viper.GetString("KOPS_STATE_STORE")
viper.AutomaticEnv()
}
viper.SetConfigName("config")
viper.AddConfigPath("$HOME/.kops") // adding home/.kops/config directory as second search path
viper.ReadInConfig()
if err := viper.ReadInConfig(); err == nil {
viper.BindEnv("KOPS_STATE_STORE")
rootCommand.RegistryPath = viper.GetString("KOPS_STATE_STORE")
viper.AutomaticEnv()
}
}
} }
func (c *RootCmd) AddCommand(cmd *cobra.Command) { func (c *RootCmd) AddCommand(cmd *cobra.Command) {