Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CustomResources: in OpenAPI spec allow additionalProperties without properties #62333

Merged

Conversation

sttts
Copy link
Contributor

@sttts sttts commented Apr 10, 2018

This implements @ayushpateria's idea #59485 (comment).

With this PR it becomes possible to specify map[string]Interface{} non-object types, e.g. map[string]string for selectors. On the other hand, "normal" objects use properties, mutually exclusively to additionalProperties. This way we avoid a conflict with Kubernetes API conventions that unknown objects fields are dropped.

Fixes #59485

Allow additionalProperties in CRD OpenAPI v3 specification for validation, mutually exclusive to properties.

@k8s-ci-robot k8s-ci-robot added do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Apr 10, 2018
@k8s-ci-robot k8s-ci-robot requested review from deads2k and enisoc April 10, 2018 11:08
@k8s-ci-robot k8s-ci-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Apr 10, 2018
@sttts
Copy link
Contributor Author

sttts commented Apr 10, 2018

@k8s-ci-robot
Copy link
Contributor

@sttts: GitHub didn't allow me to request PR reviews from the following users: ayushpateria.

Note that only kubernetes members and repo collaborators can review this PR, and authors cannot review their own PRs.

In response to this:

/cc @nikhita @ash2k @anguslees @ayushpateria @tamalsaha

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot requested a review from tamalsaha April 10, 2018 11:11
@sttts
Copy link
Contributor Author

sttts commented Apr 10, 2018

/assign @deads2k @liggitt @nikhita

@@ -45,6 +45,7 @@ func (s *JSONSchemaPropsOrBool) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &sch); err != nil {
return err
}
nw.Allows = true
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mistake saved us 🎉

Because for additionalProperties: {...} we forgot to set allows: true (and we reject Allows == false in general), it was not possible to define a CRD with anything other than additionalProperties: true.

@sttts sttts force-pushed the sttts-crd-validation-string-maps branch 2 times, most recently from ebe82de to a524273 Compare April 10, 2018 11:18
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Apr 10, 2018
@sttts sttts force-pushed the sttts-crd-validation-string-maps branch from a524273 to e2e9c23 Compare April 10, 2018 11:21
if schema.AdditionalProperties.Allows == false {
allErrs = append(allErrs, field.Forbidden(fldPath.Child("additionalProperties"), "additionalProperties cannot be set to false"))
if len(schema.Properties) != 0 {
if schema.AdditionalProperties.Allows == false || schema.AdditionalProperties.Schema != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this extra check necessary?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If allowed= true and schema = nil, that will cause panic. I can see that it is not possible to generate such object when unmarshaling.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the implicit default of additionalProperties is true. So schema.AdditionalProperties.Allows == true must be allowed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, there is not issue with panic when allowed= true and schema = nil.

@sttts sttts force-pushed the sttts-crd-validation-string-maps branch 2 times, most recently from 351a704 to 5cc2ce8 Compare April 10, 2018 11:37
@tamalsaha
Copy link
Member

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 10, 2018
…roperties

This allows to specify map[string]Interface{} non-object types, e.g. map[string]string
for selectors.
@sttts sttts force-pushed the sttts-crd-validation-string-maps branch from 5cc2ce8 to b9df44e Compare April 10, 2018 12:12
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 10, 2018
Copy link
Member

@ash2k ash2k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 10, 2018
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: ash2k, sttts, tamalsaha

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-github-robot
Copy link

Automatic merge from submit-queue (batch tested with PRs 59027, 62333, 57661, 62086, 61584). If you want to cherry-pick this change to another branch, please follow the instructions here.

@k8s-github-robot k8s-github-robot merged commit f3cad46 into kubernetes:master Apr 11, 2018
@tamalsaha
Copy link
Member

Thanks @sttts for fixing this. This pr fixes an important use-case for map type fields validation as I explained here.

Can this be cherry picked into 1.10 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants