Appearance

CRDs, schemas, and conversion

Add a WebApp resource to the Kubernetes API and see which rules are enforced before a controller is running.

What you will understand

  • Distinguish a CustomResourceDefinition from an instance of the type it defines.
  • Predict whether an object will be accepted under the example schema.
  • Explain served versions, storage versions, and how a controller reports observed generation.

Builds on: Finalizers, garbage collection, and API evolution, Informers, caches, and workqueues

Add a new object type to the API

A platform team wants app authors to ask for a web app with a message and replica count. The lab reconciler supplies a Deployment and a ConfigMap for the page content. A CustomResourceDefinition (CRD) registers a new type with the Kubernetes API. After registering WebApp, clients can create and watch WebApp objects. Each saved WebApp is an instance of that custom type. A separate controller has to read the object and create the application resources.

Register the type, apply the WebApp, and start the demo reconciler in the scene. Before registration, the API cannot find that resource endpoint. Afterwards, the API can validate and store a WebApp even if no Deployment is created yet. This lets you diagnose the API definition and the controller independently.

When does a WebApp become a workload?

Register the API type, save one WebApp, then start its reconciler. Watch what each action adds to the cluster.

API serverlabs.kv.example/v1alpha1
Served typeNone registered
Stored objectNone stored
Waiting for a WebApp manifest
Demo reconcilerStopped
Generated resources0
  • ConfigMap—
  • Deployment—
The WebApp API is unavailable

The CRD, WebApp instance, and reconciler are three separate parts of this example.

Text walkthrough
  1. The WebApp API is unavailable

    The CRD, WebApp instance, and reconciler are three separate parts of this example.

  2. The API can now serve WebApps

    Once the CRD is established, the API exposes the WebApp resource and can validate instances using the schema.

  3. WebApp m16 is stored

    The API accepts the WebApp with replicas: 1 and its message. The stored object is present while the generated resource count is still zero.

  4. The reconciler reads the stored WebApp

    The lab reconciler uses the WebApp’s spec to build ordinary Kubernetes resources.

  5. The reconciler writes the page

    A ConfigMap named m16 now holds the page content from the WebApp message.

  6. The reconciler creates the Deployment

    Deployment m16 requests one replica and refers to that ConfigMap. Its Pod still needs time to become ready.

The scene condenses CRD establishment and schema validation. It models starting the lab’s Python reconciler explicitly and applying its ConfigMap and Deployment in separate writes. The generated Deployment’s rollout is outside this scene.

Explore another case

Validate input and manage API versions

The CRD has a schema specifying the fields, types, and validation rules it accepts. The example WebApp requests one replica and a string message. The teaching CRD accepts values from one to five. In a CRD schema, marking fields inside spec as required checks them when spec exists. To require spec itself, add that requirement at the object root. Try an out-of-range replica count using server-side dry run and look for the field the API rejects.

apiVersion: labs.kv.example/v1alpha1
kind: WebApp
metadata:
  name: m16
  namespace: kv-lab
  labels:
    lab: m16
spec:
  replicas: 1 # CRD accepts 1 through 5 in this teaching API.
  message: hello-from-webapp

A CRD can serve multiple API versions while choosing one as the storage version. Conversion translates different served representations. When representations meaningfully differ, the conversion must preserve the data clients need. Before retiring an old storage version, migrate stored objects and verify the supported versions.

Controllers write status to describe what they observed. The API’s metadata.generation changes when desired state changes for the resource. A controller can report an observedGeneration in status or conditions to indicate which spec it evaluated. Look at the scene and ask whether a previously healthy condition describes the most recent edit.

Which version do clients see and which one is stored?

A CRD can serve multiple versions while using one designated storage version. A controller can separately report the generation it observed.

  1. Receive a served version

    The client uses a served representation whose schema defines fields and validation.

  2. Convert to the storage version

    The API uses the designated storage version; adding a version alone does not rewrite old objects.

  3. Report an observation through status

    The controller writes status through its subresource. observedGeneration can advance before readyReplicas reaches the target.

The lab has one version and no conversion webhook; the failure branch models a future multi-version API.

Explore another case

Test acceptance before running the controller

Run the commands from the downloaded module directory in the disposable cluster. The Established condition means the CRD has been registered. kubectl explain reads schema information, and get confirms an instance was stored. Verify both successful and rejected dry-run payloads in the custom resource lab before moving on to the reconciler.

# Run from the downloaded m16 directory.
kubectl --context kind-kv-lab apply -f crd.yaml
kubectl --context kind-kv-lab wait --for=condition=Established crd/webapps.labs.kv.example --timeout=60s
kubectl --context kind-kv-lab -n kv-lab apply -f resources.yaml
kubectl --context kind-kv-lab explain webapp.spec
kubectl --context kind-kv-lab -n kv-lab get webapp m16 -o yaml

Protect stored data when changing an API

Deleting a CRD deletes the custom objects it manages. For a real API, plan migration and document its data ownership before replacing or retiring it. When making a schema change, test both existing objects and new inputs.

Check your understanding

WebApp generation is 4 and status still reports Ready from observedGeneration 3. What can you conclude about generation 4?

The older condition describes the controller’s observation of generation 3. Wait for or investigate a report that covers generation 4 before calling its new configuration ready.

Go to the source

Reviewed 2026-10-27 · Kubernetes 1.37 · Related local exercise

Search the book

Search loads when you open it.

Keyboard shortcuts

Ctrl + K / ⌘ + K
Search the book
?
Open this guide
Tab / Shift + Tab
Move between controls
Enter / Space
Activate a focused button or disclosure
← / →
Focused visual timeline · previous or next step
Home / End
Focused visual timeline · first or last point
Esc
Close the open dialog or menu