Skip to content

Commit

Permalink
Merge pull request #123 from pmorie/create-config-crds-order
Browse files Browse the repository at this point in the history
Make order of generated yaml from kubebuilder create config --crds deterministic
  • Loading branch information
Phillip Wittrock authored May 9, 2018
2 parents 1a90786 + e490fba commit ac35586
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 6 deletions.
34 changes: 28 additions & 6 deletions cmd/kubebuilder/create/config/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package config

import (
"fmt"
"sort"
"strings"

"github.com/ghodss/yaml"
Expand All @@ -28,6 +29,7 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
extensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/gengo/args"
Expand Down Expand Up @@ -274,7 +276,7 @@ func getPodTemplate(labels map[string]string) corev1.PodTemplateSpec {
}

func getCrds(p *parse.APIs) []string {
result := []string{}
crds := []extensionsv1beta1.CustomResourceDefinition{}
for _, g := range p.APIs.Groups {
for _, v := range g.Versions {
for _, r := range v.Resources {
Expand All @@ -283,14 +285,34 @@ func getCrds(p *parse.APIs) []string {
crd.Namespace = crdNamespace
}
crd.Labels = addLabels(map[string]string{})
s, err := yaml.Marshal(crd)
if err != nil {
glog.Fatalf("Error: %v", err)
}
result = append(result, string(s))
crds = append(crds, crd)
}
}
}

sort.Slice(crds, func(i, j int) bool {
iGroup := crds[i].Spec.Group
jGroup := crds[j].Spec.Group

if iGroup != jGroup {
return iGroup < jGroup
}

iKind := crds[i].Spec.Names.Kind
jKind := crds[j].Spec.Names.Kind

return iKind < jKind
})

result := []string{}
for i := range crds {
s, err := yaml.Marshal(crds[i])
if err != nil {
glog.Fatalf("Error: %v", err)
}
result = append(result, string(s))
}

return result
}

Expand Down
115 changes: 115 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,121 @@ EOF
diff crd.yaml expected.yaml

kubebuilder create resource --group insect --version v1beta1 --kind Wasp
kubebuilder create resource --group ant --version v1beta1 --kind Ant
kubebuilder create config --crds --output crd.yaml

# Check for ordering of generated YAML
# TODO: make this a more concise test in a follow-up
cat << EOF > expected.yaml
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
creationTimestamp: null
labels:
api: ""
kubebuilder.k8s.io: unknown
name: ants.ant.sample.kubernetes.io
spec:
group: ant.sample.kubernetes.io
names:
kind: Ant
plural: ants
scope: Namespaced
validation:
openAPIV3Schema:
properties:
apiVersion:
type: string
kind:
type: string
metadata:
type: object
spec:
type: object
status:
type: object
type: object
version: v1beta1
status:
acceptedNames:
kind: ""
plural: ""
conditions: null
---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
creationTimestamp: null
labels:
api: ""
kubebuilder.k8s.io: unknown
name: bees.insect.sample.kubernetes.io
spec:
group: insect.sample.kubernetes.io
names:
categories:
- foo
- bar
kind: Bee
plural: bees
scope: Namespaced
validation:
openAPIV3Schema:
properties:
apiVersion:
type: string
kind:
type: string
metadata:
type: object
spec:
type: object
status:
type: object
type: object
version: v1beta1
status:
acceptedNames:
kind: ""
plural: ""
conditions: null
---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
creationTimestamp: null
labels:
api: ""
kubebuilder.k8s.io: unknown
name: wasps.insect.sample.kubernetes.io
spec:
group: insect.sample.kubernetes.io
names:
kind: Wasp
plural: wasps
scope: Namespaced
validation:
openAPIV3Schema:
properties:
apiVersion:
type: string
kind:
type: string
metadata:
type: object
spec:
type: object
status:
type: object
type: object
version: v1beta1
status:
acceptedNames:
kind: ""
plural: ""
conditions: null
EOF
diff crd.yaml expected.yaml
}

function test_generated_controller {
Expand Down

0 comments on commit ac35586

Please sign in to comment.