|
| 1 | +--- |
| 2 | +title: Declarative API Validation |
| 3 | +reviewers: |
| 4 | +- aaron-prindle |
| 5 | +- yongruilin |
| 6 | +- jpbetz |
| 7 | +- thockin |
| 8 | +content_type: concept |
| 9 | +weight: 20 |
| 10 | +--- |
| 11 | + |
| 12 | +{{< feature-state for_k8s_version="v1.33" state="beta" >}} |
| 13 | + |
| 14 | +Kubernetes {{< skew currentVersion >}} includes optional _declarative validation_ for APIs. When enabled, the Kubernetes API server can use this mechanism rather than the legacy approach that relies on hand-written Go |
| 15 | +code (`validation.go` files) to ensure that requests against the API are valid. |
| 16 | +Kubernetes developers, and people [extending the Kubernetes API](/docs/concepts/extend-kubernetes/api-extension/apiserver-aggregation/), |
| 17 | +can define validation rules directly alongside the API type definitions (`types.go` files). Code authors define |
| 18 | +pecial comment tags (e.g., `+k8s:minimum=0`). A code generator (`validation-gen`) then uses these tags to produce |
| 19 | +optimized Go code for API validation. |
| 20 | + |
| 21 | +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. |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +Declarative validation is being rolled out gradually. |
| 26 | +In Kubernetes {{< skew currentVersion >}}, the APIs that use declarative validation include: |
| 27 | + |
| 28 | +* [ReplicationController](/docs/concepts/workloads/controllers/replicationcontroller/) |
| 29 | + |
| 30 | +{{< note >}} |
| 31 | +For the beta of this feature, Kubernetes is intentionally using a superseded API as a test bed for the change. |
| 32 | +Future Kubernetes releases may roll this out to more APIs. |
| 33 | +{{< /note >}} |
| 34 | + |
| 35 | + |
| 36 | +* `DeclarativeValidation`: (Beta, Default: `true`) When enabled, the API server runs *both* the new declarative validation and the old hand-written validation for migrated types/fields. The results are compared internally. |
| 37 | +* `DeclarativeValidationTakeover`: (Beta, Default: `false`) This gate determines which validation result is *authoritative* (i.e., returned to the user and used for admission decisions). |
| 38 | + |
| 39 | +**Default Behavior (Kubernetes {{< skew currentVersion >}}):** |
| 40 | + |
| 41 | +* With `DeclarativeValidation=true` and `DeclarativeValidationTakeover=false` (the default values for the gates), both validation systems run. |
| 42 | +* **The results of the *hand-written* validation are used.** The declarative validation runs in a mismatch mode for comparison. |
| 43 | +* Mismatches between the two validation systems are logged by the API server and increment the `declarative_validation_mismatch_total` metric. This helps developers identify and fix discrepancies during the Beta phase. |
| 44 | +* **Cluster upgrades should be safe** regarding this feature, as the authoritative validation logic doesn't change by default. |
| 45 | + |
| 46 | +Administrators can choose to explicitly enable `DeclarativeValidationTakeover=true` to make the *declarative* validation authoritative for migrated fields, typically after verifying stability in their environment (e.g., by monitoring the mismatch metric). |
| 47 | + |
| 48 | +## Disabling declarative validation {#opt-out} |
| 49 | + |
| 50 | +As a cluster administrator, you might consider disabling declarative validation whilst it is still beta, under specific circumstances: |
| 51 | + |
| 52 | +* **Unexpected Validation Behavior:** If enabling `DeclarativeValidationTakeover` leads to unexpected validation errors or allows objects that were previously invalid. |
| 53 | +* **Performance Regressions:** If monitoring indicates significant latency increases (e.g., in `apiserver_request_duration_seconds`) correlated with the feature's enablement. |
| 54 | +* **High Mismatch Rate:** If the `declarative_validation_mismatch_total` metric shows frequent mismatches, suggesting potential bugs in the declarative rules affecting the cluster's workloads, even if `DeclarativeValidationTakeover` is false. |
| 55 | + |
| 56 | +To revert to only using hand-written validation (as used before Kubernetes v1.33), disable the `DeclarativeValidation` feature gate, for example |
| 57 | +via command-line arguments: (`--feature-gates=DeclarativeValidation=false`). This also implicitly disables the effect of `DeclarativeValidationTakeover`. |
| 58 | + |
| 59 | +## Considerations for downgrade and rollback |
| 60 | + |
| 61 | +Disabling the feature acts as a safety mechanism. However, be aware of a potential edge case (considered unlikely due to extensive testing): If a bug in declarative validation (when `DeclarativeValidationTakeover=true`) *incorrectly allowed* an invalid object to be persisted, disabling the feature gates might then cause subsequent updates to that specific object to be blocked by the now-authoritative (and correct) hand-written validation. Resolving this might require manual correction of the stored object, potentially via direct etcd modification in rare cases. |
| 62 | + |
| 63 | +For details on managing feature gates, see [Feature Gates](/docs/reference/command-line-tools-reference/feature-gates/). |
0 commit comments