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

Feature: Added support for OpenAPI validations marker comments #435

Merged
merged 1 commit into from
Jan 3, 2024

Conversation

Schnides123
Copy link
Contributor

@Schnides123 Schnides123 commented Oct 16, 2023

This PR adds the ability to annotate API fields with basic OpenAPI schema validations via marker comments, as part of the KEP-4153: Declarative Validation feature.

/assign @alexzielenski

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Oct 16, 2023

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: Schnides123 / name: Alex Schneider (a06b517)

@k8s-ci-robot
Copy link
Contributor

Welcome @Schnides123!

It looks like this is your first PR to kubernetes/kube-openapi 🎉. 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/kube-openapi 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 cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. 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 Oct 16, 2023
@alexzielenski
Copy link
Contributor

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Oct 16, 2023
Copy link
Contributor

@alexzielenski alexzielenski left a comment

Choose a reason for hiding this comment

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

Really like the approach. needs a lot more testing, though.

When we reach a more mature PR state we should open a k/k PR with these changes to show the diff with the generated OpenAPI

We can merge this next release after code thaw

pkg/generators/markers_test.go Outdated Show resolved Hide resolved
pkg/generators/markers_test.go Show resolved Hide resolved
pkg/generators/openapi.go Outdated Show resolved Hide resolved
pkg/generators/openapi.go Outdated Show resolved Hide resolved
pkg/generators/markers.go Show resolved Hide resolved
pkg/generators/openapi.go Outdated Show resolved Hide resolved
pkg/generators/openapi.go Outdated Show resolved Hide resolved
@k8s-ci-robot k8s-ci-robot requested a review from thockin October 16, 2023 20:28
@k8s-ci-robot
Copy link
Contributor

@alexzielenski: GitHub didn't allow me to request PR reviews from the following users: WDYT, is, preferable.

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

In response to this:

An alternative to using these helper functions is to do as you did in the test by abusing the array literal notation:

&[]float64{20.0}[0]

/cc @apelisse WDYT is preferable

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.

pkg/generators/markers.go Outdated Show resolved Hide resolved
pkg/generators/markers.go Outdated Show resolved Hide resolved
@k8s-ci-robot k8s-ci-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Oct 18, 2023
@apelisse
Copy link
Member

I would still add an intergration test. Create a new directory for value validation in https://github.com/kubernetes/kube-openapi/tree/master/test/integration/testdata, add some types in there with various validation markers, carefully follow the README including the notes at the bottom. Thanks!

@k8s-ci-robot k8s-ci-robot added needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. and removed needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Oct 18, 2023
@Schnides123
Copy link
Contributor Author

I have been told to add the prefix back in, so the prefix is now back in

pkg/generators/markers.go Outdated Show resolved Hide resolved
pkg/generators/markers.go Outdated Show resolved Hide resolved
pkg/generators/markers_test.go Outdated Show resolved Hide resolved
pkg/generators/openapi.go Outdated Show resolved Hide resolved
@k8s-ci-robot k8s-ci-robot removed the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label Dec 8, 2023
@k8s-ci-robot k8s-ci-robot added the size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. label Dec 8, 2023
Copy link
Contributor

@alexzielenski alexzielenski left a comment

Choose a reason for hiding this comment

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

Looking really good. Few more tests before I think it is ready to go

pkg/generators/markers.go Outdated Show resolved Hide resolved
pkg/generators/openapi.go Outdated Show resolved Hide resolved
"Format:$.type|raw${}.OpenAPISchemaFormat(),\n"+
"},\n"+
"Format:$.type|raw${}.OpenAPISchemaFormat(),\n", args)
g.generateValueValidations(&overrides.SchemaProps)
Copy link
Contributor

@alexzielenski alexzielenski Dec 9, 2023

Choose a reason for hiding this comment

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

I see the following new calls to generateValueValidations

  • Custom V2 Type/Format and V3 Definition Functions
  • Custom V2 Type/Format and V3 oneOf types
  • Custom V2 Type/Format (and No V3 Definition or OneOf)
  • V3 OneOf Types
  • Regular Behavior (no custom types or override functions implemented)

The checked boxes are those that an integration exists already. This is function is pretty complicated. I wonder if we can simplify this function in a future PR. In the meantime, I'd like to see the rest of the cases covered in the integration tests to make sure the output compiles in those cases.

Copy link
Contributor

Choose a reason for hiding this comment

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

resolved

newKey := strings.TrimPrefix(key, prefix)

// Skip ref markers
if len(value) == 1 && regexp.MustCompile(`^(?:\s*ref\s{0,1}\().*\)\s*`).MatchString(value[0]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this possible try "k8s.io/gengo/examples/defaulter-gen/generators".ParseSymbolReference against the value to see if it is a ref instead? To avoid duplciating this logic

Copy link
Contributor

Choose a reason for hiding this comment

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

resolved

pkg/generators/markers.go Outdated Show resolved Hide resolved
pkg/generators/markers.go Show resolved Hide resolved
@@ -59,6 +139,11 @@ func ParseCommentTags(comments []string, prefix string) (CommentTags, error) {
return CommentTags{}, fmt.Errorf("failed to unmarshal marker comments: %w", err)
}

// Validate the parsed comment tags
if err := commentTags.Validate(*t); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think id prefer to not validate within the parse function. Id expect the caller to call validate on the return value rather than to make this function take a type

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are you sure? I feel like there really isn't a use-case for generating comment tags that wouldn't pass validation. Splitting it out might be convenient for plumbing the type out of the ParseCommentTags arguments, but I don't foresee us having any situations where we'd want to create a commentTags instance and then not validate it immediately after.

pkg/generators/markers_test.go Show resolved Hide resolved
pkg/generators/markers_test.go Outdated Show resolved Hide resolved
pkg/generators/markers.go Outdated Show resolved Hide resolved
pkg/generators/markers.go Outdated Show resolved Hide resolved
pkg/generators/markers.go Outdated Show resolved Hide resolved
@alexzielenski
Copy link
Contributor

alexzielenski commented Jan 3, 2024

/label tide/merge-method-squash
/lgtm

@k8s-ci-robot k8s-ci-robot added the tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges. label Jan 3, 2024
Copy link
Contributor

@alexzielenski alexzielenski left a comment

Choose a reason for hiding this comment

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

/approve

Looks really good! XXL PR but is primarily test code. Great starting point for the first alpha. Thanks for the extensive testing!

I pinned k/k against this branch and tested there are no dep changes (other than kube-openapi) and that running the code generator (without using the markers) does not result in changes

@k8s-ci-robot k8s-ci-robot added lgtm "Looks good to me", indicates that a PR is ready to be merged. approved Indicates a PR has been approved by an approver from all required OWNERS files. labels Jan 3, 2024
@alexzielenski
Copy link
Contributor

alexzielenski commented Jan 3, 2024

/hold

can you squash (tide concats the descriptions which is not useful)

edit: i squashed for you

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jan 3, 2024
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jan 3, 2024
@alexzielenski
Copy link
Contributor

/ok-to-test

shelved multiline marker comments for another PR
@alexzielenski
Copy link
Contributor

/hold cancel
/lgtm

@k8s-ci-robot k8s-ci-robot added lgtm "Looks good to me", indicates that a PR is ready to be merged. and removed do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. labels Jan 3, 2024
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: alexzielenski, Schnides123

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-ci-robot k8s-ci-robot merged commit eec4567 into kubernetes:master Jan 3, 2024
2 checks passed
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. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants