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

fix: set environment variables config values correctly for camelCaseconfig keys #245

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/cloudhut/kminion/v2/prometheus"
"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/confmap"
"github.com/knadh/koanf/providers/env"
"github.com/knadh/koanf/providers/file"
"github.com/mitchellh/mapstructure"
Expand Down Expand Up @@ -102,6 +103,23 @@ func newConfig(logger *zap.Logger) (Config, error) {
return Config{}, err
}

// Lowercase the keys that are stored internally within Koanf and reload them. This is a workaround because
// internally keys are stored case sensitive. This causes the problem that environment variables can't match
// the exact key and therefore will not be unmarshalled as expected anymore. Example:
// YAML path: minion.endToEnd.enabled
// ENV path: MINION_ENDTOEND_ENABLED => minion.endtoend.enabled
// Internal key: minion.endToEnd.enabled
// See issue: https://github.com/redpanda-data/kminion/issues/179
keys := make(map[string]interface{}, len(k.Keys()))
for _, key := range k.Keys() {
keys[strings.ToLower(key)] = k.Get(key)
}
k.Delete("")
err = k.Load(confmap.Provider(keys, "."), nil)
if err != nil {
return Config{}, fmt.Errorf("failed to unmarshal confmap variables into config struct: %w", err)
}

err = k.Unmarshal("", &cfg)
if err != nil {
return Config{}, err
Expand Down