API Extension#
Three ways to extend the Kubernetes API. All three are registered dynamically as API objects, without touching kube-apiserver flags.
| Method | Registration object | What it does |
|---|---|---|
| Admission webhook | MutatingWebhookConfiguration, ValidatingWebhookConfiguration | Intercepts requests to mutate or reject them |
| CRD | CustomResourceDefinition | Stores a new resource type in etcd |
| Aggregated API | APIService | Proxies requests for an API group to a separate server |
Admission Webhook#
There are two kinds of admission control.
| Kind | How to register | Examples |
|---|---|---|
| Compiled-in admission plugins | --enable-admission-plugins flag (edit the static pod) | NodeRestriction, ImagePolicyWebhook |
| Dynamic admission webhooks | kubectl apply a webhook configuration object | Kueue, Istio sidecar injection, Gatekeeper |
Dynamic webhooks are themselves called by two compiled-in plugins, MutatingAdmissionWebhook and ValidatingAdmissionWebhook. Both are enabled by default, so no flag change is needed. The API server watches webhook configuration objects and, when a request matches, makes an HTTPS call to the Service named there.
apiVersion: admissionregistration.k8s.io/v1
kind: MutatingWebhookConfiguration
metadata:
name: kueue-mutating-webhook-configuration
webhooks:
- name: mjob.kb.io
clientConfig:
service: # What the API server calls
name: kueue-webhook-service
namespace: kueue-system
path: /mutate-batch-v1-job
failurePolicy: Fail # If the webhook does not answer, the request fails
rules:
- apiGroups: ["batch"]
apiVersions: ["v1"]
operations: ["CREATE"]
resources: ["jobs"]- The configuration object only declares when and where to call. What to mutate or reject lives in the code of the server being called.
- With
failurePolicy: Fail, a dead backing Pod blocks all creation of the matched resources. - On private clusters, the control plane must be able to reach the webhook port.
Aggregated API (APIService)#
An APIService is a routing registration: “send requests for this API group and version to this Service”. The feature is called the API Aggregation Layer.
apiVersion: apiregistration.k8s.io/v1
kind: APIService
metadata:
name: v1beta2.visibility.kueue.x-k8s.io
spec:
group: visibility.kueue.x-k8s.io
version: v1beta2
service:
name: kueue-visibility-server
namespace: kueue-system- The
APIServiceobject is not a Pod. The extension API server that receives the requests runs as one. - List registrations with
kubectl get apiservices. Built-in API groups appear there too. - Authentication, RBAC, and
kubectlusage are the same as for ordinary resources. - The best-known example is metrics-server’s
v1beta1.metrics.k8s.io, which backskubectl top.
CRD vs Aggregated API#
| CRD | Aggregated API | |
|---|---|---|
| Storage | etcd | The extension API server itself |
| Request handling | kube-apiserver | Proxied to a separate server |
| Behavior | CRUD and watch on stored objects | Any response you implement |
| Cost to build | Low | High (you implement and operate an API server) |
The choice depends on the nature of the data.
- State that users declare and a controller reconciles: CRD + controller. Most operators use only this.
- Volatile data computed on each read: Aggregated API. Storing it in etcd costs a write on every change, and the value is already stale by the time it is read.
Pod CPU usage (metrics-server) and queue positions (Kueue) are examples of the latter.