diff --git a/cmd/argo/commands/root.go b/cmd/argo/commands/root.go index 5df892b72488..4571545b0afc 100644 --- a/cmd/argo/commands/root.go +++ b/cmd/argo/commands/root.go @@ -1,9 +1,14 @@ package commands import ( + "fmt" + "strings" + "github.com/argoproj/pkg/cli" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "github.com/spf13/pflag" + "github.com/spf13/viper" "github.com/argoproj/argo-workflows/v3" "github.com/argoproj/argo-workflows/v3/cmd/argo/commands/archive" @@ -133,5 +138,23 @@ If your server is behind an ingress with a path (you'll be running "argo server command.PersistentFlags().IntVar(&glogLevel, "gloglevel", 0, "Set the glog logging level") command.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Enabled verbose logging, i.e. --loglevel debug") + // set-up env vars for the CLI such that ARGO_* env vars can be used instead of flags + viper.AutomaticEnv() + viper.SetEnvPrefix("ARGO") + viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "_")) + // bind flags to env vars (https://github.com/spf13/viper/tree/v1.17.0#working-with-flags) + if err := viper.BindPFlags(command.PersistentFlags()); err != nil { + log.Fatal(err) + } + // workaround for handling required flags (https://github.com/spf13/viper/issues/397#issuecomment-544272457) + command.PersistentFlags().VisitAll(func(f *pflag.Flag) { + if !f.Changed && viper.IsSet(f.Name) { + val := viper.Get(f.Name) + if err := command.PersistentFlags().Set(f.Name, fmt.Sprintf("%v", val)); err != nil { + log.Fatal(err) + } + } + }) + return command } diff --git a/cmd/argo/commands/server.go b/cmd/argo/commands/server.go index 95d9ae49a8c1..8fb5b7d90506 100644 --- a/cmd/argo/commands/server.go +++ b/cmd/argo/commands/server.go @@ -237,12 +237,15 @@ See %s`, help.ArgoServer), command.Flags().Float32Var(&kubeAPIQPS, "kube-api-qps", 20.0, "QPS to use while talking with kube-apiserver.") command.Flags().IntVar(&kubeAPIBurst, "kube-api-burst", 30, "Burst to use while talking with kube-apiserver.") + // set-up env vars for the CLI such that ARGO_* env vars can be used instead of flags viper.AutomaticEnv() viper.SetEnvPrefix("ARGO") viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "_")) + // bind flags to env vars (https://github.com/spf13/viper/tree/v1.17.0#working-with-flags) if err := viper.BindPFlags(command.Flags()); err != nil { log.Fatal(err) } + // workaround for handling required flags (https://github.com/spf13/viper/issues/397#issuecomment-544272457) command.Flags().VisitAll(func(f *pflag.Flag) { if !f.Changed && viper.IsSet(f.Name) { val := viper.Get(f.Name) diff --git a/cmd/workflow-controller/main.go b/cmd/workflow-controller/main.go index c2a4fabf273c..89d0ef4a06a2 100644 --- a/cmd/workflow-controller/main.go +++ b/cmd/workflow-controller/main.go @@ -191,12 +191,15 @@ func NewRootCommand() *cobra.Command { command.Flags().StringVar(&managedNamespace, "managed-namespace", "", "namespace that workflow-controller watches, default to the installation namespace") command.Flags().BoolVar(&executorPlugins, "executor-plugins", false, "enable executor plugins") + // set-up env vars for the CLI such that ARGO_* env vars can be used instead of flags viper.AutomaticEnv() viper.SetEnvPrefix("ARGO") viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "_")) + // bind flags to env vars (https://github.com/spf13/viper/tree/v1.17.0#working-with-flags) if err := viper.BindPFlags(command.Flags()); err != nil { log.Fatal(err) } + // workaround for handling required flags (https://github.com/spf13/viper/issues/397#issuecomment-544272457) command.Flags().VisitAll(func(f *pflag.Flag) { if !f.Changed && viper.IsSet(f.Name) { val := viper.Get(f.Name)