diff --git a/cmd/cortex/main.go b/cmd/cortex/main.go index 0503298329f..7540d0fbb95 100644 --- a/cmd/cortex/main.go +++ b/cmd/cortex/main.go @@ -115,7 +115,19 @@ func parseConfigFileParameter() string { fs := flag.NewFlagSet(os.Args[0], flag.ContinueOnError) fs.SetOutput(ioutil.Discard) fs.StringVar(&configFile, configFileOption, "", "") // usage not used in this function. - _ = fs.Parse(os.Args[1:]) + + // Try to find -config.file option in the flags. As Parsing stops on the first error, eg. unknown flag, we simply + // try remaining parameters until we find config flag, or there are no params left. + // (ContinueOnError just means that flag.Parse doesn't call panic or os.Exit, but it returns error, which we ignore) + args := os.Args[1:] + for len(args) > 0 { + _ = fs.Parse(args) + if configFile != "" { + // found (!) + break + } + args = args[1:] + } return configFile } diff --git a/cmd/cortex/main_test.go b/cmd/cortex/main_test.go index 17d3ab39a10..b9ba1a85c48 100644 --- a/cmd/cortex/main_test.go +++ b/cmd/cortex/main_test.go @@ -40,6 +40,12 @@ func TestFlagParsing(t *testing.T) { stderrMessage: "-unknown.flag", }, + "new flag, with config": { + arguments: []string{"-mem-ballast-size-bytes=100000"}, + yaml: "target: ingester", + stdoutMessage: "target: ingester", + }, + "config with wrong argument override": { yaml: "target: ingester", arguments: []string{"-target=unknown"}, @@ -90,7 +96,7 @@ func testSingle(t *testing.T, arguments []string, yaml string, stdoutMessage, st _, err = tempFile.WriteString(yaml) require.NoError(t, err) - arguments = append([]string{"-" + configFileOption, tempFile.Name()}, arguments...) + arguments = append(arguments, "-"+configFileOption, tempFile.Name()) } arguments = append([]string{"./cortex"}, arguments...)