Use config when flags are not set for logging
Use viper in addition to flags for logging options. Since these flags are used in glog, we can't directly use viper. Instead, we use viper's built in precedence logic (https://github.com/spf13/viper#why-viper) to set the flags directly.pull/469/head
parent
8d31f918db
commit
fe087c6126
|
@ -40,8 +40,8 @@ var dirs = [...]string{
|
|||
constants.MakeMiniPath("cache", "localkube"),
|
||||
constants.MakeMiniPath("config")}
|
||||
|
||||
var (
|
||||
showLibmachineLogs bool
|
||||
const (
|
||||
showLibmachineLogs = "show-libmachine-logs"
|
||||
)
|
||||
|
||||
// RootCmd represents the base command when called without any subcommands
|
||||
|
@ -50,14 +50,15 @@ var RootCmd = &cobra.Command{
|
|||
Short: "Minikube is a tool for managing local Kubernetes clusters.",
|
||||
Long: `Minikube is a CLI tool that provisions and manages single-node Kubernetes clusters optimized for development workflows.`,
|
||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
||||
setFlagsUsingViper()
|
||||
for _, path := range dirs {
|
||||
if err := os.MkdirAll(path, 0777); err != nil {
|
||||
glog.Exitf("Error creating minikube directory: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
log.SetDebug(showLibmachineLogs)
|
||||
if !showLibmachineLogs {
|
||||
log.SetDebug(viper.Get(showLibmachineLogs))
|
||||
if !viper.GetBool(showLibmachineLogs) {
|
||||
log.SetOutWriter(ioutil.Discard)
|
||||
log.SetErrWriter(ioutil.Discard)
|
||||
}
|
||||
|
@ -73,9 +74,27 @@ func Execute() {
|
|||
}
|
||||
}
|
||||
|
||||
// Handle config values for flags used in external packages (e.g. glog)
|
||||
// by setting them directly, using values from viper when not passed in as args
|
||||
func setFlagsUsingViper() {
|
||||
setFlagValues := func(a *pflag.Flag) {
|
||||
viper.SetDefault(a.Name, a.DefValue)
|
||||
// If the flag is set, override viper value
|
||||
if a.Changed {
|
||||
viper.Set(a.Name, a.Value.String())
|
||||
}
|
||||
// Viper will give precedence first to calls to the Set command,
|
||||
// then to values from the config.yml
|
||||
a.Value.Set(viper.GetString(a.Name))
|
||||
}
|
||||
|
||||
pflag.VisitAll(setFlagValues)
|
||||
}
|
||||
|
||||
func init() {
|
||||
RootCmd.PersistentFlags().BoolVarP(&showLibmachineLogs, "show-libmachine-logs", "", false, "Whether or not to show logs from libmachine.")
|
||||
RootCmd.PersistentFlags().Bool(showLibmachineLogs, false, "Whether or not to show logs from libmachine.")
|
||||
pflag.CommandLine.AddGoFlagSet(goflag.CommandLine)
|
||||
viper.BindPFlags(RootCmd.PersistentFlags())
|
||||
cobra.OnInitialize(initConfig)
|
||||
}
|
||||
|
||||
|
|
|
@ -21,6 +21,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/pflag"
|
||||
"github.com/spf13/viper"
|
||||
"k8s.io/minikube/pkg/minikube/constants"
|
||||
"k8s.io/minikube/pkg/minikube/tests"
|
||||
|
@ -59,3 +60,43 @@ func TestEnvVariable(t *testing.T) {
|
|||
t.Fatalf("Viper did not respect environment variable")
|
||||
}
|
||||
}
|
||||
|
||||
func cleanup() {
|
||||
pflag.Set("v", "0")
|
||||
pflag.Lookup("v").Changed = false
|
||||
}
|
||||
|
||||
func TestFlagShouldOverrideConfig(t *testing.T) {
|
||||
defer cleanup()
|
||||
viper.Set("v", "1337")
|
||||
pflag.Set("v", "100")
|
||||
setFlagsUsingViper()
|
||||
if viper.GetInt("v") != 100 {
|
||||
viper.Debug()
|
||||
t.Fatal("Value from viper config overrode explicit flag value")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigShouldOverrideDefault(t *testing.T) {
|
||||
defer cleanup()
|
||||
viper.Set("v", "1337")
|
||||
setFlagsUsingViper()
|
||||
if viper.GetInt("v") != 1337 {
|
||||
viper.Debug()
|
||||
t.Fatalf("Value from viper config did not override default flag value")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFallbackToDefaultFlag(t *testing.T) {
|
||||
setFlagsUsingViper()
|
||||
|
||||
if viper.GetInt("stderrthreshold") != 2 {
|
||||
t.Logf("stderrthreshold %s", viper.GetInt("stderrthreshold"))
|
||||
t.Fatalf("The default flag value was overwritten")
|
||||
}
|
||||
|
||||
if viper.GetString("log-flush-frequency") != "5s" {
|
||||
t.Logf("log flush frequency: %s", viper.GetString("log-flush-frequency"))
|
||||
t.Fatalf("The default flag value was overwritten")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue