Skip to content

Commit 849072a

Browse files
authored
Merge pull request #368 from droot/bugfix/drop-type-crd-validation-schema
drop type field from the CRD schema validation at the root level
2 parents 9a2e569 + a21cc1e commit 849072a

File tree

7 files changed

+24
-22
lines changed

7 files changed

+24
-22
lines changed

cmd/internal/codegen/parse/crd.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ func (b *APIs) parseCRDs() {
4141
for _, resource := range version.Resources {
4242
if IsAPIResource(resource.Type) {
4343
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 = ""
4649
j, err := json.MarshalIndent(resource.JSONSchemaProps, "", " ")
4750
if err != nil {
4851
log.Fatalf("Could not Marshall validation %v\n", err)
@@ -114,7 +117,7 @@ func (b *APIs) getMeta() string {
114117

115118
// typeToJSONSchemaProps returns a JSONSchemaProps object and its serialization
116119
// 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) {
118121
// Special cases
119122
time := types.Name{Name: "Time", Package: "k8s.io/apimachinery/pkg/apis/meta/v1"}
120123
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
136139
case types.Builtin:
137140
v, s = b.parsePrimitiveValidation(t, found, comments)
138141
case types.Struct:
139-
v, s = b.parseObjectValidation(t, found, comments)
142+
v, s = b.parseObjectValidation(t, found, comments, isRoot)
140143
case types.Map:
141144
v, s = b.parseMapValidation(t, found, comments)
142145
case types.Slice:
143146
v, s = b.parseArrayValidation(t, found, comments)
144147
case types.Array:
145148
v, s = b.parseArrayValidation(t, found, comments)
146149
case types.Pointer:
147-
v, s = b.typeToJSONSchemaProps(t.Elem, found, comments)
150+
v, s = b.typeToJSONSchemaProps(t.Elem, found, comments, false)
148151
case types.Alias:
149-
v, s = b.typeToJSONSchemaProps(t.Underlying, found, comments)
152+
v, s = b.typeToJSONSchemaProps(t.Underlying, found, comments, false)
150153
default:
151154
log.Fatalf("Unknown supported Kind %v\n", t.Kind)
152155
}
@@ -256,7 +259,7 @@ var mapTemplate = template.Must(template.New("map-template").Parse(
256259
// parseMapValidation returns a JSONSchemaProps object and its serialization in
257260
// Go that describe the validations for the given map type.
258261
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)
260263
props := v1beta1.JSONSchemaProps{
261264
Type: "object",
262265
}
@@ -304,7 +307,7 @@ type arrayTemplateArgs struct {
304307
// parseArrayValidation returns a JSONSchemaProps object and its serialization in
305308
// Go that describe the validations for the given array type.
306309
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)
308311
props := v1beta1.JSONSchemaProps{
309312
Type: "array",
310313
Items: &v1beta1.JSONSchemaPropsOrArray{Schema: &items},
@@ -330,11 +333,17 @@ type objectTemplateArgs struct {
330333
v1beta1.JSONSchemaProps
331334
Fields map[string]string
332335
Required []string
336+
IsRoot bool
333337
}
334338

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
335342
var objectTemplate = template.Must(template.New("object-template").Parse(
336343
`v1beta1.JSONSchemaProps{
344+
{{ if not .IsRoot -}}
337345
Type: "object",
346+
{{ end -}}
338347
Properties: map[string]v1beta1.JSONSchemaProps{
339348
{{ range $k, $v := .Fields -}}
340349
"{{ $k }}": {{ $v }},
@@ -349,14 +358,14 @@ var objectTemplate = template.Must(template.New("object-template").Parse(
349358

350359
// parseObjectValidation returns a JSONSchemaProps object and its serialization in
351360
// 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) {
353362
buff := &bytes.Buffer{}
354363
props := v1beta1.JSONSchemaProps{
355364
Type: "object",
356365
}
357366

358367
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 {
360369
log.Fatalf("%v", err)
361370
}
362371
} else {
@@ -369,7 +378,7 @@ func (b *APIs) parseObjectValidation(t *types.Type, found sets.String, comments
369378
getValidation(l, &props)
370379
}
371380

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 {
373382
log.Fatalf("%v", err)
374383
}
375384
}
@@ -533,7 +542,7 @@ func (b *APIs) getMembers(t *types.Type, found sets.String) (map[string]v1beta1.
533542
}
534543
required = append(required, re...)
535544
} else {
536-
m, r := b.typeToJSONSchemaProps(member.Type, found, member.CommentLines)
545+
m, r := b.typeToJSONSchemaProps(member.Type, found, member.CommentLines, false)
537546
members[name] = m
538547
result[name] = r
539548
if !strings.HasSuffix(strat, "omitempty") {

common.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ cd "$base_dir" || {
4242
}
4343

4444
k8s_version=1.10.1
45+
#k8s_version=1.11.0
4546
goarch=amd64
4647
goos="unknown"
4748

@@ -163,4 +164,4 @@ function dump_cache {
163164
if [ -d "$TEST_DEP" ]; then
164165
cp -r $TEST_DEP/* .
165166
fi
166-
}
167+
}

test/data/resource/expected/crd-expected.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ spec:
6464
type: object
6565
status:
6666
type: object
67-
type: object
6867
version: v1beta1
6968
status:
7069
acceptedNames:

test/projects/memcached-api-server/test/hack/install.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ spec:
3838
type: string
3939
type: array
4040
type: object
41-
type: object
4241
version: v1alpha1
4342
status:
4443
acceptedNames:

test/projects/validations/test/hack/install.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ spec:
6969
type: object
7070
status:
7171
type: object
72-
type: object
7372
version: v1
7473
status:
7574
acceptedNames:

test_existing_projects.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ set -o pipefail
2020
for p in ./test/projects/*
2121
do
2222
if [[ -d "$p" ]]; then
23-
go test -v "$p"
23+
go test -v -count=1 "$p"
2424
fi
2525
done

testv0.sh

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ spec:
8686
type: object
8787
status:
8888
type: object
89-
type: object
9089
version: v1beta1
9190
status:
9291
acceptedNames:
@@ -184,7 +183,6 @@ spec:
184183
type: object
185184
status:
186185
type: object
187-
type: object
188186
version: v1beta1
189187
status:
190188
acceptedNames:
@@ -293,7 +291,6 @@ spec:
293291
type: object
294292
status:
295293
type: object
296-
type: object
297294
version: v1beta1
298295
status:
299296
acceptedNames:
@@ -336,7 +333,6 @@ spec:
336333
type: object
337334
status:
338335
type: object
339-
type: object
340336
version: v1beta1
341337
status:
342338
acceptedNames:
@@ -371,7 +367,6 @@ spec:
371367
type: object
372368
status:
373369
type: object
374-
type: object
375370
version: v1beta1
376371
status:
377372
acceptedNames:

0 commit comments

Comments
 (0)