Skip to content

Commit

Permalink
Optimize sort
Browse files Browse the repository at this point in the history
  • Loading branch information
ash2k committed Jun 3, 2021
1 parent 670dee1 commit 79a5cc6
Showing 1 changed file with 54 additions and 44 deletions.
98 changes: 54 additions & 44 deletions pkg/ordering/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,61 +58,71 @@ func less(i, j object.ObjMetadata) bool {
}
// In case of tie, compare the namespace and name combination so that the output
// order is consistent irrespective of input order
return i.Namespace+i.Name < j.Namespace+j.Name
}

// 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 orderFirst = []string{
"Namespace",
"ResourceQuota",
"StorageClass",
"CustomResourceDefinition",
"MutatingWebhookConfiguration",
"ServiceAccount",
"PodSecurityPolicy",
"Role",
"ClusterRole",
"RoleBinding",
"ClusterRoleBinding",
"ConfigMap",
"Secret",
"Service",
"LimitRange",
"PriorityClass",
"Deployment",
"StatefulSet",
"CronJob",
"PodDisruptionBudget",
}

var orderLast = []string{
"ValidatingWebhookConfiguration",
if i.Namespace != j.Namespace {
return i.Namespace < j.Namespace
}
return i.Name < j.Name
}

// getIndexByKind returns the index of the kind respecting the order
func getIndexByKind(kind string) int {
m := map[string]int{}
var kind2index = computeKind2index()

func computeKind2index() map[string]int {
// 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.
orderFirst := []string{
"Namespace",
"ResourceQuota",
"StorageClass",
"CustomResourceDefinition",
"MutatingWebhookConfiguration",
"ServiceAccount",
"PodSecurityPolicy",
"Role",
"ClusterRole",
"RoleBinding",
"ClusterRoleBinding",
"ConfigMap",
"Secret",
"Service",
"LimitRange",
"PriorityClass",
"Deployment",
"StatefulSet",
"CronJob",
"PodDisruptionBudget",
}
orderLast := []string{
"ValidatingWebhookConfiguration",
}
kind2indexResult := make(map[string]int, len(orderFirst)+len(orderLast))
for i, n := range orderFirst {
m[n] = -len(orderFirst) + i
kind2indexResult[n] = -len(orderFirst) + i
}
for i, n := range orderLast {
m[n] = 1 + i
kind2indexResult[n] = 1 + i
}
return m[kind]
return kind2indexResult
}

func Equals(x schema.GroupKind, o schema.GroupKind) bool {
return x.Group == o.Group && x.Kind == o.Kind
// getIndexByKind returns the index of the kind respecting the order
func getIndexByKind(kind string) int {
return kind2index[kind]
}

func IsLessThan(x schema.GroupKind, o schema.GroupKind) bool {
indexI := getIndexByKind(x.Kind)
indexJ := getIndexByKind(o.Kind)
func Equals(i, j schema.GroupKind) bool {
return i.Group == j.Group && i.Kind == j.Kind
}

func IsLessThan(i, j schema.GroupKind) bool {
indexI := getIndexByKind(i.Kind)
indexJ := getIndexByKind(j.Kind)
if indexI != indexJ {
return indexI < indexJ
}
return x.String() < o.String()
if i.Group != j.Group {
return i.Group < j.Group
}
return i.Kind < j.Kind
}

0 comments on commit 79a5cc6

Please sign in to comment.