From 317833aeff5b6cbb44e8fd638c7799145651908b Mon Sep 17 00:00:00 2001 From: Jeffrey Regan Date: Thu, 27 Sep 2018 10:45:54 -0700 Subject: [PATCH] Increase sort determinism. --- .../testdata/skip-testcase-crds/expected.yaml | 14 +++--- .../testcase-variable-ref/expected.yaml | 16 +++---- pkg/crds/crds_test.go | 5 -- pkg/gvk/gvk.go | 31 ++++++++++-- pkg/gvk/gvk_test.go | 12 +++-- pkg/resource/resid.go | 27 +++++++++-- pkg/resource/resid_test.go | 48 ++++++++++++------- 7 files changed, 106 insertions(+), 47 deletions(-) diff --git a/pkg/commands/testdata/skip-testcase-crds/expected.yaml b/pkg/commands/testdata/skip-testcase-crds/expected.yaml index 8116db1e6..3485e6556 100644 --- a/pkg/commands/testdata/skip-testcase-crds/expected.yaml +++ b/pkg/commands/testdata/skip-testcase-crds/expected.yaml @@ -5,6 +5,13 @@ kind: Secret metadata: name: test-crdsecret --- +apiVersion: v1beta1 +kind: Bee +metadata: + name: test-bee +spec: + action: fly +--- apiVersion: jingfang.example.com/v1beta1 kind: MyKind metadata: @@ -14,10 +21,3 @@ spec: name: test-bee secretRef: name: test-crdsecret ---- -apiVersion: v1beta1 -kind: Bee -metadata: - name: test-bee -spec: - action: fly diff --git a/pkg/commands/testdata/testcase-variable-ref/expected.yaml b/pkg/commands/testdata/testcase-variable-ref/expected.yaml index ab030bf5b..af28d3c61 100644 --- a/pkg/commands/testdata/testcase-variable-ref/expected.yaml +++ b/pkg/commands/testdata/testcase-variable-ref/expected.yaml @@ -77,10 +77,16 @@ metadata: apiVersion: v1 kind: Service metadata: + annotations: + prometheus.io/path: _status/vars + prometheus.io/port: "8080" + prometheus.io/scrape: "true" + service.alpha.kubernetes.io/tolerate-unready-endpoints: "true" labels: app: cockroachdb - name: dev-base-cockroachdb-public + name: dev-base-cockroachdb spec: + clusterIP: None ports: - name: grpc port: 26257 @@ -94,16 +100,10 @@ spec: apiVersion: v1 kind: Service metadata: - annotations: - prometheus.io/path: _status/vars - prometheus.io/port: "8080" - prometheus.io/scrape: "true" - service.alpha.kubernetes.io/tolerate-unready-endpoints: "true" labels: app: cockroachdb - name: dev-base-cockroachdb + name: dev-base-cockroachdb-public spec: - clusterIP: None ports: - name: grpc port: 26257 diff --git a/pkg/crds/crds_test.go b/pkg/crds/crds_test.go index 62b7dac39..c290d8661 100644 --- a/pkg/crds/crds_test.go +++ b/pkg/crds/crds_test.go @@ -18,7 +18,6 @@ package crds import ( "reflect" - "sort" "testing" "sigs.k8s.io/kustomize/pkg/gvk" @@ -174,10 +173,6 @@ func TestRegisterCRD(t *testing.T) { }, } - sort.Slice(refpathconfigs, func(i, j int) bool { - return refpathconfigs[i].Gvk.String() < refpathconfigs[j].Gvk.String() - }) - expected := &transformerconfig.TransformerConfig{ NameReference: refpathconfigs, } diff --git a/pkg/gvk/gvk.go b/pkg/gvk/gvk.go index e8eb23a60..dcdcde72c 100644 --- a/pkg/gvk/gvk.go +++ b/pkg/gvk/gvk.go @@ -18,6 +18,7 @@ package gvk import ( "k8s.io/apimachinery/pkg/runtime/schema" + "strings" ) // Gvk identifies a Kubernetes API type. @@ -53,12 +54,28 @@ func (x Gvk) ToSchemaGvk() schema.GroupVersionKind { } } +const ( + noGroup = "noGroup" + noVersion = "noVersion" + noKind = "noKind" + separator = "_" +) + // String returns a string representation of the GVK. func (x Gvk) String() string { - if x.Group == "" { - return x.Version + "_" + x.Kind + g := x.Group + if g == "" { + g = noGroup } - return x.Group + "_" + x.Version + "_" + x.Kind + v := x.Version + if v == "" { + v = noVersion + } + k := x.Kind + if k == "" { + k = noKind + } + return strings.Join([]string{g, v, k}, separator) } // Equals returns true if the Gvk's have equal fields. @@ -66,6 +83,10 @@ func (x Gvk) Equals(o Gvk) bool { return x.Group == o.Group && x.Version == o.Version && x.Kind == o.Kind } +// An attempt to order things to help k8s, e.g. +// a Service should come before things that refer to it. +// Namespace should be first. +// In some cases order just specified to provide determinism. var order = []string{ "Namespace", "CustomResourceDefinition", @@ -77,6 +98,10 @@ var order = []string{ "ConfigMap", "Secret", "Service", + "Deployment", + "StatefulSet", + "CronJob", + "PodDisruptionBudget", } var typeOrders = func() map[string]int { m := map[string]int{} diff --git a/pkg/gvk/gvk_test.go b/pkg/gvk/gvk_test.go index 4ecfa2ed5..469e603aa 100644 --- a/pkg/gvk/gvk_test.go +++ b/pkg/gvk/gvk_test.go @@ -62,10 +62,14 @@ var stringTests = []struct { x Gvk s string }{ - {Gvk{Group: "a", Version: "b", Kind: "c"}, "a_b_c"}, - {Gvk{Group: "a", Kind: "c"}, "a__c"}, - {Gvk{Kind: "c"}, "_c"}, - {Gvk{Version: "b", Kind: "c"}, "b_c"}, + {Gvk{}, "noGroup_noVersion_noKind"}, + {Gvk{Kind: "k"}, "noGroup_noVersion_k"}, + {Gvk{Version: "v"}, "noGroup_v_noKind"}, + {Gvk{Version: "v", Kind: "k"}, "noGroup_v_k"}, + {Gvk{Group: "g"}, "g_noVersion_noKind"}, + {Gvk{Group: "g", Kind: "k"}, "g_noVersion_k"}, + {Gvk{Group: "g", Version: "v"}, "g_v_noKind"}, + {Gvk{Group: "g", Version: "v", Kind: "k"}, "g_v_k"}, } func TestString(t *testing.T) { diff --git a/pkg/resource/resid.go b/pkg/resource/resid.go index c8f38eb8a..954914fb9 100644 --- a/pkg/resource/resid.go +++ b/pkg/resource/resid.go @@ -58,16 +58,35 @@ func NewResIdKindOnly(k string, n string) ResId { return ResId{gvKind: gvk.FromKind(k), name: n} } +const ( + noNamespace = "noNamespace" + noPrefix = "noPrefix" + noName = "noName" + separator = "|" +) + // String of ResId based on GVK, name and prefix func (n ResId) String() string { - fields := []string{n.gvKind.Group, n.gvKind.Version, n.gvKind.Kind, - n.namespace, n.prefix, n.name} - return strings.Join(fields, "_") + ".yaml" + ns := n.namespace + if ns == "" { + ns = noNamespace + } + p := n.prefix + if p == "" { + p = noPrefix + } + nm := n.name + if nm == "" { + nm = noName + } + + return strings.Join( + []string{n.gvKind.String(), ns, p, nm}, separator) } // GvknString of ResId based on GVK and name func (n ResId) GvknString() string { - return n.gvKind.String() + "_" + n.name + ".yaml" + return n.gvKind.String() + separator + n.name } // GvknEquals return if two ResId have the same Group/Version/Kind and name diff --git a/pkg/resource/resid_test.go b/pkg/resource/resid_test.go index 57ff82570..221499a59 100644 --- a/pkg/resource/resid_test.go +++ b/pkg/resource/resid_test.go @@ -11,19 +11,27 @@ var stringTests = []struct { s string }{ {ResId{gvKind: gvk.Gvk{Group: "g", Version: "v", Kind: "k"}, - name: "nm", prefix: "p", namespace: "ns"}, "g_v_k_ns_p_nm.yaml"}, + name: "nm", prefix: "p", namespace: "ns"}, + "g_v_k|ns|p|nm"}, {ResId{gvKind: gvk.Gvk{Version: "v", Kind: "k"}, - name: "nm", prefix: "p", namespace: "ns"}, "_v_k_ns_p_nm.yaml"}, + name: "nm", prefix: "p", namespace: "ns"}, + "noGroup_v_k|ns|p|nm"}, {ResId{gvKind: gvk.Gvk{Kind: "k"}, - name: "nm", prefix: "p", namespace: "ns"}, "__k_ns_p_nm.yaml"}, + name: "nm", prefix: "p", namespace: "ns"}, + "noGroup_noVersion_k|ns|p|nm"}, {ResId{gvKind: gvk.Gvk{}, - name: "nm", prefix: "p", namespace: "ns"}, "___ns_p_nm.yaml"}, + name: "nm", prefix: "p", namespace: "ns"}, + "noGroup_noVersion_noKind|ns|p|nm"}, {ResId{gvKind: gvk.Gvk{}, - name: "nm", prefix: "p"}, "____p_nm.yaml"}, + name: "nm", prefix: "p"}, + "noGroup_noVersion_noKind|noNamespace|p|nm"}, {ResId{gvKind: gvk.Gvk{}, - name: "nm"}, "_____nm.yaml"}, - {ResId{gvKind: gvk.Gvk{}}, "_____.yaml"}, - {ResId{}, "_____.yaml"}, + name: "nm"}, + "noGroup_noVersion_noKind|noNamespace|noPrefix|nm"}, + {ResId{gvKind: gvk.Gvk{}}, + "noGroup_noVersion_noKind|noNamespace|noPrefix|noName"}, + {ResId{}, + "noGroup_noVersion_noKind|noNamespace|noPrefix|noName"}, } func TestString(t *testing.T) { @@ -39,19 +47,27 @@ var gvknStringTests = []struct { s string }{ {ResId{gvKind: gvk.Gvk{Group: "g", Version: "v", Kind: "k"}, - name: "nm", prefix: "p", namespace: "ns"}, "g_v_k_nm.yaml"}, + name: "nm", prefix: "p", namespace: "ns"}, + "g_v_k|nm"}, {ResId{gvKind: gvk.Gvk{Version: "v", Kind: "k"}, - name: "nm", prefix: "p", namespace: "ns"}, "v_k_nm.yaml"}, + name: "nm", prefix: "p", namespace: "ns"}, + "noGroup_v_k|nm"}, {ResId{gvKind: gvk.Gvk{Kind: "k"}, - name: "nm", prefix: "p", namespace: "ns"}, "_k_nm.yaml"}, + name: "nm", prefix: "p", namespace: "ns"}, + "noGroup_noVersion_k|nm"}, {ResId{gvKind: gvk.Gvk{}, - name: "nm", prefix: "p", namespace: "ns"}, "__nm.yaml"}, + name: "nm", prefix: "p", namespace: "ns"}, + "noGroup_noVersion_noKind|nm"}, {ResId{gvKind: gvk.Gvk{}, - name: "nm", prefix: "p"}, "__nm.yaml"}, + name: "nm", prefix: "p"}, + "noGroup_noVersion_noKind|nm"}, {ResId{gvKind: gvk.Gvk{}, - name: "nm"}, "__nm.yaml"}, - {ResId{gvKind: gvk.Gvk{}}, "__.yaml"}, - {ResId{}, "__.yaml"}, + name: "nm"}, + "noGroup_noVersion_noKind|nm"}, + {ResId{gvKind: gvk.Gvk{}}, + "noGroup_noVersion_noKind|"}, + {ResId{}, + "noGroup_noVersion_noKind|"}, } func TestGvknString(t *testing.T) {