Skip to content

Commit a38befd

Browse files
authored
Merge pull request #132 from Liujingfang1/order
correct ordering of the k8s objects
2 parents 0312cdf + 991ffbb commit a38befd

File tree

3 files changed

+95
-72
lines changed

3 files changed

+95
-72
lines changed

pkg/commands/testdata/testcase-variable-ref/expected.yaml

Lines changed: 68 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,72 @@
11
apiVersion: v1
2+
kind: ServiceAccount
3+
metadata:
4+
labels:
5+
app: cockroachdb
6+
name: dev-base-cockroachdb
7+
---
8+
apiVersion: rbac.authorization.k8s.io/v1beta1
9+
kind: Role
10+
metadata:
11+
labels:
12+
app: cockroachdb
13+
name: dev-base-cockroachdb
14+
rules:
15+
- apiGroups:
16+
- ""
17+
resources:
18+
- secrets
19+
verbs:
20+
- create
21+
- get
22+
---
23+
apiVersion: rbac.authorization.k8s.io/v1beta1
24+
kind: ClusterRole
25+
metadata:
26+
labels:
27+
app: cockroachdb
28+
name: dev-base-cockroachdb
29+
rules:
30+
- apiGroups:
31+
- certificates.k8s.io
32+
resources:
33+
- certificatesigningrequests
34+
verbs:
35+
- create
36+
- get
37+
- watch
38+
---
39+
apiVersion: rbac.authorization.k8s.io/v1beta1
40+
kind: RoleBinding
41+
metadata:
42+
labels:
43+
app: cockroachdb
44+
name: dev-base-cockroachdb
45+
roleRef:
46+
apiGroup: rbac.authorization.k8s.io
47+
kind: Role
48+
name: dev-base-cockroachdb
49+
subjects:
50+
- kind: ServiceAccount
51+
name: dev-base-cockroachdb
52+
namespace: default
53+
---
54+
apiVersion: rbac.authorization.k8s.io/v1beta1
55+
kind: ClusterRoleBinding
56+
metadata:
57+
labels:
58+
app: cockroachdb
59+
name: dev-base-cockroachdb
60+
roleRef:
61+
apiGroup: rbac.authorization.k8s.io
62+
kind: ClusterRole
63+
name: dev-base-cockroachdb
64+
subjects:
65+
- kind: ServiceAccount
66+
name: dev-base-cockroachdb
67+
namespace: default
68+
---
69+
apiVersion: v1
270
kind: Service
371
metadata:
472
annotations:
@@ -38,13 +106,6 @@ spec:
38106
selector:
39107
app: cockroachdb
40108
---
41-
apiVersion: v1
42-
kind: ServiceAccount
43-
metadata:
44-
labels:
45-
app: cockroachdb
46-
name: dev-base-cockroachdb
47-
---
48109
apiVersion: apps/v1beta1
49110
kind: StatefulSet
50111
metadata:
@@ -142,64 +203,3 @@ spec:
142203
selector:
143204
matchLabels:
144205
app: cockroachdb
145-
---
146-
apiVersion: rbac.authorization.k8s.io/v1beta1
147-
kind: ClusterRole
148-
metadata:
149-
labels:
150-
app: cockroachdb
151-
name: dev-base-cockroachdb
152-
rules:
153-
- apiGroups:
154-
- certificates.k8s.io
155-
resources:
156-
- certificatesigningrequests
157-
verbs:
158-
- create
159-
- get
160-
- watch
161-
---
162-
apiVersion: rbac.authorization.k8s.io/v1beta1
163-
kind: ClusterRoleBinding
164-
metadata:
165-
labels:
166-
app: cockroachdb
167-
name: dev-base-cockroachdb
168-
roleRef:
169-
apiGroup: rbac.authorization.k8s.io
170-
kind: ClusterRole
171-
name: dev-base-cockroachdb
172-
subjects:
173-
- kind: ServiceAccount
174-
name: dev-base-cockroachdb
175-
namespace: default
176-
---
177-
apiVersion: rbac.authorization.k8s.io/v1beta1
178-
kind: Role
179-
metadata:
180-
labels:
181-
app: cockroachdb
182-
name: dev-base-cockroachdb
183-
rules:
184-
- apiGroups:
185-
- ""
186-
resources:
187-
- secrets
188-
verbs:
189-
- create
190-
- get
191-
---
192-
apiVersion: rbac.authorization.k8s.io/v1beta1
193-
kind: RoleBinding
194-
metadata:
195-
labels:
196-
app: cockroachdb
197-
name: dev-base-cockroachdb
198-
roleRef:
199-
apiGroup: rbac.authorization.k8s.io
200-
kind: Role
201-
name: dev-base-cockroachdb
202-
subjects:
203-
- kind: ServiceAccount
204-
name: dev-base-cockroachdb
205-
namespace: default

pkg/resmap/idslice.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,27 @@ func (a IdSlice) Less(i, j int) bool {
3737
return a[i].Name() < a[j].Name()
3838
}
3939

40+
var order = []string{"Namespace", "CustomResourceDefinition", "ServiceAccount",
41+
"Role", "ClusterRole", "RoleBinding", "ClusterRoleBinding"}
42+
var typeOrders = func() map[string]int {
43+
m := map[string]int{}
44+
for i, n := range order {
45+
m[n] = i
46+
}
47+
return m
48+
}()
49+
4050
func gvkLess(i, j schema.GroupVersionKind) bool {
41-
if i.Kind == "Namespace" {
51+
indexi, foundi := typeOrders[i.Kind]
52+
indexj, foundj := typeOrders[j.Kind]
53+
if foundi && foundj {
54+
return indexi < indexj
55+
}
56+
if foundi && !foundj {
4257
return true
43-
} else if j.Kind == "Namespace" {
58+
}
59+
if !foundi && foundj {
4460
return false
45-
} else {
46-
return i.String() < j.String()
4761
}
62+
return i.String() < j.String()
4863
}

pkg/resmap/idslice_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,18 @@ func TestLess(t *testing.T) {
3131
resource.NewResId(schema.GroupVersionKind{Kind: "Pod"}, "pod"),
3232
resource.NewResId(schema.GroupVersionKind{Kind: "Namespace"}, "ns1"),
3333
resource.NewResId(schema.GroupVersionKind{Kind: "Namespace"}, "ns2"),
34+
resource.NewResId(schema.GroupVersionKind{Kind: "Role"}, "ro"),
35+
resource.NewResId(schema.GroupVersionKind{Kind: "RoleBinding"}, "rb"),
36+
resource.NewResId(schema.GroupVersionKind{Kind: "CustomResourceDefinition"}, "crd"),
37+
resource.NewResId(schema.GroupVersionKind{Kind: "ServiceAccount"}, "sa"),
3438
}
3539
expected := IdSlice{
3640
resource.NewResId(schema.GroupVersionKind{Kind: "Namespace"}, "ns1"),
3741
resource.NewResId(schema.GroupVersionKind{Kind: "Namespace"}, "ns2"),
42+
resource.NewResId(schema.GroupVersionKind{Kind: "CustomResourceDefinition"}, "crd"),
43+
resource.NewResId(schema.GroupVersionKind{Kind: "ServiceAccount"}, "sa"),
44+
resource.NewResId(schema.GroupVersionKind{Kind: "Role"}, "ro"),
45+
resource.NewResId(schema.GroupVersionKind{Kind: "RoleBinding"}, "rb"),
3846
resource.NewResId(schema.GroupVersionKind{Kind: "ConfigMap"}, "cm"),
3947
resource.NewResId(schema.GroupVersionKind{Kind: "Pod"}, "pod"),
4048
}

0 commit comments

Comments
 (0)