@@ -41,8 +41,11 @@ func (b *APIs) parseCRDs() {
41
41
for _ , resource := range version .Resources {
42
42
if IsAPIResource (resource .Type ) {
43
43
resource .JSONSchemaProps , resource .Validation =
44
- b .typeToJSONSchemaProps (resource .Type , sets .NewString (), []string {})
45
-
44
+ b .typeToJSONSchemaProps (resource .Type , sets .NewString (), []string {}, true )
45
+ // k8s 1.11 rejects CRDs with type property at the root if status sub-resources enabled.
46
+ // This change drops the type field for CRD at the root level. Refer to issue below:
47
+ // https://github.com/kubernetes/kubernetes/issues/65293
48
+ resource .JSONSchemaProps .Type = ""
46
49
j , err := json .MarshalIndent (resource .JSONSchemaProps , "" , " " )
47
50
if err != nil {
48
51
log .Fatalf ("Could not Marshall validation %v\n " , err )
@@ -114,7 +117,7 @@ func (b *APIs) getMeta() string {
114
117
115
118
// typeToJSONSchemaProps returns a JSONSchemaProps object and its serialization
116
119
// in Go that describe the JSONSchema validations for the given type.
117
- func (b * APIs ) typeToJSONSchemaProps (t * types.Type , found sets.String , comments []string ) (v1beta1.JSONSchemaProps , string ) {
120
+ func (b * APIs ) typeToJSONSchemaProps (t * types.Type , found sets.String , comments []string , isRoot bool ) (v1beta1.JSONSchemaProps , string ) {
118
121
// Special cases
119
122
time := types.Name {Name : "Time" , Package : "k8s.io/apimachinery/pkg/apis/meta/v1" }
120
123
meta := types.Name {Name : "ObjectMeta" , Package : "k8s.io/apimachinery/pkg/apis/meta/v1" }
@@ -136,17 +139,17 @@ func (b *APIs) typeToJSONSchemaProps(t *types.Type, found sets.String, comments
136
139
case types .Builtin :
137
140
v , s = b .parsePrimitiveValidation (t , found , comments )
138
141
case types .Struct :
139
- v , s = b .parseObjectValidation (t , found , comments )
142
+ v , s = b .parseObjectValidation (t , found , comments , isRoot )
140
143
case types .Map :
141
144
v , s = b .parseMapValidation (t , found , comments )
142
145
case types .Slice :
143
146
v , s = b .parseArrayValidation (t , found , comments )
144
147
case types .Array :
145
148
v , s = b .parseArrayValidation (t , found , comments )
146
149
case types .Pointer :
147
- v , s = b .typeToJSONSchemaProps (t .Elem , found , comments )
150
+ v , s = b .typeToJSONSchemaProps (t .Elem , found , comments , false )
148
151
case types .Alias :
149
- v , s = b .typeToJSONSchemaProps (t .Underlying , found , comments )
152
+ v , s = b .typeToJSONSchemaProps (t .Underlying , found , comments , false )
150
153
default :
151
154
log .Fatalf ("Unknown supported Kind %v\n " , t .Kind )
152
155
}
@@ -256,7 +259,7 @@ var mapTemplate = template.Must(template.New("map-template").Parse(
256
259
// parseMapValidation returns a JSONSchemaProps object and its serialization in
257
260
// Go that describe the validations for the given map type.
258
261
func (b * APIs ) parseMapValidation (t * types.Type , found sets.String , comments []string ) (v1beta1.JSONSchemaProps , string ) {
259
- additionalProps , result := b .typeToJSONSchemaProps (t .Elem , found , comments )
262
+ additionalProps , result := b .typeToJSONSchemaProps (t .Elem , found , comments , false )
260
263
props := v1beta1.JSONSchemaProps {
261
264
Type : "object" ,
262
265
}
@@ -304,7 +307,7 @@ type arrayTemplateArgs struct {
304
307
// parseArrayValidation returns a JSONSchemaProps object and its serialization in
305
308
// Go that describe the validations for the given array type.
306
309
func (b * APIs ) parseArrayValidation (t * types.Type , found sets.String , comments []string ) (v1beta1.JSONSchemaProps , string ) {
307
- items , result := b .typeToJSONSchemaProps (t .Elem , found , comments )
310
+ items , result := b .typeToJSONSchemaProps (t .Elem , found , comments , false )
308
311
props := v1beta1.JSONSchemaProps {
309
312
Type : "array" ,
310
313
Items : & v1beta1.JSONSchemaPropsOrArray {Schema : & items },
@@ -330,11 +333,17 @@ type objectTemplateArgs struct {
330
333
v1beta1.JSONSchemaProps
331
334
Fields map [string ]string
332
335
Required []string
336
+ IsRoot bool
333
337
}
334
338
339
+ // Drop the "type" field from the CRD validation schema at the root level.
340
+ // K8s 1.11 server rejects such CRDs. Refer to issue below for more details:
341
+ // https://github.com/kubernetes/kubernetes/issues/65293
335
342
var objectTemplate = template .Must (template .New ("object-template" ).Parse (
336
343
`v1beta1.JSONSchemaProps{
344
+ {{ if not .IsRoot -}}
337
345
Type: "object",
346
+ {{ end -}}
338
347
Properties: map[string]v1beta1.JSONSchemaProps{
339
348
{{ range $k, $v := .Fields -}}
340
349
"{{ $k }}": {{ $v }},
@@ -349,14 +358,14 @@ var objectTemplate = template.Must(template.New("object-template").Parse(
349
358
350
359
// parseObjectValidation returns a JSONSchemaProps object and its serialization in
351
360
// Go that describe the validations for the given object type.
352
- func (b * APIs ) parseObjectValidation (t * types.Type , found sets.String , comments []string ) (v1beta1.JSONSchemaProps , string ) {
361
+ func (b * APIs ) parseObjectValidation (t * types.Type , found sets.String , comments []string , isRoot bool ) (v1beta1.JSONSchemaProps , string ) {
353
362
buff := & bytes.Buffer {}
354
363
props := v1beta1.JSONSchemaProps {
355
364
Type : "object" ,
356
365
}
357
366
358
367
if strings .HasPrefix (t .Name .String (), "k8s.io/api" ) {
359
- if err := objectTemplate .Execute (buff , objectTemplateArgs {props , nil , nil }); err != nil {
368
+ if err := objectTemplate .Execute (buff , objectTemplateArgs {props , nil , nil , false }); err != nil {
360
369
log .Fatalf ("%v" , err )
361
370
}
362
371
} else {
@@ -369,7 +378,7 @@ func (b *APIs) parseObjectValidation(t *types.Type, found sets.String, comments
369
378
getValidation (l , & props )
370
379
}
371
380
372
- if err := objectTemplate .Execute (buff , objectTemplateArgs {props , result , required }); err != nil {
381
+ if err := objectTemplate .Execute (buff , objectTemplateArgs {props , result , required , isRoot }); err != nil {
373
382
log .Fatalf ("%v" , err )
374
383
}
375
384
}
@@ -533,7 +542,7 @@ func (b *APIs) getMembers(t *types.Type, found sets.String) (map[string]v1beta1.
533
542
}
534
543
required = append (required , re ... )
535
544
} else {
536
- m , r := b .typeToJSONSchemaProps (member .Type , found , member .CommentLines )
545
+ m , r := b .typeToJSONSchemaProps (member .Type , found , member .CommentLines , false )
537
546
members [name ] = m
538
547
result [name ] = r
539
548
if ! strings .HasSuffix (strat , "omitempty" ) {
0 commit comments