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 OpenAPI required tag for all required types #6397

Open
Tracked by #10852
killianmuldoon opened this issue Apr 8, 2022 · 11 comments
Open
Tracked by #10852

Add OpenAPI required tag for all required types #6397

killianmuldoon opened this issue Apr 8, 2022 · 11 comments
Labels
area/api Issues or PRs related to the APIs help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. kind/api-change Categorizes issue or PR as related to adding, removing, or otherwise changing an API kind/feature Categorizes issue or PR as related to a new feature. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. triage/accepted Indicates an issue or PR is ready to be actively worked on.

Comments

@killianmuldoon
Copy link
Contributor

In our API types we currently conceive 'Required' as meaning not optional.

Our optional types have the following definition:

An optional field MUST be marked with +optional and include an omitempty JSON tag.
Fields SHOULD be pointers if there is a good reason for it, for example:
    the nil and the zero values (by Go standards) have semantic differences.
        Note: This doesn't apply to map or slice types as they are assignable to nil.
    the field is of a struct type, contains only fields with omitempty and you want to prevent that it shows up as an empty object after marshalling (e.g. kubectl get)

Currently with Kubernetes this is enough as fields not marked optional are required in CRDs.

We could be explicit with Optional vs Required in our API definitions by explicitly setting Required on the OpenAPI specs. This would make our API more explicit and holistic on the OpenAPI level.

It could add safety:

You can also specify // +kubebuilder:validation:Optional on a package level, which then switches this defaulting round so that fields are optional if no tag is specified. If someone sets that on your package, it can be quite easy to miss, so it's better to be explicit about what you want on each field

But complicate our UX:

Also the fact that any conceptually required field that you want to default needs to be declared optional for client side validation to not complain e.g kubectl, is suboptimal and confusing.

See the original discussion at: #6383 (comment)

@fabriziopandini @enxebre @JoelSpeed @sbueringer

/kind feature
/area api

@k8s-ci-robot k8s-ci-robot added kind/feature Categorizes issue or PR as related to a new feature. area/api Issues or PRs related to the APIs labels Apr 8, 2022
@killianmuldoon
Copy link
Contributor Author

killianmuldoon commented Apr 8, 2022

A quick test of what adding // +kubebuilder:validation:Optional on a package level surprisingly (for me at least) shows that the change would pass all of our unit integration and e2e tests.

It fails the apidiff test though so the risk of this happening accidentally is extremely low IMO.

@fabriziopandini
Copy link
Member

I don't have strong opinions on using this tag, but IMO it seems to be something we can defer to the next API bump.

From a quick test adding +kubebuilder:validation:Required on a required field doesn't affect the generated CRD (the field is already treated as required no matter of the marker), the same when adding +kubebuilder:validation:Optional on a not required field defined according to our current standard.

TL;DR; following our current standard we are slightly less explicit in the code, but we are already ok in generated CRDs.

@JoelSpeed
Copy link
Contributor

My vote, to avoid potential surprises later is that we should be explicit on all fields and have either +kubebuilder:validation:Required or +optional|+kubebuilder:validation:Optional.

Being explicit means should mean that we prevent surprises later should anything change in the logic of the generation (unlikely) or if a user accidentally, or deliberately, adds a package level optional tag (which would change the defaults).
It also has the added benefit of being easier to read as the user doesn't have to know the implicit rules of, required being something that is not optional.

@fabriziopandini fabriziopandini added the kind/api-change Categorizes issue or PR as related to adding, removing, or otherwise changing an API label Apr 12, 2022
@sbueringer
Copy link
Member

sbueringer commented Apr 26, 2022

I think it's hard to accidentally add the optional marker on the package level and miss it in reviews as a lot of CRDs change / or the verify job fails.

But I think it would be still nice to be explicit so that on every single field there is an explicit decision to make a field optional or required.

My only concern is if it's sustainable to enforce via manual reviews that every single field has an optional or required marker. It's easier to ensure nobody adds the package-level optional (as the impact is really obvious).

So overall I'm in favor of making it explicit, but it would be nice to add a linter as follow-up to enforce this automatically on all fields.

P.S. nit: I think we're talking about markers, tags in Go are something else.

@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle stale
  • Mark this issue or PR as rotten with /lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jul 25, 2022
@killianmuldoon
Copy link
Contributor Author

/remove-lifecycle stale

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jul 25, 2022
@fabriziopandini fabriziopandini added triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed triage/accepted Indicates an issue or PR is ready to be actively worked on. labels Jul 29, 2022
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle stale
  • Mark this issue or PR as rotten with /lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Oct 27, 2022
@fabriziopandini
Copy link
Member

/lifecycle frozen
/triage accepted

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. lifecycle/frozen Indicates that an issue or PR should not be auto-closed due to staleness. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels Nov 2, 2022
@k8s-triage-robot
Copy link

This issue has not been updated in over 1 year, and should be re-triaged.

You can:

  • Confirm that this issue is still relevant with /triage accepted (org members only)
  • Close this issue with /close

For more details on the triage process, see https://www.kubernetes.dev/docs/guide/issue-triage/

/remove-triage accepted

@k8s-ci-robot k8s-ci-robot added needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. and removed triage/accepted Indicates an issue or PR is ready to be actively worked on. labels Jan 19, 2024
@sbueringer
Copy link
Member

/triage accepted

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Jan 19, 2024
@fabriziopandini
Copy link
Member

/priority important-longterm

@k8s-ci-robot k8s-ci-robot added the priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. label Apr 12, 2024
@fabriziopandini fabriziopandini removed the lifecycle/frozen Indicates that an issue or PR should not be auto-closed due to staleness. label Apr 16, 2024
@fabriziopandini fabriziopandini added the help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. label May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/api Issues or PRs related to the APIs help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. kind/api-change Categorizes issue or PR as related to adding, removing, or otherwise changing an API kind/feature Categorizes issue or PR as related to a new feature. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

No branches or pull requests

6 participants