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

Add ability to set default field requirement status #211

Merged
merged 1 commit into from
Jun 7, 2019

Conversation

damemi
Copy link
Contributor

@damemi damemi commented May 15, 2019

This introduces a // +kubebuilder:validation:[Required/Optional] marker which can be applied to both packages and fields. Placing it at the package level determines the default requirement for fields in that package (ie, a package marked with // +kubebuilder:validation:Optional will assume no fields are required unless specifically marked with // +kubebuilder:validation:Required).

If no package-level tag is set, this defaults to the current behavior of assuming all fields not tagged with omitempty, inline, or (now) // +kubebuilder:validation:Optional are required, so this change doesn't break current use cases.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label May 15, 2019
@k8s-ci-robot k8s-ci-robot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label May 15, 2019
@damemi damemi force-pushed the add-required-tag branch from 6a05e9e to 0f2cb74 Compare May 15, 2019 19:42
@damemi
Copy link
Contributor Author

damemi commented May 15, 2019

/hold

might change this to cover both cases and let people set their default or keep the current default

@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 May 15, 2019
@damemi damemi changed the title ⚠ Add required validation tag, making fields optional by default ⚠ Add required validation tag and ability to make fields optional by default May 16, 2019
@damemi damemi force-pushed the add-required-tag branch from 2df1105 to a68f302 Compare May 16, 2019 20:49
@damemi
Copy link
Contributor Author

damemi commented May 16, 2019

I've updated this with the ability to set a package-level // +defaultOptional=true tag which enables the // +required tag, otherwise using the original default behavior as described above

@sttts
Copy link
Contributor

sttts commented May 16, 2019

Why another tag? Aren‘t +required/optional enough?

@damemi
Copy link
Contributor Author

damemi commented May 17, 2019

@sttts you mean just re-use required/optional at the package level? Yeah, I guess we could do it that way too. However +optional doesn't exist right now, so we'd at least have to add that new marker. Come to think of it, the implementation of that would actually add 4 new markers though, (required/optional for package and required/optional for validation).

@sttts
Copy link
Contributor

sttts commented May 17, 2019

more markers is fine (in implementation). But for the users, it looks like one, right? just different levels.

@damemi damemi force-pushed the add-required-tag branch 2 times, most recently from 2eab8e7 to 449036f Compare May 17, 2019 20:55
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels May 17, 2019
@damemi
Copy link
Contributor Author

damemi commented May 17, 2019

@sttts Updated this to have a // +kubebuilder:validation:[Required/Optional] marker at both package and field level. Note that I don't think this will carry through to sub-packages. Also if neither is set, we default to the current setting of everything being required by default (which makes the package-level required tag unnecessary, but it could be good to keep for clarity)

@damemi damemi force-pushed the add-required-tag branch from 449036f to 9244fa3 Compare May 17, 2019 20:57
@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label May 31, 2019
@damemi damemi force-pushed the add-required-tag branch from 9244fa3 to fcacb8d Compare June 3, 2019 15:31
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jun 3, 2019
@damemi damemi force-pushed the add-required-tag branch from fcacb8d to 2a0b532 Compare June 3, 2019 15:44
@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jun 3, 2019
@damemi
Copy link
Contributor Author

damemi commented Jun 4, 2019

/hold cancel
@DirectXMan12 any time to take another look at this? Tested with a couple projects and it works for me

@k8s-ci-robot k8s-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jun 4, 2019
@damemi damemi changed the title ⚠ Add required validation tag and ability to make fields optional by default Add ability to set default field requirement status Jun 4, 2019
@damemi damemi force-pushed the add-required-tag branch from 2a0b532 to 83d617e Compare June 4, 2019 17:30
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Jun 4, 2019
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.

minor comments inline, otherwise lgtm

pkg/crd/schema.go Outdated Show resolved Hide resolved
pkg/crd/parser.go Outdated Show resolved Hide resolved
@damemi damemi force-pushed the add-required-tag branch from 83d617e to af492d2 Compare June 5, 2019 17:47
@damemi
Copy link
Contributor Author

damemi commented Jun 6, 2019

@DirectXMan12 updated with your feedback, can you please tag?

@DirectXMan12
Copy link
Contributor

force-pushed an update to register the +optional marker, and to make the Required and Optional markers only function on fields and packages, not on types (which doesn't really make sense).

Will merge once tests pass.

pkg/crd/schema.go Outdated Show resolved Hide resolved
Using +kubebuilder:validation:Optional/Required at the package level sets
the default value for fields in that package to that value, while using the
opposite marker on fields in that package sets them as an exception.
The `+optional` marker is an alias for
`+kubebuilder:validation:Optional`, for interoperability with main
Kubernetes.
@damemi damemi force-pushed the add-required-tag branch from d86df0b to fcf4205 Compare June 7, 2019 14:58
@damemi
Copy link
Contributor Author

damemi commented Jun 7, 2019

Removed the TODO for +optional, tests running again...

@DirectXMan12
Copy link
Contributor

/lgtm
/approve

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

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: damemi, DirectXMan12

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 added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jun 7, 2019
@k8s-ci-robot k8s-ci-robot merged commit 783d255 into kubernetes-sigs:master Jun 7, 2019
DirectXMan12 pushed a commit to damemi/controller-tools that referenced this pull request Jun 27, 2019
Currently the `+optional` and `+nullable` markers don't work. I've fixed
`optional` by making sure the `FieldOnlyMarkers` slice that was added in
 kubernetes-sigs#211 actually gets added to `AllDefinitions`. In the current code,
`+nullable` would actually only be recognized as
`+kubebuilder:validation:Nullable=true`, so I'm updating this to be more
consistent with what's expected for that marker.
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/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants