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

Configuration: Support environment expansion in configuration #2837

Merged
merged 3 commits into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 8 additions & 6 deletions cmd/loki/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ func init() {
}

type Config struct {
loki.Config `yaml:",inline"`
printVersion bool
verifyConfig bool
printConfig bool
logConfig bool
configFile string
loki.Config `yaml:",inline"`
printVersion bool
verifyConfig bool
printConfig bool
logConfig bool
configFile string
configExpandEnv bool
}

func (c *Config) RegisterFlags(f *flag.FlagSet) {
Expand All @@ -43,6 +44,7 @@ func (c *Config) RegisterFlags(f *flag.FlagSet) {
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.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
14 changes: 8 additions & 6 deletions cmd/promtail/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ func init() {
}

type Config struct {
config.Config `yaml:",inline"`
printVersion bool
printConfig bool
logConfig bool
dryRun bool
configFile string
config.Config `yaml:",inline"`
printVersion bool
printConfig bool
logConfig bool
dryRun bool
configFile string
configExpandEnv bool
}

func (c *Config) RegisterFlags(f *flag.FlagSet) {
Expand All @@ -43,6 +44,7 @@ func (c *Config) RegisterFlags(f *flag.FlagSet) {
"level with the order reversed, reversing the order makes viewing the entries easier in Grafana.")
f.BoolVar(&c.dryRun, "dry-run", false, "Start Promtail but print entries instead of sending them to Loki.")
f.StringVar(&c.configFile, "config.file", "", "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)
}

Expand Down
27 changes: 25 additions & 2 deletions docs/sources/clients/promtail/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,30 @@ For more detailed information on configuring how to discover and scrape logs fro
targets, see [Scraping](../scraping/). For more information on transforming logs
from scraped targets, see [Pipelines](../pipelines/).

Generic placeholders are defined as follows:
### Use environment variables in the configuration

You can use environment variable references in the config file to set values that need to be configurable during deployment.
cyriltovena marked this conversation as resolved.
Show resolved Hide resolved
To do this, use:

```
${VAR}
```

Where VAR is the name of the environment variable.

Each variable reference is replaced at startup by the value of the environment variable.
The replacement is case-sensitive and occurs before the YAML file is parsed.
References to undefined variables are replaced by empty strings unless you specify a default value or custom error text.

To specify a default value, use:

```
${VAR:default_value}
```

Where default_value is the value to use if the environment variable is undefined.

### Generic placeholders:

- `<boolean>`: a boolean that can take the values `true` or `false`
- `<int>`: any integer matching the regular expression `[1-9]+[0-9]*`
Expand All @@ -93,7 +116,7 @@ Generic placeholders are defined as follows:
- `<string>`: a regular string
- `<secret>`: a regular string that is a secret, such as a password

Supported contents and default values of `config.yaml`:
### Supported contents and default values of `config.yaml`:

```yaml
# Configures the server for Promtail.
Expand Down
28 changes: 26 additions & 2 deletions docs/sources/configuration/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,30 @@ command line. The file is written in [YAML format](https://en.wikipedia.org/wiki
defined by the scheme below. Brackets indicate that a parameter is optional. For
non-list parameters the value is set to the specified default.

Generic placeholders are defined as follows:
### Use environment variables in the configuration

You can use environment variable references in the config file to set values that need to be configurable during deployment.
To do this, use:

```
${VAR}
```

Where VAR is the name of the environment variable.

Each variable reference is replaced at startup by the value of the environment variable.
The replacement is case-sensitive and occurs before the YAML file is parsed.
References to undefined variables are replaced by empty strings unless you specify a default value or custom error text.

To specify a default value, use:

```
${VAR:default_value}
```

Where default_value is the value to use if the environment variable is undefined.

### Generic placeholders:

- `<boolean>` : a boolean that can take the values `true` or `false`
- `<int>` : any integer matching the regular expression `[1-9]+[0-9]*`
Expand All @@ -76,7 +99,7 @@ Generic placeholders are defined as follows:
- `<string>` : a regular string
- `<secret>` : a regular string that is a secret, such as a password

Supported contents and default values of `loki.yaml`:
### Supported contents and default values of `loki.yaml`:

```yaml
# The module to run Loki with. Supported values
Expand Down Expand Up @@ -1850,3 +1873,4 @@ multi_kv_config:
mirror-enabled: false
primary: consul
```
### Generic placeholders
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/docker/docker v17.12.0-ce-rc1.0.20201009160326-9c15e82f19b0+incompatible
github.com/docker/go-metrics v0.0.0-20181218153428-b84716841b82 // indirect
github.com/docker/go-plugins-helpers v0.0.0-20181025120712-1e6269c305b8
github.com/drone/envsubst v1.0.2
github.com/dustin/go-humanize v1.0.0
github.com/fatih/color v1.9.0
github.com/fluent/fluent-bit-go v0.0.0-20190925192703-ea13c021720c
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96/go.mod h1:Qh8CwZgvJUkLughtfhJv5dyTYa91l1fOUCrgjqmcifM=
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
github.com/drone/envsubst v1.0.2 h1:dpYLMAspQHW0a8dZpLRKe9jCNvIGZPhCPrycZzIHdqo=
github.com/drone/envsubst v1.0.2/go.mod h1:bkZbnc/2vh1M12Ecn7EYScpI4YGYU0etwLJICOWi8Z0=
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
Expand Down
19 changes: 16 additions & 3 deletions pkg/cfg/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"fmt"
"io/ioutil"
"os"
"strconv"

"github.com/drone/envsubst"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
Expand Down Expand Up @@ -36,13 +38,19 @@ func dJSON(y []byte) Source {
}

// YAML returns a Source that opens the supplied `.yaml` file and loads it.
func YAML(f string) Source {
func YAML(f string, envSubst bool) Source {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think either we should rename the variable to something like expandEnvVars and/or add a comment about what envSubst means. It would look clearer.

return func(dst Cloneable) error {
y, err := ioutil.ReadFile(f)
if err != nil {
return err
}

if envSubst {
s, err := envsubst.EvalEnv(string(y))
if err != nil {
return err
}
y = []byte(s)
}
err = dYAML(y)(dst)
return errors.Wrap(err, f)
}
Expand Down Expand Up @@ -82,8 +90,13 @@ 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())(dst)
return YAML(f.Value.String(), expandEnv)(dst)

}
}
2 changes: 1 addition & 1 deletion pkg/logcli/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (q *Query) DoLocalQuery(out output.LogOutput, statistics bool, orgID string
if q.LocalConfig == "" {
return errors.New("no supplied config file")
}
if err := cfg.YAML(q.LocalConfig)(&conf); err != nil {
if err := cfg.YAML(q.LocalConfig, false)(&conf); err != nil {
return err
}

Expand Down
8 changes: 8 additions & 0 deletions vendor/github.com/drone/envsubst/.drone.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/github.com/drone/envsubst/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/drone/envsubst/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions vendor/github.com/drone/envsubst/README

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions vendor/github.com/drone/envsubst/eval.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading