Order ValidatingWebhookConfig last.

Fixes the cert-manager example of #821.
This commit is contained in:
Max Goltzsche
2019-05-22 23:38:25 +02:00
parent 7dc1eae40f
commit a09b42b364
2 changed files with 27 additions and 16 deletions

View File

@@ -92,12 +92,11 @@ func (x Gvk) Equals(o Gvk) bool {
// 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 orderFirst = []string{
"Namespace",
"StorageClass",
"CustomResourceDefinition",
"MutatingWebhookConfiguration",
"ValidatingWebhookConfiguration",
"ServiceAccount",
"PodSecurityPolicy",
"Role",
@@ -113,28 +112,26 @@ var order = []string{
"CronJob",
"PodDisruptionBudget",
}
var orderLast = []string{
"ValidatingWebhookConfiguration",
}
var typeOrders = func() map[string]int {
m := map[string]int{}
for i, n := range order {
m[n] = i
for i, n := range orderFirst {
m[n] = -len(orderFirst) + i
}
for i, n := range orderLast {
m[n] = 1 + i
}
return m
}()
// IsLessThan returns true if self is less than the argument.
func (x Gvk) IsLessThan(o Gvk) bool {
indexI, foundI := typeOrders[x.Kind]
indexJ, foundJ := typeOrders[o.Kind]
if foundI && foundJ {
if indexI != indexJ {
return indexI < indexJ
}
}
if foundI && !foundJ {
return true
}
if !foundI && foundJ {
return false
indexI := typeOrders[x.Kind]
indexJ := typeOrders[o.Kind]
if indexI != indexJ {
return indexI < indexJ
}
return x.String() < o.String()
}

View File

@@ -58,6 +58,20 @@ var lessThanTests = []struct {
Gvk{Group: "a", Version: "b", Kind: "ClusterRole"}},
{Gvk{Group: "a", Version: "d", Kind: "Namespace"},
Gvk{Group: "b", Version: "c", Kind: "Namespace"}},
{Gvk{Group: "a", Version: "b", Kind: orderFirst[len(orderFirst)-1]},
Gvk{Group: "a", Version: "b", Kind: orderLast[0]}},
{Gvk{Group: "a", Version: "b", Kind: orderFirst[len(orderFirst)-1]},
Gvk{Group: "a", Version: "b", Kind: "CustomKindX"}},
{Gvk{Group: "a", Version: "b", Kind: "CustomKindX"},
Gvk{Group: "a", Version: "b", Kind: orderLast[0]}},
{Gvk{Group: "a", Version: "b", Kind: "CustomKindA"},
Gvk{Group: "a", Version: "b", Kind: "CustomKindB"}},
{Gvk{Group: "a", Version: "b", Kind: "CustomKindX"},
Gvk{Group: "a", Version: "b", Kind: "ValidatingWebhookConfiguration"}},
{Gvk{Group: "a", Version: "b", Kind: "APIService"},
Gvk{Group: "a", Version: "b", Kind: "ValidatingWebhookConfiguration"}},
{Gvk{Group: "a", Version: "b", Kind: "Service"},
Gvk{Group: "a", Version: "b", Kind: "APIService"}},
}
func TestIsLessThan1(t *testing.T) {