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

✨ Generate schema for types with custom JSON marshaling as Any type #427

Merged
merged 1 commit into from
May 17, 2020

Conversation

skaslev
Copy link
Contributor

@skaslev skaslev commented Apr 1, 2020

Currently controller-gen complains about structs with missing json tags even
when those structs implement custom JSON marshalling.

With this change we check if a type implements custom JSON marshalling and if it
does, we output schema for Any type. This still allows the validation type to
overriden with a marker.

Fixes #391

@k8s-ci-robot k8s-ci-robot added the do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. label Apr 1, 2020
@k8s-ci-robot
Copy link
Contributor

Thanks for your pull request. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please follow instructions at https://git.k8s.io/community/CLA.md#the-contributor-license-agreement to sign the CLA.

It may take a couple minutes for the CLA signature to be fully registered; after that, please reply here with a new comment and we'll verify. Thanks.


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. I understand the commands that are listed here.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. label Apr 1, 2020
@k8s-ci-robot
Copy link
Contributor

Welcome @skaslev!

It looks like this is your first PR to kubernetes-sigs/controller-tools 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/controller-tools has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Apr 1, 2020
@skaslev skaslev changed the title 🐛 Generate schema for types implementing custom JSON marshaling as Any type 🐛 Generate schema for types implementing custom JSON marshaling as Any type Apr 1, 2020
@skaslev skaslev force-pushed the custom-json branch 2 times, most recently from 985db0a to c92ebff Compare April 1, 2020 07:50
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. label Apr 1, 2020
@skaslev skaslev changed the title 🐛 Generate schema for types implementing custom JSON marshaling as Any type 🐛 Generate schema for types with custom JSON marshaling as Any type Apr 1, 2020
@skaslev
Copy link
Contributor Author

skaslev commented Apr 1, 2020

/assign @DirectXMan12

@skaslev
Copy link
Contributor Author

skaslev commented Apr 1, 2020

/retest

@k8s-ci-robot
Copy link
Contributor

@skaslev: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/retest

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.

@skaslev
Copy link
Contributor Author

skaslev commented Apr 1, 2020

/check-cla

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Apr 1, 2020
Copy link
Contributor

@DirectXMan12 DirectXMan12 left a comment

Choose a reason for hiding this comment

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

can you add a test case for this (just add something to the big integration test)? Otherwise LGTM

@DirectXMan12
Copy link
Contributor

Thanks! We've wanted something like this for a while.

@yuzisun
Copy link

yuzisun commented Apr 26, 2020

@skaslev any chance to update the test and get this in? thank you!

@DirectXMan12
Copy link
Contributor

@yuzisun (or anyone else) -- if skaslev doesn't get back to us and you feel like writing the tests, feel free to add an additional commit and submit a new PR (and link to this one).

@k8s-ci-robot k8s-ci-robot removed the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label May 5, 2020
@skaslev skaslev deleted the custom-json branch May 19, 2020 11:59
christarazi added a commit to christarazi/controller-tools that referenced this pull request Jul 17, 2020
christarazi added a commit to christarazi/controller-tools that referenced this pull request Jul 17, 2020
christarazi added a commit to christarazi/controller-tools that referenced this pull request Jul 17, 2020
christarazi added a commit to christarazi/controller-tools that referenced this pull request Jul 17, 2020
christarazi added a commit to christarazi/controller-tools that referenced this pull request Jul 17, 2020
christarazi added a commit to christarazi/cilium that referenced this pull request Aug 6, 2020
To generate CRD validation schemas (also known as structual validation
schemas[1]), we bring in controller-tools. This houses a tool called
controller-gen which performs various K8s related code generation.

Note that we are using a fork of controller-tools due to the upstream
(as of v0.3.0) lacking support in two main areas.

1) A type which implements custom JSON marshalling has its validation
   schema generation skipped. In our case, api.Rule (which is set as the
   Spec field in CNP and CCNP) implements custom JSON marshalling. With
   the upstream tool, this skips the validation generation for it. In
   our fork, we revert this feature. See PR:
   kubernetes-sigs/controller-tools#427
2) For CNP and CCNP, we require `oneOf` validation for the
   endpointSelector and nodeSelector fields. This is because we want to
   validate that either endpointSelector OR nodeSelector is present, but
   not both, and at least one. In the upstream, there's currently an
   open PR that implements support for this. We have manually merged it
   in our fork. See PR:
   kubernetes-sigs/controller-tools#298

This commit also brings updates to our dependencies:

- gomega v1.7.0 -> v1.8.1
- golang.org/x/internal/{tools,event...}

[1]:
https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#specifying-a-structural-schema

Co-authored-by: Chris Tarazi <chris@isovalent.com>
Signed-off-by: André Martins <andre@cilium.io>
Signed-off-by: Chris Tarazi <chris@isovalent.com>
aanm pushed a commit to cilium/cilium that referenced this pull request Aug 6, 2020
To generate CRD validation schemas (also known as structual validation
schemas[1]), we bring in controller-tools. This houses a tool called
controller-gen which performs various K8s related code generation.

Note that we are using a fork of controller-tools due to the upstream
(as of v0.3.0) lacking support in two main areas.

1) A type which implements custom JSON marshalling has its validation
   schema generation skipped. In our case, api.Rule (which is set as the
   Spec field in CNP and CCNP) implements custom JSON marshalling. With
   the upstream tool, this skips the validation generation for it. In
   our fork, we revert this feature. See PR:
   kubernetes-sigs/controller-tools#427
2) For CNP and CCNP, we require `oneOf` validation for the
   endpointSelector and nodeSelector fields. This is because we want to
   validate that either endpointSelector OR nodeSelector is present, but
   not both, and at least one. In the upstream, there's currently an
   open PR that implements support for this. We have manually merged it
   in our fork. See PR:
   kubernetes-sigs/controller-tools#298

This commit also brings updates to our dependencies:

- gomega v1.7.0 -> v1.8.1
- golang.org/x/internal/{tools,event...}

[1]:
https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#specifying-a-structural-schema

Co-authored-by: Chris Tarazi <chris@isovalent.com>
Signed-off-by: André Martins <andre@cilium.io>
Signed-off-by: Chris Tarazi <chris@isovalent.com>
christarazi added a commit to christarazi/controller-tools that referenced this pull request Aug 14, 2020
christarazi added a commit to christarazi/controller-tools that referenced this pull request Sep 4, 2020
@vincepri
Copy link
Member

vincepri commented Oct 5, 2020

@DirectXMan12 @skaslev Using the tip of the main branch, I'm now getting an invalid CRD:

  CustomResourceDefinition.apiextensions.k8s.io "kubeadmcontrolplanes.controlplane.cluster.x-k8s.io" is invalid: spec.validation.openAPIV3Schema.properties[spec].properties[kubeadmConfigSpec].properties[initConfiguration].properties[bootstrapTokens].items.properties[token].type: Unsupported value: "Any": supported values: "array", "boolean", "integer", "number", "object", "string"

@vincepri
Copy link
Member

vincepri commented Oct 5, 2020

We should improve the error message saying that when using this capability, folks should manually specify the validation type

@alvaroaleman
Copy link
Member

alvaroaleman commented Oct 12, 2020

Any is just not valid. If folks have a custom marshaller, they must specify the json tag or use a kubebuilder annotation. This PR doesn't fix the issue it claims to fix while introducing a new issue: The schema we generate for types with a custom marshaller and without a kubebuilder annotation is now always invalid. Revert in #505

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. 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.

markers to skip fields that does not have json tags for crd generation
6 participants