This repository has been archived by the owner on Sep 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 175
/
sort.go
158 lines (141 loc) · 3.78 KB
/
sort.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright 2018 The ksonnet authors
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cluster
import (
"sort"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
// UnstructuredSlice is a sortable slice of k8s unstructured.Unstructured objects
type UnstructuredSlice []*unstructured.Unstructured
// Sort sorts an UnstructuredSlice
func (u UnstructuredSlice) Sort() {
sort.Stable(u)
}
func (u UnstructuredSlice) Len() int {
return len(u)
}
func (u UnstructuredSlice) Swap(i, j int) {
u[i], u[j] = u[j], u[i]
}
func (u UnstructuredSlice) Less(i, j int) bool {
// Ordered sort key extractors
sorters := []func(a, b *unstructured.Unstructured) (less bool, keepGoing bool){
func(a, b *unstructured.Unstructured) (bool, bool) {
return lessString(a.GetNamespace(), b.GetNamespace())
},
func(a, b *unstructured.Unstructured) (bool, bool) {
return lessKind(a.GetKind(), b.GetKind())
},
func(a, b *unstructured.Unstructured) (bool, bool) {
return lessString(
a.GroupVersionKind().GroupVersion().String(),
b.GroupVersionKind().GroupVersion().String(),
)
},
func(a, b *unstructured.Unstructured) (bool, bool) {
return lessString(a.GetName(), b.GetName())
},
func(a, b *unstructured.Unstructured) (bool, bool) {
return lessString(a.GetGenerateName(), b.GetGenerateName())
},
func(a, b *unstructured.Unstructured) (bool, bool) {
return lessString(string(a.GetUID()), string(b.GetUID()))
},
}
a := u[i]
b := u[j]
switch {
case a == nil:
return true
case b == nil:
return false
}
for _, f := range sorters {
// When keys are equal, we will continue to the next sort level to break the tie.
if less, equal := f(a, b); !equal {
return less
}
}
return false
}
func lessString(a, b string) (less bool, equal bool) {
switch {
case a < b:
return true, false
case b < a:
return false, false
default:
// Not done, resume to next comparison key
return false, true
}
}
// Compares k8s resource kinds, applies semantic ordering (order we want to apply in)
func lessKind(a, b string) (less bool, equal bool) {
if a == b {
// Tell caller we will need to continue to next sort level for a tie-breaker
return false, true
}
aRank, aOk := kindOrderMap[a]
bRank, bOk := kindOrderMap[b]
switch {
// Known order kinds come before unknown order kinds
case aOk && !bOk:
return true, false
case bOk && !aOk:
return false, false
case aOk && bOk:
return aRank < bRank, false
}
// Fallback to lexical comparison
return a < b, false
}
// Order we should apply resources to a cluster.
// Borrowed from https://github.com/helm/helm/blob/7cad59091a9451b2aa4f95aa882ea27e6b195f98/pkg/tiller/kind_sorter.go
var kindOrder = []string{
"Namespace",
"ResourceQuota",
"LimitRange",
"PodSecurityPolicy",
"Secret",
"ConfigMap",
"StorageClass",
"PersistentVolume",
"PersistentVolumeClaim",
"ServiceAccount",
"CustomResourceDefinition",
"ClusterRole",
"ClusterRoleBinding",
"Role",
"RoleBinding",
"Service",
"DaemonSet",
"Pod",
"ReplicationController",
"ReplicaSet",
"Deployment",
"StatefulSet",
"Job",
"CronJob",
"Ingress",
"APIService",
}
var kindOrderMap map[string]int
func init() {
// Build lookup table for sorting
kindOrderMap = make(map[string]int)
for i, kind := range kindOrder {
kindOrderMap[kind] = i
}
}