How to inject a complex YAML config (arrays & nested objects) into a Go application running on Kubernetes (using Helm)?

4 days ago 5
ARTICLE AD BOX

I have a Go application running on Kubernetes, deployed using Helm. The application loads configuration using koanf, supporting both YAML files and environment variables.

I have a complex configuration like this:

yaml cron: jobs: - job_kind: daily topic: Topic1 interval: 1 day_of_month_to_start: 1 day_of_month_to_stop: 31 at_times: - hour: 05 minute: 00 second: 00 - hour: 11 minute: 00 second: 00 - job_kind: monthly topic: Topic2 interval: 1 days_of_the_month: - -1 at_times: - hour: 21 minute: 30 second: 0

I load the config in Go like this:

options := cfgloader.Option{ Prefix: "MYAPP_", Delimiter: ".", Separator: "__", YamlFilePath: "/path/to/config.yaml", } func Load(options Option, config any) error { k := koanf.New(options.Delimiter) if options.YamlFilePath != "" { if err := k.Load(file.Provider(options.YamlFilePath), yaml.Parser()); err != nil { return err } } callback := func(source string) string { base := strings.ToLower(strings.TrimPrefix(source, options.Prefix)) return strings.ReplaceAll(base, options.Separator, ".") } if err := k.Load(env.Provider(options.Prefix, options.Delimiter, callback), nil); err != nil { return err } return k.Unmarshal("", &config) }

Problem

I’m unsure about the correct Kubernetes/Helm approach to inject this configuration into the application.

Specifically:

Environment variables are flat strings, but this config contains arrays and nested objects Helm templating for arrays into env vars becomes very verbose and error-prone

I want a clean, idiomatic Kubernetes solution.

I prefer to keep the same structure of this configuration inside Helm’s values.yaml (i.e., the same nested YAML with arrays), and avoid flattening it manually into environment variables.

What is the recommended way to inject a complex YAML config like this into a Go app running in Kubernetes?

Examples using ConfigMap + volumeMount or best practices would be appreciated.

Read Entire Article