Skip to content

Commit

Permalink
Order ValidatingWebhookConfig last.
Browse files Browse the repository at this point in the history
Fixes the cert-manager example of kubernetes-sigs#821.
  • Loading branch information
mgoltzsche committed May 22, 2019
1 parent 7dc1eae commit a09b42b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
29 changes: 13 additions & 16 deletions pkg/gvk/gvk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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()
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/gvk/gvk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit a09b42b

Please sign in to comment.