Repository: grafana/tanka (go)

Issue:
Support nil type for disabling objects
Maybe there is already a way to do this, but we often set up templates with the ability to "disable" kube objects like so:

{
   run_load_balancer: false,
   if run_load_balancer then {
       ...
   } else null,
  ... 
}

Currently tanka errors out on nil https://github.com/grafana/tanka/blob/9f62754982952285f7a7dc62750360b6a1213bc0/pkg/kubernetes/reconcile.go#L112

Pull request description:
fix(kubernetes): ignore nil during reconcile
While extracting the objects from the Jsonnet output, Tanka was not correctly
dealing with keys that were set to `nil`, leading those to be classified as
invalid objects, while they were no objects at all.

Such a case, where a key is explicitely set to `nil` is Jsonnet if:

```jsonnet
{
  key: if false then "ouch"
}
```

Because the condition is false, Jsonnet adds an implicit `else null`.

Fixes #160 

Patch:
diff --git a/pkg/kubernetes/reconcile.go b/pkg/kubernetes/reconcile.go
index 78d1a8cc..b8ecf551 100644
--- a/pkg/kubernetes/reconcile.go
+++ b/pkg/kubernetes/reconcile.go
@@ -99,9 +99,13 @@ func walkJSON(deep interface{}, extracted map[string]manifest.Manifest, path tra
 
 	// walk it
 	for key, d := range deep.(map[string]interface{}) {
-		if key == "__ksonnet" {
+		switch {
+		case key == "__ksonnet": // exclude ksonnet object
+			continue
+		case d == nil: // result from false if condition in Jsonnet
 			continue
 		}
+
 		path := append(path, key)
 
 		if _, ok := d.(map[string]interface{}); !ok {


Test patch:
diff --git a/pkg/kubernetes/reconcile_test.go b/pkg/kubernetes/reconcile_test.go
index fdecf160..d16c13d3 100644
--- a/pkg/kubernetes/reconcile_test.go
+++ b/pkg/kubernetes/reconcile_test.go
@@ -34,6 +34,15 @@ func TestExtract(t *testing.T) {
 			name: "array",
 			data: testDataArray(),
 		},
+		{
+			name: "nil",
+			data: func() testData {
+				d := testDataRegular()
+				d.deep.(map[string]interface{})["disabledObject"] = nil
+				return d
+			}(),
+			err: nil, // we expect no error, just the result of testDataRegular
+		},
 	}
 
 	for _, c := range tests {