From 0e273571249f8b31f09e67ea47a149029ea0d749 Mon Sep 17 00:00:00 2001 From: Paul Gier Date: Fri, 3 Apr 2020 14:57:19 -0500 Subject: [PATCH] cmd/operator: parse CLI flags in Main instead of init This allows tests to be run against functions in Main without conflicting between test flags and application flags. More info: https://github.com/golang/go/issues/31859 --- cmd/operator/main.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/cmd/operator/main.go b/cmd/operator/main.go index 412c12aaaa1..de3a3606f2e 100644 --- a/cmd/operator/main.go +++ b/cmd/operator/main.go @@ -105,7 +105,6 @@ func serve(srv *http.Server, listener net.Listener, logger log.Logger) func() er } var ( - cfg prometheuscontroller.Config availableLogLevels = []string{ logLevelAll, logLevelDebug, @@ -118,11 +117,13 @@ var ( logFormatLogfmt, logFormatJson, } + cfg = prometheuscontroller.Config{ + CrdKinds: monitoringv1.DefaultCrdKinds, + } + flagset = flag.CommandLine ) func init() { - cfg.CrdKinds = monitoringv1.DefaultCrdKinds - flagset := flag.CommandLine klog.InitFlags(flagset) flagset.StringVar(&cfg.Host, "apiserver", "", "API Server addr, e.g. ' - NOT RECOMMENDED FOR PRODUCTION - http://127.0.0.1:8080'. Omit parameter to run in on-cluster mode and utilize the service account token.") flagset.StringVar(&cfg.TLSConfig.CertFile, "cert-file", "", " - NOT RECOMMENDED FOR PRODUCTION - Path to public TLS certificate file.") @@ -156,6 +157,9 @@ func init() { flagset.StringVar(&cfg.PromSelector, "prometheus-instance-selector", "", "Label selector to filter Prometheus CRDs to manage") flagset.StringVar(&cfg.AlertManagerSelector, "alertmanager-instance-selector", "", "Label selector to filter AlertManager CRDs to manage") flagset.StringVar(&cfg.ThanosRulerSelector, "thanos-ruler-instance-selector", "", "Label selector to filter ThanosRuler CRDs to manage") +} + +func Main() int { flagset.Parse(os.Args[1:]) cfg.Namespaces.AllowList = ns.asSlice() @@ -179,9 +183,7 @@ func init() { if len(cfg.Namespaces.ThanosRulerAllowList) == 0 { cfg.Namespaces.ThanosRulerAllowList = cfg.Namespaces.AllowList } -} -func Main() int { logger := log.NewLogfmtLogger(log.NewSyncWriter(os.Stdout)) if cfg.LogFormat == logFormatJson { logger = log.NewJSONLogger(log.NewSyncWriter(os.Stdout))