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

support reading configs from environment variables #448

Open
frzifus opened this issue Feb 1, 2023 · 3 comments
Open

support reading configs from environment variables #448

frzifus opened this issue Feb 1, 2023 · 3 comments

Comments

@frzifus
Copy link
Contributor

frzifus commented Feb 1, 2023

Currently only file paths to the configs can be specified:

      --rbac.config=/etc/observatorium/rbac.yaml
      --tenants.config=/etc/observatorium/tenants.yaml

While there is nothing wrong with it, i think ability to provide these configurations via environment variables would simplify the usage.

Like this:

      --rbac.config=env:OBSERVATORIUM_CONFIG_RBAC
      --tenants.config=env:OBSERVATORIUM_CONFIG_TENANTS

wdyt?

@periklis
Copy link
Contributor

periklis commented Feb 1, 2023

Although I am not a big fan of configuration via environment variables, I don't necessary say it's a bad idea. IMHO the above is proposing a bit custom syntax with env:VAR_NAME which requires custom maintenance. I have seen (but don't recall names) in other projects to use the following pattern. First try if --rbac.config is set as a flag then lookup if SOME_PREFIX_RBAC_CONFIG is given, if both not given then it is not set. I am not sure if our current go flagset library supports this, but I believe this would work for both modes.

@frzifus
Copy link
Contributor Author

frzifus commented Feb 1, 2023

I seem to remember seeing something like this on the library viper. OTEL uses koanf instead. Seems they faced some issues in the past using viper. It would be a nice to have, but my intention was not to move larger blocks.

What I had in mind was something simple like this snippet.
func readCfgOrFatal(cfgPath string) string {
	if strings.HasPrefix(cfgPath, "env:") {
		env := strings.Replace(cfgPath, "env:", "", 1)
		return os.Getenv(env)
	}
	f, err := os.ReadFile(cfgPath)
	if err != nil {
		stdlog.Fatalf("cannot read configuration file from path %q: %v", cfgPath, err)
	}
	return string(f)
}

@matej-g
Copy link
Contributor

matej-g commented Feb 1, 2023

If I understood @periklis that would make sense to me as well - just have a predefined env var name which serves as an alternative to flag. Then:

  • Flag / config has precedent over env var
  • If none is provide, we use defaults (or, if configuration is mandatory, error out)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants