- Define a CRD with an OpenAPI v3 schema for server-side validation.
- Apply the CRD and wait until it is established.
- Create a namespace and a sample custom resource (CR).
- Observe schema validation rejecting invalid CRs.
1) Edit the CRD
Openwidget-crd.yaml (for example, in VS Code) and add a shortName and an OpenAPI v3 schema under the version you serve. The example below demonstrates:
shortNames: [wj]sokubectl get wjworks.specschema requiringsizeandcolor.sizeconstrained by an enum:small,medium,large.replicaswithminimum,maximum, and adefault.
Using
shortNames (for example wj) makes commands like kubectl get wj possible and more convenient.2) Apply the CRD and wait until it is established
Apply the CRD to the cluster and wait until the API server marks it asEstablished. This ensures the new resource kind is served by the API.
kubectl get crd widgets.training.example.com -o yamlkubectl api-resources | grep widget
3) Create a namespace for the demo
Create a dedicated namespace to isolate demo resources:4) Create a sample Widget (valid CR)
Createwidget-sample.yaml with valid values that conform to the CRD schema:
5) Apply a deliberately invalid Widget to see validation
To demonstrate server-side schema validation, createbad-widget.yaml that violates the size enum:
size: huge is not one of the allowed enum values defined in the CRD.
If you update your CRD schema after CRs already exist, be careful: removing fields or tightening validation can cause existing objects to fail validation. Prefer adding new fields or using a migration strategy.
Summary and next steps
- You created a namespaced CRD with an OpenAPI v3 schema and a
shortNamefor convenience. - You applied a valid CR and confirmed it is stored by the API.
- You verified that server-side validation prevents invalid CRs from being created.
- Add additional validation (patterns,
maxLength, nested object validation) to the CRD schema as needed. - Implement a controller for the Widget kind to reconcile and act on Widget objects.
- Read more: Extending the Kubernetes API with CRDs.