Repository: grafana/tanka
Pull request #264 (2020-04-19 22:13:16)

fix(examples): prom-grafana dependencies
Were installed using a development version of jb that apparently never
worked at all


Patch:
diff --git a/examples/prom-grafana/jsonnetfile.json b/examples/prom-grafana/jsonnetfile.json
index 655fa8d7..0db30b86 100644
--- a/examples/prom-grafana/jsonnetfile.json
+++ b/examples/prom-grafana/jsonnetfile.json
@@ -1,5 +1,5 @@
 {
-  "goImportStyle": false,
+  "version": 1,
   "dependencies": [
     {
       "source": {
@@ -19,5 +19,6 @@
       },
       "version": "master"
     }
-  ]
+  ],
+  "legacyImports": true
 }
diff --git a/examples/prom-grafana/jsonnetfile.lock.json b/examples/prom-grafana/jsonnetfile.lock.json
index 73f9f087..22f79437 100644
--- a/examples/prom-grafana/jsonnetfile.lock.json
+++ b/examples/prom-grafana/jsonnetfile.lock.json
@@ -1,5 +1,5 @@
 {
-  "goImportStyle": false,
+  "version": 1,
   "dependencies": [
     {
       "source": {
@@ -8,8 +8,8 @@
           "subdir": "ksonnet-util"
         }
       },
-      "version": "f8a06bd7de550bd73921526a040f0db465133059",
-      "sum": "R5bR+TRNe7mWXLIDz84lJH58rFJU3+ieIb4xbhu+vyE="
+      "version": "b7d0399a4c8b9fe3ee381b3dc8752e7c778b3f1a",
+      "sum": "+NXJP8shQKw88Rs/X1firb7V2NzRSUvujBsMkKGh3e4="
     },
     {
       "source": {
@@ -21,5 +21,6 @@
       "version": "0d2f82676817bbf9e4acf6495b2090205f323b9f",
       "sum": "ur22hPQq0JAPBxm8hNMcwjumj4MkozDwOKiGZvVMYh4="
     }
-  ]
+  ],
+  "legacyImports": false
 }
diff --git a/pkg/kubernetes/reconcile.go b/pkg/kubernetes/reconcile.go
index 3d23b38a..ae397c4c 100644
--- a/pkg/kubernetes/reconcile.go
+++ b/pkg/kubernetes/reconcile.go
@@ -113,6 +113,11 @@ func Reconcile(raw map[string]interface{}, spec v1alpha1.Spec, targets []*regexp
 			return out[i].Kind() < out[j].Kind()
 		}
 
+		// If namespaces differ, sort by the namespace.
+		if out[i].Metadata().Namespace() != out[j].Metadata().Namespace() {
+			return out[i].Metadata().Namespace() < out[j].Metadata().Namespace()
+		}
+
 		// Otherwise, order the objects by name.
 		return out[i].Metadata().Name() < out[j].Metadata().Name()
 	})


Test patch:
diff --git a/pkg/kubernetes/reconcile_test.go b/pkg/kubernetes/reconcile_test.go
index 9a594091..cf89785a 100644
--- a/pkg/kubernetes/reconcile_test.go
+++ b/pkg/kubernetes/reconcile_test.go
@@ -1,10 +1,14 @@
 package kubernetes
 
 import (
+	"regexp"
 	"testing"
 
 	"github.com/stretchr/testify/assert"
 	"github.com/stretchr/testify/require"
+
+	"github.com/grafana/tanka/pkg/kubernetes/manifest"
+	"github.com/grafana/tanka/pkg/spec/v1alpha1"
 )
 
 func TestExtract(t *testing.T) {
@@ -54,3 +58,130 @@ func TestExtract(t *testing.T) {
 		})
 	}
 }
+
+func mkobj(kind string, name string, ns string) map[string]interface{} {
+	ret := map[string]interface{}{
+		"kind":       kind,
+		"apiVersion": "apiversion",
+		"metadata": map[string]interface{}{
+			"name": name,
+		},
+	}
+	if ns != "" {
+		ret["metadata"].(map[string]interface{})["namespace"] = ns
+	}
+
+	return ret
+}
+
+func TestReconcileSorting(t *testing.T) {
+	tests := []struct {
+		raw     map[string]interface{}
+		targets []*regexp.Regexp
+		state   manifest.List
+		err     error
+	}{
+		{
+			// sorting by kinds in the `kindOrder` list
+			raw: map[string]interface{}{
+				"a": mkobj("Service", "service", "default"),
+				"b": mkobj("Deployment", "deployment", "default"),
+				"c": mkobj("CustomResourceDefinition", "crd", ""),
+			},
+			state: manifest.List{
+				mkobj("CustomResourceDefinition", "crd", ""),
+				mkobj("Service", "service", "default"),
+				mkobj("Deployment", "deployment", "default"),
+			},
+		},
+		{
+			// alphabtical sorting by kinds outside `kindOrder` list
+			raw: map[string]interface{}{
+				"a": mkobj("B", "b", "default"),
+				"b": mkobj("C", "c", "default"),
+				"c": mkobj("A", "a", "default"),
+			},
+			state: manifest.List{
+				mkobj("A", "a", "default"),
+				mkobj("B", "b", "default"),
+				mkobj("C", "c", "default"),
+			},
+		},
+		{
+			// sorting by the namespace if kinds match
+			raw: map[string]interface{}{
+				"a": mkobj("Service", "service", "default2"),
+				"b": mkobj("Service", "service", "default"),
+				"c": mkobj("Service", "service", "default1"),
+			},
+			state: manifest.List{
+				mkobj("Service", "service", "default"),
+				mkobj("Service", "service", "default1"),
+				mkobj("Service", "service", "default2"),
+			},
+		},
+		{
+			// sorting by the names if both kinds and namespaces match
+			raw: map[string]interface{}{
+				"a": mkobj("Service", "service2", "default"),
+				"b": mkobj("Service", "service", "default"),
+				"c": mkobj("Service", "service1", "default"),
+			},
+			state: manifest.List{
+				mkobj("Service", "service", "default"),
+				mkobj("Service", "service1", "default"),
+				mkobj("Service", "service2", "default"),
+			},
+		},
+		{
+			// sorting by the names if both kinds match and there are no namespaces
+			raw: map[string]interface{}{
+				"a": mkobj("CustomResourceDefinition", "crd2", ""),
+				"b": mkobj("CustomResourceDefinition", "crd", ""),
+				"c": mkobj("CustomResourceDefinition", "crd1", ""),
+			},
+			state: manifest.List{
+				mkobj("CustomResourceDefinition", "crd", ""),
+				mkobj("CustomResourceDefinition", "crd1", ""),
+				mkobj("CustomResourceDefinition", "crd2", ""),
+			},
+		},
+		{
+			raw: map[string]interface{}{
+				"a": mkobj("Deployment", "b", "a"),
+				"b": mkobj("ConfigMap", "a", "a"),
+				"c": mkobj("Issuer", "a", "a"),
+				"d": mkobj("Service", "b", "a"),
+				"e": mkobj("Service", "a", "a"),
+				"f": mkobj("Deployment", "a", "a"),
+				"g": mkobj("ConfigMap", "a", "b"),
+				"h": mkobj("Issuer", "b", "a"),
+				"i": mkobj("Service", "b", "b"),
+				"j": mkobj("Deployment", "a", "b"),
+				"k": mkobj("ConfigMap", "b", "a"),
+				"l": mkobj("Issuer", "a", "b"),
+			},
+			state: manifest.List{
+				mkobj("ConfigMap", "a", "a"),
+				mkobj("ConfigMap", "b", "a"),
+				mkobj("ConfigMap", "a", "b"),
+				mkobj("Service", "a", "a"),
+				mkobj("Service", "b", "a"),
+				mkobj("Service", "b", "b"),
+				mkobj("Deployment", "a", "a"),
+				mkobj("Deployment", "b", "a"),
+				mkobj("Deployment", "a", "b"),
+				mkobj("Issuer", "a", "a"),
+				mkobj("Issuer", "b", "a"),
+				mkobj("Issuer", "a", "b"),
+			},
+		},
+	}
+
+	for _, test := range tests {
+		res, err := Reconcile(test.raw, v1alpha1.New().Spec, test.targets)
+
+		require.NoError(t, err)
+		require.Equal(t, test.state, res)
+	}
+}