about summary refs log tree commit diff
path: root/ops
diff options
context:
space:
mode:
authorArmin Schlegel <a.schlegel@gridx.de>2024-07-12T10·25+0200
committertazjin <tazjin@tvl.su>2024-08-20T17·09+0000
commit2357079891819c679821ea58a7715de7be431aaa (patch)
treef0109773dfbdcd555c48055a7aeceed67d53547c /ops
parent2beabe968ca70ce2aef8def08d7dab7340979ea6 (diff)
feat(kontemplate): defaults can now have nested values r/8546
See https://b.tvl.fyi/issues/409 for details.

Change-Id: Ibb54fab7a78e0e5f708c2a7dc8bb26ac0b2b4689
Signed-off-by: Armin Schlegel <a.schlegel@gridx.de>
Reviewed-on: https://cl.tvl.fyi/c/depot/+/11972
Tested-by: BuildkiteCI
Reviewed-by: tazjin <tazjin@tvl.su>
Diffstat (limited to 'ops')
-rw-r--r--ops/kontemplate/util/util.go18
-rw-r--r--ops/kontemplate/util/util_test.go6
2 files changed, 23 insertions, 1 deletions
diff --git a/ops/kontemplate/util/util.go b/ops/kontemplate/util/util.go
index 56fa1e3fc9c5..f96f5c1d7aa4 100644
--- a/ops/kontemplate/util/util.go
+++ b/ops/kontemplate/util/util.go
@@ -29,13 +29,29 @@ func Merge(in1 *map[string]interface{}, in2 *map[string]interface{}) *map[string
 		return in1
 	}
 
+	// The maps are map[string]interface{} with unknown depth.
+	// Loop over both maps into every level and merge them.
 	new := make(map[string]interface{})
+
 	for k, v := range *in1 {
 		new[k] = v
 	}
 
 	for k, v := range *in2 {
-		new[k] = v
+		if existing, ok := new[k]; ok {
+			// If both values are maps, merge them recursively
+			if existingMap, ok := existing.(map[string]interface{}); ok {
+				if newMap, ok := v.(map[string]interface{}); ok {
+					new[k] = *Merge(&existingMap, &newMap)
+				} else {
+					new[k] = v
+				}
+			} else {
+				new[k] = v
+			}
+		} else {
+			new[k] = v
+		}
 	}
 
 	return &new
diff --git a/ops/kontemplate/util/util_test.go b/ops/kontemplate/util/util_test.go
index 53c56081758c..328add3d250f 100644
--- a/ops/kontemplate/util/util_test.go
+++ b/ops/kontemplate/util/util_test.go
@@ -47,6 +47,9 @@ func TestMergeWithNilMap(t *testing.T) {
 func TestMergeMaps(t *testing.T) {
 	map1 := map[string]interface{}{
 		"foo": "bar",
+		"baz": map[string]interface{}{
+			"qux": "quux",
+		},
 	}
 
 	map2 := map[string]interface{}{
@@ -56,6 +59,9 @@ func TestMergeMaps(t *testing.T) {
 	result := Merge(&map1, &map2)
 	expected := map[string]interface{}{
 		"foo": "bar",
+		"baz": map[string]interface{}{
+			"qux": "quux",
+		},
 		"bar": "baz",
 	}