Skip to content

Commit 46695ab

Browse files
authored
Merge pull request #267 from haiyanmeng/versions
Sort the CRD Versions in the generated CRD and set `crd.Spec.Version` explicitly
2 parents 6f93faf + 8c5c3d4 commit 46695ab

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

pkg/crd/spec.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package crd
1717

1818
import (
1919
"fmt"
20+
"sort"
2021
"strings"
2122

2223
"github.com/gobuffalo/flect"
@@ -119,6 +120,11 @@ func (p *Parser) NeedCRDFor(groupKind schema.GroupKind) {
119120
return
120121
}
121122

123+
// it is necessary to make sure the order of CRD versions in crd.Spec.Versions is stable and explicitly set crd.Spec.Version.
124+
// Otherwise, crd.Spec.Version may point to different CRD versions across different runs.
125+
sort.Slice(crd.Spec.Versions, func(i, j int) bool { return crd.Spec.Versions[i].Name < crd.Spec.Versions[j].Name })
126+
crd.Spec.Version = crd.Spec.Versions[0].Name
127+
122128
// make sure we have *a* storage version
123129
// (default it if we only have one, otherwise, bail)
124130
if len(crd.Spec.Versions) == 1 {

pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ spec:
1111
kind: CronJob
1212
plural: cronjobs
1313
scope: Namespaced
14+
version: v1
1415
versions:
1516
- name: v1
1617
schema:

0 commit comments

Comments
 (0)