Skip to content

wip: feat(requiredfields): add K8s Declarative Validation marker support#228

Open
sivchari wants to merge 1 commit intokubernetes-sigs:mainfrom
sivchari:feat/requiredfields-k8s-dv-markers
Open

wip: feat(requiredfields): add K8s Declarative Validation marker support#228
sivchari wants to merge 1 commit intokubernetes-sigs:mainfrom
sivchari:feat/requiredfields-k8s-dv-markers

Conversation

@sivchari
Copy link
Member

@sivchari sivchari commented Feb 18, 2026

Add support for K8s DV markers in the requiredfields linter:

  • +k8s:required marker detection with +required suggestion
  • +k8s:minLength, +k8s:minimum, +k8s:maximum, +k8s:minItems markers
  • +k8s:enum support with const value analysis to detect if empty string is a valid enum value

The k8sEnumAllowsEmpty function analyzes const values of enum types using go/types package to determine if empty string is allowed.

Fixes: #224

@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Feb 18, 2026
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: sivchari

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

The pull request process is described here

Details 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 approved Indicates a PR has been approved by an approver from all required OWNERS files. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Feb 18, 2026
markers.KubebuilderMinLengthMarker,
markers.K8sMinLengthMarker,

// MinProperties markers
Copy link
Contributor

Choose a reason for hiding this comment

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

@everettraven do you know if there's anything in progress for MinProperties in DV?

Copy link
Contributor

Choose a reason for hiding this comment

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

Not that I'm aware of. Issue here: jpbetz/validation-gen#251

Copy link
Member Author

Choose a reason for hiding this comment

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

Since DV doesn't have minProperties yet (jpbetz/validation-gen#251), only the kubebuilder marker is registered here. I'm happy to add k8s:minProperties support if needed.


// Check +k8s:required marker (for declarative validation)
// If +k8s:required is present but neither +required nor +kubebuilder:validation:Required is present, suggest adding the preferred one
hasPreferredRequired := fieldMarkers.Has(markers.RequiredMarker)
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be based on the configured preferred required marker?


// checkK8sRequired checks for +k8s:required marker usage.
// +k8s:required is for declarative validation and is separate from +required or +kubebuilder:validation:Required markers.
// If the field has +k8s:required but doesn't have +required or +kubebuilder:validation:Required, we suggest adding the preferred one.
Copy link
Contributor

Choose a reason for hiding this comment

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

At some point, we expect that k8s:required will be the preferred singular choice. So maybe we need to be able to configure the preferred marker also as k8s:required, and in that case, not suggest adding anything additional

return fieldMarkers.Has(markers.KubebuilderEnumMarker)
// Check if the field has a kubebuilder enum marker on the field,
// or if the field's type has a +k8s:enum marker.
// Note: fieldMarkers includes type markers via TypeAwareMarkerCollectionForField.
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens if the +k8s:enum is on the field, not the type? We probably don't want to allow that?

Add support for K8s DV markers in the requiredfields linter:
- +k8s:required marker detection with +required suggestion
- +k8s:minLength, +k8s:minimum, +k8s:maximum, +k8s:minItems markers
- +k8s:enum support with const value analysis to detect if empty
  string is a valid enum value

The k8sEnumAllowsEmpty function analyzes const values of enum types
using go/types package to determine if empty string is allowed.

Signed-off-by: sivchari <shibuuuu5@gmail.com>
@sivchari sivchari force-pushed the feat/requiredfields-k8s-dv-markers branch from 0798af6 to 24d1ae2 Compare March 11, 2026 05:56
@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 Mar 11, 2026
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. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[declarative-validation] Add DV marker support to requiredfields

4 participants