Sub topic 3
Extending the API with a CustomResourceDefinition
This chapter assumes you can read Kubernetes YAML, use kubectl, and know the spec/status split. It also assumes you know that controllers reconcile desired state toward observed state. Here you will register a new API type in the cluster.
What a CRD does
A CustomResourceDefinition (CRD) is a Kubernetes object that tells the API server about a new resource type. After you apply a CRD, the API server serves a new REST endpoint for it. It stores instances in etcd, validates them against your schema, and lets kubectl list and edit them like built-in objects.
A CRD only defines the type. It does not make anything happen. A controller must watch the instances and act on them. That comes in later chapters.
A first CRD
This CRD registers a namespaced WebApp type in the example.com group.
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: webapps.example.com
spec:
group: example.com
scope: Namespaced
names:
plural: webapps
singular: webapp
kind: WebApp
versions:
- name: v1
served: true
storage: true
subresources:
status: {}
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
required: ["image"]
properties:
image:
type: string
replicas:
type: integer
minimum: 0
default: 1
status:
type: object
properties:
readyReplicas:
type: integerReading the fields
| Field | Meaning |
|---|---|
| `metadata.name` | Must be `<plural>.<group>`, here `webapps.example.com`. |
| `spec.group` | The API group, used in `apiVersion` as `example.com/v1`. |
| `spec.scope` | `Namespaced` or `Cluster`. It cannot be changed after creation. |
| `spec.names` | The plural, singular and kind names the API uses. |
| `versions[].served` | Whether the API server exposes this version. |
| `versions[].storage` | Whether this version is the one written to etcd. Exactly one version must be `true`. |
| `subresources.status` | Enables a separate `/status` endpoint. |
| `schema.openAPIV3Schema` | The structure and validation rules for instances. |
The required list and minimum are checked when the API server admits an object. The default fills in replicas: 1 when the field is missing. Schemas get their own chapter, so treat this one as a preview.
How a request flows
Applying and checking it
kubectl apply -f webapp-crd.yaml
kubectl get crd webapps.example.com
kubectl api-resources --api-group=example.comThe CRD's Established condition should become True within a few seconds. Until then, requests for WebApp objects fail.
Common errors
- Wrong name. If
metadata.nameis not<plural>.<group>, the API server rejects the CRD. - No structural schema. In
apiextensions.k8s.io/v1, every version needs a schema, and it must be structural: every field has atype. - Missing storage version. Exactly one version needs
storage: true. - Deleting a CRD. This deletes every custom resource of that type. Treat it like dropping a database table.
Exercise
A teammate applies a CRD with names.plural: webapps and group: example.com, but sets metadata.name: webapp.example.com. What happens, and how do they fix it?
Show answer
The API server rejects the CRD, because the name must be <plural>.<group>. They should set metadata.name to webapps.example.com.