-
Notifications
You must be signed in to change notification settings - Fork 21
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 loading configuration from both YAML files and env vars #831
base: main
Are you sure you want to change the base?
Conversation
65232f7
to
f73a444
Compare
Bumps [github.com/icinga/icinga-go-library](https://github.com/icinga/icinga-go-library) from 0.4.0 to 0.5.0. - [Commits](Icinga/icinga-go-library@v0.4.0...v0.5.0) --- updated-dependencies: - dependency-name: github.com/icinga/icinga-go-library dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]>
f73a444
to
614d015
Compare
614d015
to
8f5e40e
Compare
8f5e40e
to
b18e594
Compare
if err := config.Load(&cfg, config.LoadOptions{ | ||
Flags: flags, | ||
EnvOptions: config.EnvOptions{Prefix: "ICINGADB_"}, | ||
}); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works as far as I have tested it.
However, the error output may be misleading for a non-existing /etc/icingadb/config.yml
file when not explicitly setting the --config
flag, relying on the default.
$ stat /etc/icingadb/config.yml
stat: cannot statx '/etc/icingadb/config.yml': No such file or directory
$ ./icingadb
invalid configuration: database host missing
$ ./icingadb -c /etc/icingadb/config.yml
can't open YAML file /etc/icingadb/config.yml: open /etc/icingadb/config.yml: no such file or directory
If there are no ICINGADB_
environment variables and the default config is not existing, the reported error does not state that the expected file does not exist, but says that certain fields are absent. But honestly, I don't know how to address this. For a package installations the config file should be present.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is also the config_test.go
file, testing against the now unused config.FromYAMLFile
function. Maybe you want to remove this test case and create another one for YAML-only, environment-only, and mixed-setups.
I will keep the original PR description as a reference, but the implementation now uses
config.Load()
from our Go libs.With this PR Icinga DB supports configuration loading in three scenarios:
config.FromYAMLFile()
is still called first but continuation withconfig.FromEnv()
is allowed by handling:config.FromEnv()
.config.FromEnv()
is called regardless of the outcome fromconfig.FromYAMLFile()
. If no environment variables are set, configuration relies entirely on YAML. Otherwise, environment variables can supplement, override YAML settings, or serve as the sole source.config.FromEnv()
also includes validation, ensuring completeness after considering both sources.Possible alternative implementations:
os.Stat()
could be used before callingconfig.FromYAMLFile()
, rather than handling non-existent file errors. This approach would split the logic into two sections and add an additional if block.Validate()
in the config package automatically. Instead, require it to be called manually. I appreciate that library-wise, bothconfig.FromYAMLFile()
andconfig.FromEnv()
include validation allowing them to be used without needing an additional function call on their own. When combining them, I think it's straightforward to useerrors.Is()
to check forErrInvalidConfiguration
, i.e. errors fromValidate()
.requires Icinga/icinga-go-library#87
requires #879
closes #756