Skip to content

Commit e490fba

Browse files
committed
Make order of generated yaml from kubebuilder create config --crds deterministic
1 parent 225cbc3 commit e490fba

File tree

2 files changed

+143
-6
lines changed

2 files changed

+143
-6
lines changed

cmd/kubebuilder/create/config/gen.go

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package config
1818

1919
import (
2020
"fmt"
21+
"sort"
2122
"strings"
2223

2324
"github.com/ghodss/yaml"
@@ -28,6 +29,7 @@ import (
2829
appsv1 "k8s.io/api/apps/v1"
2930
corev1 "k8s.io/api/core/v1"
3031
rbacv1 "k8s.io/api/rbac/v1"
32+
extensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
3133
"k8s.io/apimachinery/pkg/api/resource"
3234
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3335
"k8s.io/gengo/args"
@@ -274,7 +276,7 @@ func getPodTemplate(labels map[string]string) corev1.PodTemplateSpec {
274276
}
275277

276278
func getCrds(p *parse.APIs) []string {
277-
result := []string{}
279+
crds := []extensionsv1beta1.CustomResourceDefinition{}
278280
for _, g := range p.APIs.Groups {
279281
for _, v := range g.Versions {
280282
for _, r := range v.Resources {
@@ -283,14 +285,34 @@ func getCrds(p *parse.APIs) []string {
283285
crd.Namespace = crdNamespace
284286
}
285287
crd.Labels = addLabels(map[string]string{})
286-
s, err := yaml.Marshal(crd)
287-
if err != nil {
288-
glog.Fatalf("Error: %v", err)
289-
}
290-
result = append(result, string(s))
288+
crds = append(crds, crd)
291289
}
292290
}
293291
}
292+
293+
sort.Slice(crds, func(i, j int) bool {
294+
iGroup := crds[i].Spec.Group
295+
jGroup := crds[j].Spec.Group
296+
297+
if iGroup != jGroup {
298+
return iGroup < jGroup
299+
}
300+
301+
iKind := crds[i].Spec.Names.Kind
302+
jKind := crds[j].Spec.Names.Kind
303+
304+
return iKind < jKind
305+
})
306+
307+
result := []string{}
308+
for i := range crds {
309+
s, err := yaml.Marshal(crds[i])
310+
if err != nil {
311+
glog.Fatalf("Error: %v", err)
312+
}
313+
result = append(result, string(s))
314+
}
315+
294316
return result
295317
}
296318

test.sh

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,121 @@ EOF
200200
diff crd.yaml expected.yaml
201201

202202
kubebuilder create resource --group insect --version v1beta1 --kind Wasp
203+
kubebuilder create resource --group ant --version v1beta1 --kind Ant
204+
kubebuilder create config --crds --output crd.yaml
205+
206+
# Check for ordering of generated YAML
207+
# TODO: make this a more concise test in a follow-up
208+
cat << EOF > expected.yaml
209+
apiVersion: apiextensions.k8s.io/v1beta1
210+
kind: CustomResourceDefinition
211+
metadata:
212+
creationTimestamp: null
213+
labels:
214+
api: ""
215+
kubebuilder.k8s.io: unknown
216+
name: ants.ant.sample.kubernetes.io
217+
spec:
218+
group: ant.sample.kubernetes.io
219+
names:
220+
kind: Ant
221+
plural: ants
222+
scope: Namespaced
223+
validation:
224+
openAPIV3Schema:
225+
properties:
226+
apiVersion:
227+
type: string
228+
kind:
229+
type: string
230+
metadata:
231+
type: object
232+
spec:
233+
type: object
234+
status:
235+
type: object
236+
type: object
237+
version: v1beta1
238+
status:
239+
acceptedNames:
240+
kind: ""
241+
plural: ""
242+
conditions: null
243+
---
244+
apiVersion: apiextensions.k8s.io/v1beta1
245+
kind: CustomResourceDefinition
246+
metadata:
247+
creationTimestamp: null
248+
labels:
249+
api: ""
250+
kubebuilder.k8s.io: unknown
251+
name: bees.insect.sample.kubernetes.io
252+
spec:
253+
group: insect.sample.kubernetes.io
254+
names:
255+
categories:
256+
- foo
257+
- bar
258+
kind: Bee
259+
plural: bees
260+
scope: Namespaced
261+
validation:
262+
openAPIV3Schema:
263+
properties:
264+
apiVersion:
265+
type: string
266+
kind:
267+
type: string
268+
metadata:
269+
type: object
270+
spec:
271+
type: object
272+
status:
273+
type: object
274+
type: object
275+
version: v1beta1
276+
status:
277+
acceptedNames:
278+
kind: ""
279+
plural: ""
280+
conditions: null
281+
---
282+
apiVersion: apiextensions.k8s.io/v1beta1
283+
kind: CustomResourceDefinition
284+
metadata:
285+
creationTimestamp: null
286+
labels:
287+
api: ""
288+
kubebuilder.k8s.io: unknown
289+
name: wasps.insect.sample.kubernetes.io
290+
spec:
291+
group: insect.sample.kubernetes.io
292+
names:
293+
kind: Wasp
294+
plural: wasps
295+
scope: Namespaced
296+
validation:
297+
openAPIV3Schema:
298+
properties:
299+
apiVersion:
300+
type: string
301+
kind:
302+
type: string
303+
metadata:
304+
type: object
305+
spec:
306+
type: object
307+
status:
308+
type: object
309+
type: object
310+
version: v1beta1
311+
status:
312+
acceptedNames:
313+
kind: ""
314+
plural: ""
315+
conditions: null
316+
EOF
317+
diff crd.yaml expected.yaml
203318
}
204319

205320
function test_generated_controller {

0 commit comments

Comments
 (0)