Skip to content

Commit 1897419

Browse files
committed
fix: Do not hoist identical version schemas for apiextensions.k8s.io/v1
The underlying k8s api that converts into the final CRD object is deduplicating versions. With rehydrating the schemas we fix a problem with possible API incompatibility and the generated specs.
1 parent a7b2d06 commit 1897419

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

pkg/kube_resource/generator/generator.go

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -241,35 +241,41 @@ func CRDContainsValidation(crd *apiextensions.CustomResourceDefinition) bool {
241241
func buildSwagger(crd *apiextensions.CustomResourceDefinition) (*spec.Swagger, error) {
242242
var schemas spec.Definitions = map[string]spec.Schema{}
243243
group, kind := crd.Spec.Group, crd.Spec.Names.Kind
244-
if crd.Spec.Validation != nil && crd.Spec.Validation.OpenAPIV3Schema != nil {
244+
245+
// The upstream v1→internal conversion hoists identical per-version schemas into Spec.Validation
246+
// and clears them on each version. Rehydrate them here so we still emit every version.
247+
if crd.Spec.Validation != nil {
248+
for i := range crd.Spec.Versions {
249+
if crd.Spec.Versions[i].Schema == nil {
250+
crd.Spec.Versions[i].Schema = crd.Spec.Validation
251+
}
252+
}
253+
}
254+
255+
if len(crd.Spec.Versions) > 0 {
256+
for _, version := range crd.Spec.Versions {
257+
if version.Schema == nil || version.Schema.OpenAPIV3Schema == nil {
258+
continue
259+
}
260+
var schema spec.Schema
261+
err := validation.ConvertJSONSchemaProps(version.Schema.OpenAPIV3Schema, &schema)
262+
if err != nil {
263+
return nil, err
264+
}
265+
setKubeNative(&schema, group, version.Name, kind)
266+
name := fmt.Sprintf("%s.%s.%s", group, version.Name, kind)
267+
schemas[name] = schema
268+
}
269+
} else if crd.Spec.Validation != nil && crd.Spec.Validation.OpenAPIV3Schema != nil {
245270
var schema spec.Schema
246271
err := validation.ConvertJSONSchemaProps(crd.Spec.Validation.OpenAPIV3Schema, &schema)
247272
if err != nil {
248273
return nil, err
249274
}
250-
var version string
251-
if len(crd.Spec.Versions) >= 0 {
252-
version = crd.Spec.Versions[0].Name
253-
} else {
254-
version = crd.Spec.Version
255-
}
275+
version := crd.Spec.Version
256276
setKubeNative(&schema, group, version, kind)
257277
name := fmt.Sprintf("%s.%s.%s", group, version, kind)
258278
schemas[name] = schema
259-
} else if len(crd.Spec.Versions) > 0 {
260-
for _, version := range crd.Spec.Versions {
261-
if version.Schema != nil && version.Schema.OpenAPIV3Schema != nil {
262-
var schema spec.Schema
263-
err := validation.ConvertJSONSchemaProps(version.Schema.OpenAPIV3Schema, &schema)
264-
if err != nil {
265-
return nil, err
266-
}
267-
version := version.Name
268-
setKubeNative(&schema, group, version, kind)
269-
name := fmt.Sprintf("%s.%s.%s", group, version, kind)
270-
schemas[name] = schema
271-
}
272-
}
273279
}
274280

275281
// todo: set extensions, include kcl-type and user-defined extensions

0 commit comments

Comments
 (0)