api icon indicating copy to clipboard operation
api copied to clipboard

support reading configs from environment variables

Open frzifus opened this issue 2 years ago • 3 comments

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?

frzifus avatar Feb 01 '23 14:02 frzifus

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.

periklis avatar Feb 01 '23 16:02 periklis

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)
}

frzifus avatar Feb 01 '23 16:02 frzifus

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)

matej-g avatar Feb 01 '23 16:02 matej-g