@@ -139,6 +139,7 @@ func (s *CRD) CustomRender() ([]byte, error) {
139
139
delete (dstCRD .Labels , "controller-tools.k8s.io" )
140
140
}
141
141
addCRDSubresource (dstCRD )
142
+ addCRDVersions (dstCRD )
142
143
return getCRDBytes (dstCRD )
143
144
}
144
145
@@ -181,6 +182,39 @@ func addCRDSubresource(crd *apiextv1beta1.CustomResourceDefinition) {
181
182
}
182
183
}
183
184
185
+ func addCRDVersions (crd * apiextv1beta1.CustomResourceDefinition ) {
186
+ // crd.Version is deprecated, use crd.Versions instead.
187
+ var crdVersions []apiextv1beta1.CustomResourceDefinitionVersion
188
+ if crd .Spec .Version != "" {
189
+ var verExists , hasStorageVer bool
190
+ for _ , ver := range crd .Spec .Versions {
191
+ if crd .Spec .Version == ver .Name {
192
+ verExists = true
193
+ }
194
+ // There must be exactly one version flagged as a storage version.
195
+ if ver .Storage {
196
+ hasStorageVer = true
197
+ }
198
+ }
199
+ if ! verExists {
200
+ crdVersions = []apiextv1beta1.CustomResourceDefinitionVersion {
201
+ {Name : crd .Spec .Version , Served : true , Storage : ! hasStorageVer },
202
+ }
203
+ }
204
+ } else {
205
+ crdVersions = []apiextv1beta1.CustomResourceDefinitionVersion {
206
+ {Name : "v1alpha1" , Served : true , Storage : true },
207
+ }
208
+ }
209
+
210
+ if len (crd .Spec .Versions ) > 0 {
211
+ // crd.Version should always be the first element in crd.Versions.
212
+ crd .Spec .Versions = append (crdVersions , crd .Spec .Versions ... )
213
+ } else {
214
+ crd .Spec .Versions = crdVersions
215
+ }
216
+ }
217
+
184
218
func getCRDBytes (crd * apiextv1beta1.CustomResourceDefinition ) ([]byte , error ) {
185
219
// Remove the "status" field from yaml data, which causes a
186
220
// resource creation error.
0 commit comments