From 38bf4aaaf5457618368ffda11717af10a96e9378 Mon Sep 17 00:00:00 2001 From: Philippe Richard Date: Sun, 19 Sep 2021 23:49:55 -0400 Subject: [PATCH] feat: Add env vars config for argo-server and workflow-controller Signed-off-by: Philippe Richard --- cmd/argo/commands/server.go | 23 ++++++++++++++++++----- cmd/workflow-controller/main.go | 17 +++++++++++++++++ docs/environment-variables.md | 14 +++++++++++++- go.mod | 2 ++ 4 files changed, 50 insertions(+), 6 deletions(-) diff --git a/cmd/argo/commands/server.go b/cmd/argo/commands/server.go index f26f6f6b6dab..79aaf2d51b35 100644 --- a/cmd/argo/commands/server.go +++ b/cmd/argo/commands/server.go @@ -7,6 +7,7 @@ import ( "os" "reflect" "strconv" + "strings" "time" eventsource "github.com/argoproj/argo-events/pkg/client/eventsource/clientset/versioned" @@ -15,6 +16,8 @@ import ( log "github.com/sirupsen/logrus" "github.com/skratchdot/open-golang/open" "github.com/spf13/cobra" + "github.com/spf13/pflag" + "github.com/spf13/viper" "golang.org/x/net/context" "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" @@ -180,11 +183,7 @@ See %s`, help.ArgoServer), } command.Flags().IntVarP(&port, "port", "p", 2746, "Port to listen on") - defaultBaseHRef := os.Getenv("BASE_HREF") - if defaultBaseHRef == "" { - defaultBaseHRef = "/" - } - command.Flags().StringVar(&baseHRef, "basehref", defaultBaseHRef, "Value for base href in index.html. Used if the server is running behind reverse proxy under subpath different from /. Defaults to the environment variable BASE_HREF.") + command.Flags().StringVar(&baseHRef, "basehref", "/", "Value for base href in index.html. Used if the server is running behind reverse proxy under subpath different from /. Defaults to the environment variable BASE_HREF.") // "-e" for encrypt, like zip command.Flags().BoolVarP(&secure, "secure", "e", true, "Whether or not we should listen on TLS.") command.Flags().BoolVar(&htst, "hsts", true, "Whether or not we should add a HTTP Secure Transport Security header. This only has effect if secure is enabled.") @@ -198,5 +197,19 @@ See %s`, help.ArgoServer), command.Flags().StringVar(&frameOptions, "x-frame-options", "DENY", "Set X-Frame-Options header in HTTP responses.") command.Flags().StringVar(&accessControlAllowOrigin, "access-control-allow-origin", "", "Set Access-Control-Allow-Origin header in HTTP responses.") command.Flags().StringVar(&logFormat, "log-format", "text", "The formatter to use for logs. One of: text|json") + + viper.AutomaticEnv() + viper.SetEnvPrefix("ARGO") + viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "_")) + if err := viper.BindPFlags(command.Flags()); err != nil { + log.Fatal(err) + } + command.Flags().VisitAll(func(f *pflag.Flag) { + if !f.Changed && viper.IsSet(f.Name) { + val := viper.Get(f.Name) + command.Flags().Set(f.Name, fmt.Sprintf("%v", val)) + } + }) + return &command } diff --git a/cmd/workflow-controller/main.go b/cmd/workflow-controller/main.go index 77f520a745f8..bff509701965 100644 --- a/cmd/workflow-controller/main.go +++ b/cmd/workflow-controller/main.go @@ -5,6 +5,7 @@ import ( "fmt" "net/http" "os" + "strings" "time" "github.com/argoproj/pkg/cli" @@ -13,6 +14,8 @@ import ( "github.com/argoproj/pkg/stats" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "github.com/spf13/pflag" + "github.com/spf13/viper" runtimeutil "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/client-go/kubernetes" @@ -135,6 +138,20 @@ func NewRootCommand() *cobra.Command { command.Flags().Float32Var(&qps, "qps", 20.0, "Queries per second") command.Flags().BoolVar(&namespaced, "namespaced", false, "run workflow-controller as namespaced mode") command.Flags().StringVar(&managedNamespace, "managed-namespace", "", "namespace that workflow-controller watches, default to the installation namespace") + + viper.AutomaticEnv() + viper.SetEnvPrefix("ARGO") + viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "_")) + if err := viper.BindPFlags(command.Flags()); err != nil { + log.Fatal(err) + } + command.Flags().VisitAll(func(f *pflag.Flag) { + if !f.Changed && viper.IsSet(f.Name) { + val := viper.Get(f.Name) + command.Flags().Set(f.Name, fmt.Sprintf("%v", val)) + } + }) + return &command } diff --git a/docs/environment-variables.md b/docs/environment-variables.md index dfee85c59bcf..d2fa61f42848 100644 --- a/docs/environment-variables.md +++ b/docs/environment-variables.md @@ -37,7 +37,19 @@ Note that these environment variables may be removed at any time. | `WORKFLOW_GC_PERIOD` | `time.Duration` | `5m` | The periodicity for GC of workflows. | | `BUBBLE_ENTRY_TEMPLATE_ERR` | `bool` | `true` | Whether to bubble up template errors to workflow. | | `INFORMER_WRITE_BACK` | `bool` | `true` | Whether to write back to informer instead of catching up. | -| `GRPC_MESSAGE_SIZE` | `string` | Use different GRPC Max message size for Argo server deployment (supporting huge workflows) | +| `GRPC_MESSAGE_SIZE` | `string` | Use different GRPC Max message size for Argo server deployment (supporting huge workflows). | + +CLI parameters of the `argo-server` and `workflow-controller` can be specified as environment variables with the `ARGO_` prefix. For example: + +``` +workflow-controller --managed-namespace=argo +``` + +Can be expressed as: + +``` +ARGO_MANAGED_NAMESPACE=argo workflow-controller +``` You can set environment variable for the argo-server deployment, for example: diff --git a/go.mod b/go.mod index 51205995444a..f0e3ef5a4679 100644 --- a/go.mod +++ b/go.mod @@ -48,6 +48,8 @@ require ( github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 github.com/soheilhy/cmux v0.1.4 github.com/spf13/cobra v1.2.1 + github.com/spf13/pflag v1.0.5 + github.com/spf13/viper v1.8.1 github.com/stretchr/testify v1.7.0 github.com/tidwall/gjson v1.7.5 github.com/valyala/fasthttp v1.22.0 // indirect