mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 09:02:53 +00:00
Merge pull request #378 from monopole/stringStuff
Increase sort determinism.
This commit is contained in:
@@ -5,6 +5,13 @@ kind: Secret
|
|||||||
metadata:
|
metadata:
|
||||||
name: test-crdsecret
|
name: test-crdsecret
|
||||||
---
|
---
|
||||||
|
apiVersion: v1beta1
|
||||||
|
kind: Bee
|
||||||
|
metadata:
|
||||||
|
name: test-bee
|
||||||
|
spec:
|
||||||
|
action: fly
|
||||||
|
---
|
||||||
apiVersion: jingfang.example.com/v1beta1
|
apiVersion: jingfang.example.com/v1beta1
|
||||||
kind: MyKind
|
kind: MyKind
|
||||||
metadata:
|
metadata:
|
||||||
@@ -14,10 +21,3 @@ spec:
|
|||||||
name: test-bee
|
name: test-bee
|
||||||
secretRef:
|
secretRef:
|
||||||
name: test-crdsecret
|
name: test-crdsecret
|
||||||
---
|
|
||||||
apiVersion: v1beta1
|
|
||||||
kind: Bee
|
|
||||||
metadata:
|
|
||||||
name: test-bee
|
|
||||||
spec:
|
|
||||||
action: fly
|
|
||||||
|
|||||||
@@ -77,10 +77,16 @@ metadata:
|
|||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Service
|
kind: Service
|
||||||
metadata:
|
metadata:
|
||||||
|
annotations:
|
||||||
|
prometheus.io/path: _status/vars
|
||||||
|
prometheus.io/port: "8080"
|
||||||
|
prometheus.io/scrape: "true"
|
||||||
|
service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
|
||||||
labels:
|
labels:
|
||||||
app: cockroachdb
|
app: cockroachdb
|
||||||
name: dev-base-cockroachdb-public
|
name: dev-base-cockroachdb
|
||||||
spec:
|
spec:
|
||||||
|
clusterIP: None
|
||||||
ports:
|
ports:
|
||||||
- name: grpc
|
- name: grpc
|
||||||
port: 26257
|
port: 26257
|
||||||
@@ -94,16 +100,10 @@ spec:
|
|||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Service
|
kind: Service
|
||||||
metadata:
|
metadata:
|
||||||
annotations:
|
|
||||||
prometheus.io/path: _status/vars
|
|
||||||
prometheus.io/port: "8080"
|
|
||||||
prometheus.io/scrape: "true"
|
|
||||||
service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
|
|
||||||
labels:
|
labels:
|
||||||
app: cockroachdb
|
app: cockroachdb
|
||||||
name: dev-base-cockroachdb
|
name: dev-base-cockroachdb-public
|
||||||
spec:
|
spec:
|
||||||
clusterIP: None
|
|
||||||
ports:
|
ports:
|
||||||
- name: grpc
|
- name: grpc
|
||||||
port: 26257
|
port: 26257
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ package crds
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/pkg/gvk"
|
"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{
|
expected := &transformerconfig.TransformerConfig{
|
||||||
NameReference: refpathconfigs,
|
NameReference: refpathconfigs,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ package gvk
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Gvk identifies a Kubernetes API type.
|
// 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.
|
// String returns a string representation of the GVK.
|
||||||
func (x Gvk) String() string {
|
func (x Gvk) String() string {
|
||||||
if x.Group == "" {
|
g := x.Group
|
||||||
return x.Version + "_" + x.Kind
|
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.
|
// 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
|
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{
|
var order = []string{
|
||||||
"Namespace",
|
"Namespace",
|
||||||
"CustomResourceDefinition",
|
"CustomResourceDefinition",
|
||||||
@@ -77,6 +98,10 @@ var order = []string{
|
|||||||
"ConfigMap",
|
"ConfigMap",
|
||||||
"Secret",
|
"Secret",
|
||||||
"Service",
|
"Service",
|
||||||
|
"Deployment",
|
||||||
|
"StatefulSet",
|
||||||
|
"CronJob",
|
||||||
|
"PodDisruptionBudget",
|
||||||
}
|
}
|
||||||
var typeOrders = func() map[string]int {
|
var typeOrders = func() map[string]int {
|
||||||
m := map[string]int{}
|
m := map[string]int{}
|
||||||
|
|||||||
@@ -62,10 +62,14 @@ var stringTests = []struct {
|
|||||||
x Gvk
|
x Gvk
|
||||||
s string
|
s string
|
||||||
}{
|
}{
|
||||||
{Gvk{Group: "a", Version: "b", Kind: "c"}, "a_b_c"},
|
{Gvk{}, "noGroup_noVersion_noKind"},
|
||||||
{Gvk{Group: "a", Kind: "c"}, "a__c"},
|
{Gvk{Kind: "k"}, "noGroup_noVersion_k"},
|
||||||
{Gvk{Kind: "c"}, "_c"},
|
{Gvk{Version: "v"}, "noGroup_v_noKind"},
|
||||||
{Gvk{Version: "b", Kind: "c"}, "b_c"},
|
{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) {
|
func TestString(t *testing.T) {
|
||||||
|
|||||||
@@ -58,16 +58,35 @@ func NewResIdKindOnly(k string, n string) ResId {
|
|||||||
return ResId{gvKind: gvk.FromKind(k), name: n}
|
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
|
// String of ResId based on GVK, name and prefix
|
||||||
func (n ResId) String() string {
|
func (n ResId) String() string {
|
||||||
fields := []string{n.gvKind.Group, n.gvKind.Version, n.gvKind.Kind,
|
ns := n.namespace
|
||||||
n.namespace, n.prefix, n.name}
|
if ns == "" {
|
||||||
return strings.Join(fields, "_") + ".yaml"
|
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
|
// GvknString of ResId based on GVK and name
|
||||||
func (n ResId) GvknString() string {
|
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
|
// GvknEquals return if two ResId have the same Group/Version/Kind and name
|
||||||
|
|||||||
@@ -11,19 +11,27 @@ var stringTests = []struct {
|
|||||||
s string
|
s string
|
||||||
}{
|
}{
|
||||||
{ResId{gvKind: gvk.Gvk{Group: "g", Version: "v", Kind: "k"},
|
{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"},
|
{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"},
|
{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{},
|
{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{},
|
{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{},
|
{ResId{gvKind: gvk.Gvk{},
|
||||||
name: "nm"}, "_____nm.yaml"},
|
name: "nm"},
|
||||||
{ResId{gvKind: gvk.Gvk{}}, "_____.yaml"},
|
"noGroup_noVersion_noKind|noNamespace|noPrefix|nm"},
|
||||||
{ResId{}, "_____.yaml"},
|
{ResId{gvKind: gvk.Gvk{}},
|
||||||
|
"noGroup_noVersion_noKind|noNamespace|noPrefix|noName"},
|
||||||
|
{ResId{},
|
||||||
|
"noGroup_noVersion_noKind|noNamespace|noPrefix|noName"},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestString(t *testing.T) {
|
func TestString(t *testing.T) {
|
||||||
@@ -39,19 +47,27 @@ var gvknStringTests = []struct {
|
|||||||
s string
|
s string
|
||||||
}{
|
}{
|
||||||
{ResId{gvKind: gvk.Gvk{Group: "g", Version: "v", Kind: "k"},
|
{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"},
|
{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"},
|
{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{},
|
{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{},
|
{ResId{gvKind: gvk.Gvk{},
|
||||||
name: "nm", prefix: "p"}, "__nm.yaml"},
|
name: "nm", prefix: "p"},
|
||||||
|
"noGroup_noVersion_noKind|nm"},
|
||||||
{ResId{gvKind: gvk.Gvk{},
|
{ResId{gvKind: gvk.Gvk{},
|
||||||
name: "nm"}, "__nm.yaml"},
|
name: "nm"},
|
||||||
{ResId{gvKind: gvk.Gvk{}}, "__.yaml"},
|
"noGroup_noVersion_noKind|nm"},
|
||||||
{ResId{}, "__.yaml"},
|
{ResId{gvKind: gvk.Gvk{}},
|
||||||
|
"noGroup_noVersion_noKind|"},
|
||||||
|
{ResId{},
|
||||||
|
"noGroup_noVersion_noKind|"},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGvknString(t *testing.T) {
|
func TestGvknString(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user