Skip to content

Commit ac35586

Browse files
author
Phillip Wittrock
authored
Merge pull request #123 from pmorie/create-config-crds-order
Make order of generated yaml from kubebuilder create config --crds deterministic
2 parents 1a90786 + e490fba commit ac35586

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
@@ -212,6 +212,121 @@ EOF
212212
diff crd.yaml expected.yaml
213213

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

217332
function test_generated_controller {

0 commit comments

Comments
 (0)