Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Look up config file in default locations #6160

Merged
merged 5 commits into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions docs/sources/configuration/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@ 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.
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.

### Use environment variables in the configuration

Expand Down
2 changes: 1 addition & 1 deletion pkg/loki/config_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", "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)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
}
6 changes: 3 additions & 3 deletions pkg/util/cfg/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
Expand Down
22 changes: 14 additions & 8 deletions pkg/util/cfg/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"os"
"strconv"
"strings"

"github.com/drone/envsubst"
"github.com/pkg/errors"
Expand Down Expand Up @@ -65,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)
Expand All @@ -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, set %s for custom config path", f.Value.String(), name)
}
}
4 changes: 2 additions & 2 deletions pkg/util/cfg/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down