Increase sort determinism.

This commit is contained in:
Jeffrey Regan
2018-09-27 10:45:54 -07:00
parent acf989f1be
commit 317833aeff
7 changed files with 106 additions and 47 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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,
}

View File

@@ -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{}

View File

@@ -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) {

View File

@@ -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

View File

@@ -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) {