Avoid calling flag.Parse() twice.#1997
Avoid calling flag.Parse() twice.#1997pracucci merged 9 commits intocortexproject:masterfrom pstibrany:dont-call-flag-parse-twice
Conversation
flag.Parse() was called twice previously, first to get -config.file value to parse the config file, and second to parse remaining command line parameters – overwriting values from config file. Unfortunately that confuses command line parameters that accept multiple values. Here we get -config.file via a separate FlagSet to avoid calling flag.Parse() (on default FlagSet) twice. Signed-off-by: Peter Štibraný <peter.stibrany@grafana.com>
Signed-off-by: Peter Štibraný <peter.stibrany@grafana.com>
pracucci
left a comment
There was a problem hiding this comment.
This PR introduces a regression. When I run Cortex from this PR branch with the TSDB storage I get this:
level=error ts=2020-01-17T16:47:19.9634104Z caller=log.go:141 msg="error initializing cortex" err="Must set -dynamodb.url in aws mode\nerror initialising module: table-manager\ngithub.com/cortexproject/cortex/pkg/cortex.(*Cortex).initModule\n\t/workspace/src/github.com/cortexproject/cortex/pkg/cortex/cortex.go:239\ngithub.com/cortexproject/cortex/pkg/cortex.(*Cortex).init\n\t/workspace/src/github.com/cortexproject/cortex/pkg/cortex/cortex.go:227\ngithub.com/cortexproject/cortex/pkg/cortex.New\n\t/workspace/src/github.com/cortexproject/cortex/pkg/cortex/cortex.go:185\nmain.main\n\t/workspace/src/github.com/cortexproject/cortex/cmd/cortex/main.go:73\nruntime.main\n\t/usr/local/Cellar/go/1.13/libexec/src/runtime/proc.go:203\nruntime.goexit\n\t/usr/local/Cellar/go/1.13/libexec/src/runtime/asm_amd64.s:1357"
I've the feeling it has to do with defaults. The flagext.RegisterFlags() does also set default values on the config. With this PR's change, we're calling RegisterFlags() after loading the config file, so we're overriding what we loaded from the config with the default values from flags. flagext.RegisterFlags() should be called before parsing the config file.
Would be great if we could have a unit test on this (maybe moving all config file + CLI flags logic in a single function to unit test), but if it's too complicated just skip it and do some manual tests please 😉
Signed-off-by: Peter Štibraný <peter.stibrany@grafana.com>
Signed-off-by: Peter Štibraný <peter.stibrany@grafana.com>
Added tests for happy path. Signed-off-by: Peter Štibraný <peter.stibrany@grafana.com>
Signed-off-by: Peter Štibraný <peter.stibrany@grafana.com>
Thanks for your review and my apologies! I was happy that flags work correctly that I forgot to check actual usage of config file 🤦♂ You're right about |
|
Tests work by 1) introducing "test mode" to main() function, which only does config/flags parsing, dumps resulting YAML and then stop, and 2) capturing stdout/stderr and checking for expected messages. |
Signed-off-by: Peter Štibraný <peter.stibrany@grafana.com>
Signed-off-by: Peter Štibraný <peter.stibrany@grafana.com>
pracucci
left a comment
There was a problem hiding this comment.
Very good job. The testMode doesn't make the main() much dirty and looks a good strategy to have smoke tests on CLI flags / YAML config parsing. LGTM!
Signed-off-by: Peter Štibraný <peter.stibrany@grafana.com>
flag.Parse()was called twice previously, first to get-config.filevalue to parse the config file, and second to parse remaining command line parameters – overwriting values from config file.Unfortunately that confuses command line parameters that accept multiple values (eg.
-memberlist.joinor-experimental.tsdb.block-ranges-periodin #1942). In this PR we get-config.filevia a separate FlagSet (which doesn't report any error or usage) to avoid callingflag.Parse()on default FlagSet twice.This should not be visible to end user in any way.