From c89219e6047c9c42e86a66a4aa02e5f93104bafe Mon Sep 17 00:00:00 2001 From: Christian Haudum Date: Wed, 11 May 2022 16:18:04 +0200 Subject: [PATCH 1/5] Look for config.yaml file in ./ and ./config directories This change makes Loki look for a config file in the current directory or in the config/ sub-directory. This is a pre-requisite for making an easy-to-use tarball distribution that also ships a config.yaml Signed-off-by: Christian Haudum --- pkg/loki/config_wrapper.go | 2 +- pkg/util/cfg/files.go | 20 +++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/pkg/loki/config_wrapper.go b/pkg/loki/config_wrapper.go index 239b6c97b2a7f..0bb84f56624fa 100644 --- a/pkg/loki/config_wrapper.go +++ b/pkg/loki/config_wrapper.go @@ -42,7 +42,7 @@ func (c *ConfigWrapper) RegisterFlags(f *flag.FlagSet) { f.BoolVar(&c.ListTargets, "list-targets", false, "List available targets") f.BoolVar(&c.LogConfig, "log-config-reverse-order", false, "Dump the entire Loki config object at Info log "+ "level with the order reversed, reversing the order makes viewing the entries easier in Grafana.") - f.StringVar(&c.ConfigFile, "config.file", "", "yaml file to load") + f.StringVar(&c.ConfigFile, "config.file", "config.yaml,config/config.yaml", "yaml file to load") f.BoolVar(&c.ConfigExpandEnv, "config.expand-env", false, "Expands ${var} in config according to the values of the environment variables.") c.Config.RegisterFlags(f) } diff --git a/pkg/util/cfg/files.go b/pkg/util/cfg/files.go index 829312e7e740a..948adbe06c2aa 100644 --- a/pkg/util/cfg/files.go +++ b/pkg/util/cfg/files.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "os" "strconv" + "strings" "github.com/drone/envsubst" "github.com/pkg/errors" @@ -92,13 +93,18 @@ func YAMLFlag(args []string, name string) Source { if f == nil || f.Value.String() == "" { return nil } - expandEnv := false - expandEnvFlag := freshFlags.Lookup("config.expand-env") - if expandEnvFlag != nil { - expandEnv, _ = strconv.ParseBool(expandEnvFlag.Value.String()) // Can ignore error as false returned - } - - return YAML(f.Value.String(), expandEnv)(dst) + for _, val := range strings.Split(f.Value.String(), ",") { + val := strings.TrimSpace(val) + expandEnv := false + expandEnvFlag := freshFlags.Lookup("config.expand-env") + if expandEnvFlag != nil { + expandEnv, _ = strconv.ParseBool(expandEnvFlag.Value.String()) // Can ignore error as false returned + } + if _, err := os.Stat(val); err == nil { + return YAML(val, expandEnv)(dst) + } + } + return fmt.Errorf("%s does not exist", f.Value.String()) } } From 2c11a23690656bcbad8cad3e58c666a3bb3ac60c Mon Sep 17 00:00:00 2001 From: Christian Haudum Date: Thu, 12 May 2022 09:00:13 +0200 Subject: [PATCH 2/5] Rename YAMLFlag to ConfigFileLoader The `YAMLFlag` function is solely used for loading config files. Signed-off-by: Christian Haudum --- pkg/util/cfg/cfg.go | 2 +- pkg/util/cfg/dynamic.go | 6 +++--- pkg/util/cfg/files.go | 2 +- pkg/util/cfg/files_test.go | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pkg/util/cfg/cfg.go b/pkg/util/cfg/cfg.go index 188021533522f..6ba2c363cd902 100644 --- a/pkg/util/cfg/cfg.go +++ b/pkg/util/cfg/cfg.go @@ -47,7 +47,7 @@ func Unmarshal(dst Cloneable, sources ...Source) error { func DefaultUnmarshal(dst Cloneable, args []string, fs *flag.FlagSet) error { return Unmarshal(dst, Defaults(fs), - YAMLFlag(args, "config.file"), + ConfigFileLoader(args, "config.file"), Flags(args, fs), ) } diff --git a/pkg/util/cfg/dynamic.go b/pkg/util/cfg/dynamic.go index a56e39b6e849c..3f4de0528f51b 100644 --- a/pkg/util/cfg/dynamic.go +++ b/pkg/util/cfg/dynamic.go @@ -20,16 +20,16 @@ func DynamicUnmarshal(dst DynamicCloneable, args []string, fs *flag.FlagSet) err // First populate the config with defaults including flags from the command line Defaults(fs), // Next populate the config from the config file, we do this to populate the `common` - // section of the config file by taking advantage of the code in YAMLFlag which will load + // section of the config file by taking advantage of the code in ConfigFileLoader which will load // and process the config file. - YAMLFlag(args, "config.file"), + ConfigFileLoader(args, "config.file"), // Apply any dynamic logic to set other defaults in the config. This function is called after parsing the // config files so that values from a common, or shared, section can be used in // the dynamic evaluation dst.ApplyDynamicConfig(), // Load configs from the config file a second time, this will supersede anything set by the common // config with values specified in the config file. - YAMLFlag(args, "config.file"), + ConfigFileLoader(args, "config.file"), // Load the flags again, this will supersede anything set from config file with flags from the command line. Flags(args, fs), ) diff --git a/pkg/util/cfg/files.go b/pkg/util/cfg/files.go index 948adbe06c2aa..6ba28c484dc2f 100644 --- a/pkg/util/cfg/files.go +++ b/pkg/util/cfg/files.go @@ -66,7 +66,7 @@ func dYAML(y []byte) Source { } } -func YAMLFlag(args []string, name string) Source { +func ConfigFileLoader(args []string, name string) Source { return func(dst Cloneable) error { freshFlags := flag.NewFlagSet("config-file-loader", flag.ContinueOnError) diff --git a/pkg/util/cfg/files_test.go b/pkg/util/cfg/files_test.go index 64c47b1324a7f..554b710982feb 100644 --- a/pkg/util/cfg/files_test.go +++ b/pkg/util/cfg/files_test.go @@ -22,9 +22,9 @@ func (cfg *testCfg) Clone() flagext.Registerer { }(*cfg) } -func TestYAMLFlagDoesNotMutate(t *testing.T) { +func TestConfigFileLoaderDoesNotMutate(t *testing.T) { cfg := &testCfg{} - err := YAMLFlag(nil, "something")(cfg) + err := ConfigFileLoader(nil, "something")(cfg) require.Nil(t, err) require.Equal(t, 0, cfg.v) From 65f85a7a03f5054c78aaed3d3102eaf1a3734467 Mon Sep 17 00:00:00 2001 From: Christian Haudum Date: Tue, 17 May 2022 08:16:08 +0200 Subject: [PATCH 3/5] fixup! Look for config.yaml file in ./ and ./config directories --- pkg/util/cfg/files.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/util/cfg/files.go b/pkg/util/cfg/files.go index 6ba28c484dc2f..0cdf93344cd6e 100644 --- a/pkg/util/cfg/files.go +++ b/pkg/util/cfg/files.go @@ -105,6 +105,6 @@ func ConfigFileLoader(args []string, name string) Source { return YAML(val, expandEnv)(dst) } } - return fmt.Errorf("%s does not exist", f.Value.String()) + return fmt.Errorf("%s does not exist, set %s for custom config path", f.Value.String(), name) } } From f926c8217d8dcead38d7ba81a5a469f14681b903 Mon Sep 17 00:00:00 2001 From: Christian Haudum Date: Tue, 17 May 2022 08:20:22 +0200 Subject: [PATCH 4/5] fixup! fixup! Look for config.yaml file in ./ and ./config directories Signed-off-by: Christian Haudum --- docs/sources/configuration/_index.md | 8 +++++--- pkg/loki/config_wrapper.go | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/docs/sources/configuration/_index.md b/docs/sources/configuration/_index.md index 92590b15e4e75..c38b4b153183c 100644 --- a/docs/sources/configuration/_index.md +++ b/docs/sources/configuration/_index.md @@ -32,9 +32,11 @@ that the order of configs reads correctly top to bottom when viewed in Grafana's ## Configuration File Reference To specify which configuration file to load, pass the `-config.file` flag at the -command line. The file is written in [YAML format](https://en.wikipedia.org/wiki/YAML), -defined by the scheme below. Brackets indicate that a parameter is optional. For -non-list parameters the value is set to the specified default. +command line. The value can be a list of comma separated paths, then the first +file that exists will be used. The file is written in [YAML +format](https://en.wikipedia.org/wiki/YAML), defined by the scheme below. +Brackets indicate that a parameter is optional. For non-list parameters the +value is set to the specified default. ### Use environment variables in the configuration diff --git a/pkg/loki/config_wrapper.go b/pkg/loki/config_wrapper.go index 0bb84f56624fa..91859a8f23241 100644 --- a/pkg/loki/config_wrapper.go +++ b/pkg/loki/config_wrapper.go @@ -42,7 +42,7 @@ func (c *ConfigWrapper) RegisterFlags(f *flag.FlagSet) { f.BoolVar(&c.ListTargets, "list-targets", false, "List available targets") f.BoolVar(&c.LogConfig, "log-config-reverse-order", false, "Dump the entire Loki config object at Info log "+ "level with the order reversed, reversing the order makes viewing the entries easier in Grafana.") - f.StringVar(&c.ConfigFile, "config.file", "config.yaml,config/config.yaml", "yaml file to load") + f.StringVar(&c.ConfigFile, "config.file", "config.yaml,config/config.yaml", "configuration file to load, can be a comma separated list of paths, first existing file will be used") f.BoolVar(&c.ConfigExpandEnv, "config.expand-env", false, "Expands ${var} in config according to the values of the environment variables.") c.Config.RegisterFlags(f) } From d64dac077a589afab8b2ac3720f72b98314d1af7 Mon Sep 17 00:00:00 2001 From: Christian Haudum Date: Tue, 17 May 2022 08:24:29 +0200 Subject: [PATCH 5/5] fixup! fixup! fixup! Look for config.yaml file in ./ and ./config directories Signed-off-by: Christian Haudum --- docs/sources/configuration/_index.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/sources/configuration/_index.md b/docs/sources/configuration/_index.md index c38b4b153183c..f758ab5d8d51d 100644 --- a/docs/sources/configuration/_index.md +++ b/docs/sources/configuration/_index.md @@ -33,7 +33,11 @@ that the order of configs reads correctly top to bottom when viewed in Grafana's To specify which configuration file to load, pass the `-config.file` flag at the command line. The value can be a list of comma separated paths, then the first -file that exists will be used. The file is written in [YAML +file that exists will be used. +If no `-config.file` argument is specified, Loki will look up the `config.yaml` in the +current working directory and the `config/` sub-directory and try to use that. + +The file is written in [YAML format](https://en.wikipedia.org/wiki/YAML), defined by the scheme below. Brackets indicate that a parameter is optional. For non-list parameters the value is set to the specified default.