Skip to content

Commit

Permalink
Fix parsing configs with dots in mapping keys
Browse files Browse the repository at this point in the history
  • Loading branch information
DrJosh9000 committed Oct 31, 2024
1 parent 637da43 commit 8fc8a3e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
11 changes: 9 additions & 2 deletions cmd/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,16 @@ func ReadConfigFromFileArgsAndEnv(cmd *cobra.Command, args []string) (*viper.Vip
configFile = os.Getenv("CONFIG")
}

v := viper.New()
// By default Viper unmarshals a key like "a.b.c" as nested maps:
// map[string]any{"a": map[string]any{"b": map[string]any{"c": ... }}}
// which is frustrating, because `.` is commonly used in Kubernetes labels,
// annotations, and node selector keys (they tend to use domain names to
// "namespace" keys). So change Viper's delimiter to`::`.
v := viper.NewWithOptions(
viper.KeyDelimiter("::"),
viper.EnvKeyReplacer(strings.NewReplacer("-", "_")),
)
v.SetConfigFile(configFile)
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))

// Bind the flags to the viper instance, but only those that can appear in the config file.
errs := []error{}
Expand Down
3 changes: 3 additions & 0 deletions cmd/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ func TestReadAndParseConfig(t *testing.T) {
PodSpecPatch: &corev1.PodSpec{
ServiceAccountName: "buildkite-agent-sa",
AutomountServiceAccountToken: ptr(true),
NodeSelector: map[string]string{
"selectors.example.com/my-selector": "example-value",
},
Containers: []corev1.Container{
{
Name: "container-0",
Expand Down
2 changes: 2 additions & 0 deletions examples/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ default-sidecar-params:
pod-spec-patch:
serviceAccountName: buildkite-agent-sa
automountServiceAccountToken: true
nodeSelector:
selectors.example.com/my-selector: example-value
containers:
- name: container-0
env:
Expand Down

0 comments on commit 8fc8a3e

Please sign in to comment.