From 3b1de03a22d64f684c690bd28eae3b2a7052b70b Mon Sep 17 00:00:00 2001 From: doxsch <28098153+doxsch@users.noreply.github.com> Date: Tue, 13 Feb 2024 21:14:46 +0100 Subject: [PATCH] fix: set environment variables config values correctly for camelCase config keys --- config.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/config.go b/config.go index ac7e728..3c65e0f 100644 --- a/config.go +++ b/config.go @@ -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" @@ -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