Skip to content

Commit

Permalink
Merge pull request #368 from droot/bugfix/drop-type-crd-validation-sc…
Browse files Browse the repository at this point in the history
…hema

drop type field from the CRD schema validation at the root level
  • Loading branch information
k8s-ci-robot committed Aug 31, 2018
2 parents 9a2e569 + a21cc1e commit 849072a
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 22 deletions.
33 changes: 21 additions & 12 deletions cmd/internal/codegen/parse/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ func (b *APIs) parseCRDs() {
for _, resource := range version.Resources {
if IsAPIResource(resource.Type) {
resource.JSONSchemaProps, resource.Validation =
b.typeToJSONSchemaProps(resource.Type, sets.NewString(), []string{})

b.typeToJSONSchemaProps(resource.Type, sets.NewString(), []string{}, true)
// k8s 1.11 rejects CRDs with type property at the root if status sub-resources enabled.
// This change drops the type field for CRD at the root level. Refer to issue below:
// https://github.com/kubernetes/kubernetes/issues/65293
resource.JSONSchemaProps.Type = ""
j, err := json.MarshalIndent(resource.JSONSchemaProps, "", " ")
if err != nil {
log.Fatalf("Could not Marshall validation %v\n", err)
Expand Down Expand Up @@ -114,7 +117,7 @@ func (b *APIs) getMeta() string {

// typeToJSONSchemaProps returns a JSONSchemaProps object and its serialization
// in Go that describe the JSONSchema validations for the given type.
func (b *APIs) typeToJSONSchemaProps(t *types.Type, found sets.String, comments []string) (v1beta1.JSONSchemaProps, string) {
func (b *APIs) typeToJSONSchemaProps(t *types.Type, found sets.String, comments []string, isRoot bool) (v1beta1.JSONSchemaProps, string) {
// Special cases
time := types.Name{Name: "Time", Package: "k8s.io/apimachinery/pkg/apis/meta/v1"}
meta := types.Name{Name: "ObjectMeta", Package: "k8s.io/apimachinery/pkg/apis/meta/v1"}
Expand All @@ -136,17 +139,17 @@ func (b *APIs) typeToJSONSchemaProps(t *types.Type, found sets.String, comments
case types.Builtin:
v, s = b.parsePrimitiveValidation(t, found, comments)
case types.Struct:
v, s = b.parseObjectValidation(t, found, comments)
v, s = b.parseObjectValidation(t, found, comments, isRoot)
case types.Map:
v, s = b.parseMapValidation(t, found, comments)
case types.Slice:
v, s = b.parseArrayValidation(t, found, comments)
case types.Array:
v, s = b.parseArrayValidation(t, found, comments)
case types.Pointer:
v, s = b.typeToJSONSchemaProps(t.Elem, found, comments)
v, s = b.typeToJSONSchemaProps(t.Elem, found, comments, false)
case types.Alias:
v, s = b.typeToJSONSchemaProps(t.Underlying, found, comments)
v, s = b.typeToJSONSchemaProps(t.Underlying, found, comments, false)
default:
log.Fatalf("Unknown supported Kind %v\n", t.Kind)
}
Expand Down Expand Up @@ -256,7 +259,7 @@ var mapTemplate = template.Must(template.New("map-template").Parse(
// parseMapValidation returns a JSONSchemaProps object and its serialization in
// Go that describe the validations for the given map type.
func (b *APIs) parseMapValidation(t *types.Type, found sets.String, comments []string) (v1beta1.JSONSchemaProps, string) {
additionalProps, result := b.typeToJSONSchemaProps(t.Elem, found, comments)
additionalProps, result := b.typeToJSONSchemaProps(t.Elem, found, comments, false)
props := v1beta1.JSONSchemaProps{
Type: "object",
}
Expand Down Expand Up @@ -304,7 +307,7 @@ type arrayTemplateArgs struct {
// parseArrayValidation returns a JSONSchemaProps object and its serialization in
// Go that describe the validations for the given array type.
func (b *APIs) parseArrayValidation(t *types.Type, found sets.String, comments []string) (v1beta1.JSONSchemaProps, string) {
items, result := b.typeToJSONSchemaProps(t.Elem, found, comments)
items, result := b.typeToJSONSchemaProps(t.Elem, found, comments, false)
props := v1beta1.JSONSchemaProps{
Type: "array",
Items: &v1beta1.JSONSchemaPropsOrArray{Schema: &items},
Expand All @@ -330,11 +333,17 @@ type objectTemplateArgs struct {
v1beta1.JSONSchemaProps
Fields map[string]string
Required []string
IsRoot bool
}

// Drop the "type" field from the CRD validation schema at the root level.
// K8s 1.11 server rejects such CRDs. Refer to issue below for more details:
// https://github.com/kubernetes/kubernetes/issues/65293
var objectTemplate = template.Must(template.New("object-template").Parse(
`v1beta1.JSONSchemaProps{
{{ if not .IsRoot -}}
Type: "object",
{{ end -}}
Properties: map[string]v1beta1.JSONSchemaProps{
{{ range $k, $v := .Fields -}}
"{{ $k }}": {{ $v }},
Expand All @@ -349,14 +358,14 @@ var objectTemplate = template.Must(template.New("object-template").Parse(

// parseObjectValidation returns a JSONSchemaProps object and its serialization in
// Go that describe the validations for the given object type.
func (b *APIs) parseObjectValidation(t *types.Type, found sets.String, comments []string) (v1beta1.JSONSchemaProps, string) {
func (b *APIs) parseObjectValidation(t *types.Type, found sets.String, comments []string, isRoot bool) (v1beta1.JSONSchemaProps, string) {
buff := &bytes.Buffer{}
props := v1beta1.JSONSchemaProps{
Type: "object",
}

if strings.HasPrefix(t.Name.String(), "k8s.io/api") {
if err := objectTemplate.Execute(buff, objectTemplateArgs{props, nil, nil}); err != nil {
if err := objectTemplate.Execute(buff, objectTemplateArgs{props, nil, nil, false}); err != nil {
log.Fatalf("%v", err)
}
} else {
Expand All @@ -369,7 +378,7 @@ func (b *APIs) parseObjectValidation(t *types.Type, found sets.String, comments
getValidation(l, &props)
}

if err := objectTemplate.Execute(buff, objectTemplateArgs{props, result, required}); err != nil {
if err := objectTemplate.Execute(buff, objectTemplateArgs{props, result, required, isRoot}); err != nil {
log.Fatalf("%v", err)
}
}
Expand Down Expand Up @@ -533,7 +542,7 @@ func (b *APIs) getMembers(t *types.Type, found sets.String) (map[string]v1beta1.
}
required = append(required, re...)
} else {
m, r := b.typeToJSONSchemaProps(member.Type, found, member.CommentLines)
m, r := b.typeToJSONSchemaProps(member.Type, found, member.CommentLines, false)
members[name] = m
result[name] = r
if !strings.HasSuffix(strat, "omitempty") {
Expand Down
3 changes: 2 additions & 1 deletion common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ cd "$base_dir" || {
}

k8s_version=1.10.1
#k8s_version=1.11.0
goarch=amd64
goos="unknown"

Expand Down Expand Up @@ -163,4 +164,4 @@ function dump_cache {
if [ -d "$TEST_DEP" ]; then
cp -r $TEST_DEP/* .
fi
}
}
1 change: 0 additions & 1 deletion test/data/resource/expected/crd-expected.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ spec:
type: object
status:
type: object
type: object
version: v1beta1
status:
acceptedNames:
Expand Down
1 change: 0 additions & 1 deletion test/projects/memcached-api-server/test/hack/install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ spec:
type: string
type: array
type: object
type: object
version: v1alpha1
status:
acceptedNames:
Expand Down
1 change: 0 additions & 1 deletion test/projects/validations/test/hack/install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ spec:
type: object
status:
type: object
type: object
version: v1
status:
acceptedNames:
Expand Down
2 changes: 1 addition & 1 deletion test_existing_projects.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ set -o pipefail
for p in ./test/projects/*
do
if [[ -d "$p" ]]; then
go test -v "$p"
go test -v -count=1 "$p"
fi
done
5 changes: 0 additions & 5 deletions testv0.sh
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ spec:
type: object
status:
type: object
type: object
version: v1beta1
status:
acceptedNames:
Expand Down Expand Up @@ -184,7 +183,6 @@ spec:
type: object
status:
type: object
type: object
version: v1beta1
status:
acceptedNames:
Expand Down Expand Up @@ -293,7 +291,6 @@ spec:
type: object
status:
type: object
type: object
version: v1beta1
status:
acceptedNames:
Expand Down Expand Up @@ -336,7 +333,6 @@ spec:
type: object
status:
type: object
type: object
version: v1beta1
status:
acceptedNames:
Expand Down Expand Up @@ -371,7 +367,6 @@ spec:
type: object
status:
type: object
type: object
version: v1beta1
status:
acceptedNames:
Expand Down

0 comments on commit 849072a

Please sign in to comment.