Custom resource validation
Validation, defaulting and pruning are different
Validation rejects values that violate a contract, defaulting fills eligible absent values, and pruning removes unknown fields where the structural schema does not preserve them. They are separate API behaviors, and request field-validation settings can cause unknown fields to be rejected rather than quietly pruned. The BookRequest spec requires title and quantity; quantity must be an integer from one to ten, priority may be normal or urgent. A missing priority can receive the schema default. An integer encoded as a string is not a valid integer field.
Valid: {"title":"Algebra basics","quantity":3}Invalid: {"title":"Algebra basics","quantity":"3"}Invalid: {"title":"Algebra basics","quantity":0}Keep desired state separate from observed status
The spec describes desired state; status reports observed state. Enable the status subresource so clients can update status through a distinct API endpoint and authorize that operation separately. A normal create or update of the main resource ignores status changes when the status subresource is enabled; status updates do not replace the spec. The controller should report what it actually observed, such as fulfilledQuantity, not simply echo the desired quantity. A successful schema validation says nothing about whether an order was fulfilled.
spec: quantity: 3status: fulfilledQuantity: 1 phase: PendingExpose useful status without implying automation
Additional printer columns show selected JSON paths in tabular API responses. Our CRD shows requested quantity and phase, which helps readers compare intent and progress. A missing status value can appear blank. The optional scale subresource is different: it maps desired and observed replica counts onto the standard Scale representation through specReplicasPath and statusReplicasPath, with labelSelectorPath when required by consumers. BookRequest is not a scalable workload, so its CRD deliberately omits scale. Adding scale or a printer column does not create a scaling controller.
additionalPrinterColumns: - name: Quantity type: integer jsonPath: .spec.quantity - name: Phase type: string jsonPath: .status.phaseExpress cross-field rules with CEL where appropriate
OpenAPI handles types and simple numeric or string constraints. Kubernetes supports Common Expression Language (CEL) rules in structural CRD schemas through x-kubernetes-validations. For example, a business rule might restrict urgent requests to at most five books. This rule belongs at the spec object scope and must handle the optional priority field; has(self.priority) checks presence. The expression below is illustrative and not installed by our fixture. Verify CEL syntax, type checking, cost limits and relevant version support on a real approved API server before adoption.
x-kubernetes-validations: - rule: "!has(self.priority) || self.priority != 'urgent' || self.quantity <= 5" message: Urgent requests may request at most five books.Read validation results at the right boundary
The supplied Python test uses jsonschema against only the spec schema extracted from the YAML. It checks ordinary required/type/range/enum constraints, but does not implement Kubernetes structural-schema checks, defaulting, pruning, CEL, admission, conversion, authorization or subresource handling. In an approved isolated cluster, first establish the CRD, inspect discovery and use server-side dry run with valid and invalid objects. Check status and authorization separately. Dry run tests an API request without persisting it, not full controller reconciliation. Do not install a CRD or query a current user context for this exercise.
Offline validator PASS → subset of spec constraints testedServer dry run → API acceptance in selected environmentController observation → business behavior (separate gate)Practice and recap
Run validate_offline.py in lab-fixtures/crd-api using Python with PyYAML and jsonschema already installed. Inspect positive and negative spec cases; add an out-of-range value and explain the expected rejection. Sketch separate read and status-write permissions for the controller. Describe one test for a renamed field and one for cleanup failure. Next chapter: Controllers, operators and client libraries.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Custom resource validation — knowledge check
Validate a custom resource contract, separate status from spec and understand the limits of offline testing.
What is the difference between defaulting and validation?
What should status.fulfilledQuantity describe?
Which path is used to write status separately when enabled?
What does a successful jsonschema spec test prove?
What does a scale subresource alone provide?