Skip to content

Add comprehensive documentation for Declarative Validation#667

Open
aaron-prindle wants to merge 2 commits intokubernetes:masterfrom
aaron-prindle:dv-docs
Open

Add comprehensive documentation for Declarative Validation#667
aaron-prindle wants to merge 2 commits intokubernetes:masterfrom
aaron-prindle:dv-docs

Conversation

@aaron-prindle
Copy link

@aaron-prindle aaron-prindle commented Feb 25, 2026

Introduces a new documentation section for Declarative Validation (validation-gen) to k8s.dev (content/en/docs/code/declarative-validation/). This includes organized guides to help developers and API reviewers leverage DV efficiently in upcoming releases (v1.36+). This is related to this Slack conversation here:
https://kubernetes.slack.com/archives/C1J0BPD2M/p1771357063367439

The documentation is split across four pages:

  • Overview: Architecture, core concepts, and tooling.
  • Usage and Migration: Developer guide for both new APIs and migrating existing handwritten logic.
  • Validation Tags: A complete reference catalog of all supported +k8s: tags.
  • API Reviewers Guide: PR review guidelines, common pitfalls, and checklists.

These pages aim to explain declarative validation to API reviewers and developers in the goal of reducing the API review burden and broadening the adoption of declarative validation natively for new features.

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: aaron-prindle
Once this PR has been reviewed and has the lgtm label, please assign derekwaynecarr for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found 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 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 25, 2026
@k8s-ci-robot k8s-ci-robot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Feb 25, 2026
@aaron-prindle aaron-prindle force-pushed the dv-docs branch 2 times, most recently from 4bc9202 to 3959031 Compare February 26, 2026 00:18
Introduces a new documentation section for Declarative Validation (`validation-gen`)
to k8s.dev (`content/en/docs/code/declarative-validation/`). This includes
organized guides to help developers and API reviewers leverage DV efficiently in
upcoming releases (v1.36+).

The documentation is split into four categorized pages:
- Overview: Architecture, core concepts, and tooling.
- Usage and Migration: Developer guide for both new APIs and migrating existing handwritten logic.
- Validation Tags: A complete reference catalog of all supported `+k8s:` tags.
- API Reviewers Guide: PR review guidelines, common pitfalls, and checklists.

These pages aim to reduce the API review burden and streamline the adoption of declarative
validation natively for new features.
Copy link
Member

@lmktfy lmktfy left a comment

Choose a reason for hiding this comment

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

The only thing where I would insist on a change is the level 1 headings, which aren't needed. Lines like # Usage and Migration can just be removed.

If you drop them, I'd be happy to merge it as is. I have offered more feedback but it really doesn't block a merge.

Overview and architecture of the Declarative Validation (`validation-gen`) project.
---

# Declarative Validation (`validation-gen`)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# Declarative Validation (`validation-gen`)

Here's why:
Declarative Validation / Overview and architecture of the Declarative Validation (validation-gen) project. / 
Declarative Validation (validation-gen) / This document provides an overview of the Declarative Validation project in Kubernetes, also known as validation-gen. This feature allows developers to define validation logic for native Kubernetes API types using Go comment tags (e.g., +k8s:minimum=0).

We only need a single title for the page.

Copy link
Author

Choose a reason for hiding this comment

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

Done! I have removed the redundant H1 titles across all the new pages (_index.md, api-reviewers-guide.md, and usage-and-migration.md)


This document provides an overview of the Declarative Validation project in Kubernetes, also known as `validation-gen`. This feature allows developers to define validation logic for native Kubernetes API types using Go comment tags (e.g., `+k8s:minimum=0`).

## Architecture Overview
Copy link
Member

Choose a reason for hiding this comment

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

nit: headings within the page should be in sentence case (eg "Architecture overview")

Nice to have but doesn't block a merge.

Copy link
Author

Choose a reason for hiding this comment

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

Fixed for this case, will do a second pass for the other cases

Comment on lines +51 to +53
* [Usage and Migration](./usage-and-migration) - Learn how to use declarative validation for new APIs and how to migrate existing handwritten validation.
* [Validation Tags](./validation-tags) - See the full catalog of available validation tags.
* [API Reviewers Guide](./api-reviewers-guide) - Guidelines for API reviewers on reviewing PRs that use declarative validation.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* [Usage and Migration](./usage-and-migration) - Learn how to use declarative validation for new APIs and how to migrate existing handwritten validation.
* [Validation Tags](./validation-tags) - See the full catalog of available validation tags.
* [API Reviewers Guide](./api-reviewers-guide) - Guidelines for API reviewers on reviewing PRs that use declarative validation.
* [Usage and Migration](/docs/code/declarative-validation/usage-and-migration/) - Learn how to use declarative validation for new APIs and how to migrate existing handwritten validation.
* [Validation Tags](/docs/code/declarative-validation/validation-tags/) - See the full catalog of available validation tags.
* [API Reviewers Guide](/docs/code/declarative-validation/api-reviewers-guide/) - Guidelines for API reviewers on reviewing PRs that use declarative validation.

Guidelines for API reviewers on reviewing PRs that use Declarative Validation for new APIs and migrations.
---

# API Reviewers Guide for Declarative Validation
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# API Reviewers Guide for Declarative Validation

We only need a single title for the page. The theme adds one based on the front matter.

Copy link
Author

Choose a reason for hiding this comment

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

Removed the duplicate heading.

How to use Declarative Validation for new APIs and how to migrate existing handwritten validation.
---

# Usage and Migration
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# Usage and Migration

We only need a single title for the page. The theme adds one based on the front matter.

* `DeclarativeValidationBeta`: (Beta, Default: `true`) Introduced in v1.36. This gate acts as the global safety switch for Beta-stage validation rules. It determines whether validation rules marked as `+k8s:beta` are enforced (authoritative) or remain in shadow mode. It has no effect if `DeclarativeValidation` is disabled.
* `DeclarativeValidationTakeover`: (Deprecated in v1.36). Previously used to determine whether declarative validation results were authoritative. It is no longer honored but can still be set during upgrades to prevent "gate not recognized" errors.

**Default Behavior (Kubernetes v1.36+):**
Copy link
Member

Choose a reason for hiding this comment

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

If accepting the previous heading suggestion, try this:

Suggested change
**Default Behavior (Kubernetes v1.36+):**
### Default behavior (Kubernetes v1.36+) {#default-behavior}


While primarily a feature impacting Kubernetes contributors and potentially developers of [extension API servers](/docs/concepts/extend-kubernetes/api-extension/apiserver-aggregation/), cluster administrators should understand its behavior, especially during its rollout phases.


Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
## Rollout of declarative validation
Declarative validation is being rolled out gradually using a _validation lifecycle_ mechanism.

title: "Declarative Validation"
weight: 50
description: |
Overview and architecture of the Declarative Validation (`validation-gen`) project.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Overview and architecture of the Declarative Validation (`validation-gen`) project.
Overview and architecture of declarative API validation in Kubernetes.

I think these docs will be useful even after we have switched over.


# API Reviewers Guide for Declarative Validation

Starting in Kubernetes v1.36, Declarative Validation (DV) is the recommended way to author API validation logic. This means in v1.36+ there will be k8s API PRs that need API review which use Declarative Validation which means API reviewers need to understand how to review DV PRs. The review should be straightforward as DV's goal is to moving validation logic out of procedural Go code (`validation.go`) and into vetted declarative comment tags directly on the API types (`types.go`).
Copy link
Member

Choose a reason for hiding this comment

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

(nit)

Suggested change
Starting in Kubernetes v1.36, Declarative Validation (DV) is the recommended way to author API validation logic. This means in v1.36+ there will be k8s API PRs that need API review which use Declarative Validation which means API reviewers need to understand how to review DV PRs. The review should be straightforward as DV's goal is to moving validation logic out of procedural Go code (`validation.go`) and into vetted declarative comment tags directly on the API types (`types.go`).
Starting in Kubernetes v1.36, _Declarative Validation_ (DV) is the recommended way to author API validation logic.
This means that for v1.36 and later, there will be K8s API PRs that need API review and which use Declarative Validation.
As a result, API reviewers need to understand how to review PRs that include DV.
The review process should be straightforward as DV's goal is to moving validation logic out of procedural Go code (`validation.go`),
and into vetted declarative comment tags directly on the API types (`types.go`).


When a developer adds a new API or field and wants to use DV, the tags in `types.go` are the *only* validation logic. There should be no fallback handwritten code for these standard rules.

### What to expect:
Copy link
Member

Choose a reason for hiding this comment

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

(nit)

Suggested change
### What to expect:
### What to expect

Copy link
Member

@lmktfy lmktfy left a comment

Choose a reason for hiding this comment

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

LGTM for docs & for contributor experience.

I would like someone representing either SIG Architecture or SIG API Machinery (no special need to cover both) to glance through this and confirm that nothing is obviously wrong. It all seems fine but I haven't actually looked at any of the specifics.

@aaron-prindle
Copy link
Author

/cc @jpbetz

@k8s-ci-robot k8s-ci-robot requested a review from jpbetz March 4, 2026 18:28
Comment on lines +38 to +45
* **Format Code**:
```bash
hack/update-gofmt.sh
```
* **Run All Verification Checks**:
```bash
hack/verify-all.sh
```
Copy link

Choose a reason for hiding this comment

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

I recommend against re-documenting comment development tooling not specific to validation-gen here. Linking to other documentation may be more appropriate.

Comment on lines +34 to +36
* **Run `validate/*` Logic Tests**:
```bash
go test ./staging/src/k8s.io/apimachinery/pkg/api/validate/...
Copy link

Choose a reason for hiding this comment

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

How do we get access to or regenerate the tag documentation?


## Scenario A: New API Field (Authoritative DV)

When a developer adds a new API or field and wants to use DV, the tags in `types.go` are the *only* validation logic. There should be no fallback handwritten code for these standard rules.
Copy link

Choose a reason for hiding this comment

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

Suggested change
When a developer adds a new API or field and wants to use DV, the tags in `types.go` are the *only* validation logic. There should be no fallback handwritten code for these standard rules.
When a developer adds a new API or field and wants to use DV, the tags in `types.go` are the *only* validation logic. We *do not* require any handwritten validation code be authored for validation enforced by DV.

Maybe better? I'm looking for a way to explain this to people not deeply familiar with DV.

Comment on lines +29 to +34
Before tags can be used, the developer must ensure code generation is enabled for the API package.
```go
// +k8s:validation-gen=TypeMeta
// +k8s:validation-gen-input=k8s.io/api/<group>/<version>
package v1
```
Copy link

Choose a reason for hiding this comment

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

Show the full file name please. We have internal and versioned APIs, and it's easy to mistakenly add this to the wrong one. We should state clearly that this belongs on version APIs, it might even be worth adding a warning here about making sure to add this to the correct doc.go file.

Comment on lines +36 to +37
#### 2. `types.go` (The Single Source of Truth)
The developer adds standard DV tags directly to the new field.
Copy link

Choose a reason for hiding this comment

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

Same here. Make sure to clarify that we put the tags on versioned types.

#### 2. `types.go` (The Single Source of Truth)
The developer adds standard DV tags directly to the new field.

**Subresources:** If the API includes a `/status` subresource and validation is needed for it, the root type definition must include the `supportsSubresource` tag.
Copy link

Choose a reason for hiding this comment

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

This is the most common type of subresource, but it's not the only type of subresource and the other types behave differently: https://github.com/kubernetes/enhancements/blob/master/keps/sig-api-machinery/5073-declarative-validation-with-validation-gen/README.md#subresources

Recommend clarify this.

Comment on lines +48 to +54
type MyNewFeatureSpec struct {
// +required
// +k8s:required
// +k8s:maxLength=256
// +k8s:format=k8s-short-name
FeatureName string `json:"featureName"`
}
Copy link

Choose a reason for hiding this comment

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

I was expect a subresource example about Status to show a status stanza. It's super surprising that this example does not.

}
```

* **Tags come from the official catalog.** Invented or misspelled tags are silently ignored.
Copy link

Choose a reason for hiding this comment

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

Somehow link or provide a way for the reader to access the official catelog?

Copy link

Choose a reason for hiding this comment

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

Invented or misspelled tags are silently ignored

True. But for documentation purposes, it might be worth explaining the significance this to a API author.

(I'd expect our linter to catch k8s: tag misspellings? Does it? Should it?)

```

* **Tags come from the official catalog.** Invented or misspelled tags are silently ignored.
* **Tags are applied across all API versions.** If the resource has both `v1` and `v1beta1`, tags must appear on both.
Copy link

Choose a reason for hiding this comment

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

Suggested change
* **Tags are applied across all API versions.** If the resource has both `v1` and `v1beta1`, tags must appear on both.
* **Tags must be applied across all API versions.** If the resource has both `v1` and `v1beta1`, tags must appear on both.


* **Tags come from the official catalog.** Invented or misspelled tags are silently ignored.
* **Tags are applied across all API versions.** If the resource has both `v1` and `v1beta1`, tags must appear on both.
* **No handwritten `Validate*` functions for the same constraints.** The tags are authoritative.
Copy link

Choose a reason for hiding this comment

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

This is not always true. We sometimes have both.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants