Skip to content

Commit

Permalink
cmd/operator: parse CLI flags in Main instead of init
Browse files Browse the repository at this point in the history
This allows tests to be run against functions in Main without
conflicting between test flags and application flags.

More info: golang/go#31859
  • Loading branch information
pgier authored and Benjamin committed Jun 26, 2020
1 parent 8fa7349 commit 0e27357
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions cmd/operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.")
Expand Down Expand Up @@ -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()
Expand All @@ -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))
Expand Down

0 comments on commit 0e27357

Please sign in to comment.