Skip to content

Lesson 5 of 9 · 24m

Designing CRDs as Real APIs

Spec versus status, validation, versioning, ownership boundaries, and the CRD design mistakes that create permanent operational debt.

A CustomResourceDefinition is an API contract. Once users depend on it, careless fields become permanent. The painful CRDs are rarely too small; they are too broad. They expose implementation details, mix desired state with runtime observations, or try to model every knob in the underlying software.

Spec is desired state; status is observed state

apiVersion: platform.example.io/v1alpha1
kind: PostgresCluster
metadata: {name: orders-db}
spec: {version: "16", storageSize: 100Gi, replicas: 3}
status:
  phase: Ready
  observedGeneration: 7
  connectionSecret: orders-db-app
  conditions:
    - {type: Ready, status: "True", reason: ReplicasAvailable}

Users write spec. The operator writes status. Never reconcile from status as if it were desired intent, and never let users edit status directly. observedGeneration is crucial: it tells clients whether status reflects the latest spec or an older generation.

Validate at the API boundary

OpenAPI validation in the CRD schema is cheap compared with repairing bad state later:

schema:
  openAPIV3Schema:
    type: object
    properties:
      spec:
        type: object
        required: ["version", "storageSize"]
        properties:
          version:
            type: string
            pattern: '^(15|16|17)(\.[0-9]+)?$'
          storageSize:
            type: string
          replicas:
            type: integer
            minimum: 1
            maximum: 9

Validation is not only for correctness. It communicates supported shapes. If replicas: 0 means pause, say so explicitly; do not let people infer it from implementation behavior.

Version before you need to

Use v1alpha1 while the API is honestly unstable, but design conversion early. Kubernetes serves multiple versions and stores one storage version. Renaming or changing types later requires conversion logic, migration planning, and often dual-writing. A field added as optional is easy; a field whose meaning changes is expensive.

Model intent, not commands

Bad CRD fields often encode operations: reboot: true, restoreFromBackup: xyz, resizeNow: true. A control loop wants persistent intent. Prefer declarative fields such as desiredVersion, backup.restoreSource, or explicit one-shot Job resources when the action is genuinely transactional. Otherwise users are left asking whether reboot: true should ever be reset and by whom.

Field notes

  • Keep one controller as the clear owner of each child resource; shared ownership causes update fights.
  • Use CEL validation for cross-field rules such as "backup retention is required when backups are enabled."
  • The status subresource prevents ordinary users from accidentally writing status and lets RBAC distinguish roles.
  • A CRD is not a place to hide bad UX. If the API is confusing in YAML, it will be confusing everywhere.

Was this lesson helpful?

Sign in to rate