Extending the API Server with Admission Webhooks
Intercept and validate or mutate Kubernetes API requests in flight using validating and mutating admission webhooks to enforce cluster policy.
Extending the API Server with Admission Webhooks is a free Docker & Kubernetes for Developers lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Docker & Kubernetes for Developers learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is Admission Control
After authentication and authorization, requests pass through admission controllers that can validate or modify objects before they are persisted.
Two Kinds of Webhooks
Dynamic admission uses webhooks: mutating webhooks change objects, and validating webhooks accept or reject them.
Where They Sit in the Pipeline
The API server runs mutating webhooks first, then validating webhooks, so a mutation can add defaults before validation checks the final object.
A Use Case
Examples: inject a sidecar (mutating), enforce that every Pod sets resource limits (validating), or block images from untrusted registries.
The MutatingWebhookConfiguration
This resource registers your webhook endpoint with the API server.
apiVersion: admissionregistration.k8s.io/v1
kind: MutatingWebhookConfiguration
metadata:
name: add-defaults
webhooks:
- name: defaults.example.com
clientConfig:
service:
name: webhook-svc
namespace: default
path: /mutate
caBundle: <base64-ca>
rules:
- operations: ["CREATE"]
apiGroups: [""]
apiVersions: ["v1"]
resources: ["pods"]The AdmissionReview Payload
The API server sends an AdmissionReview JSON to your webhook and expects an AdmissionReview response with allowed true/false, and for mutation a JSONPatch.
Returning a JSON Patch
A mutating webhook returns a base64-encoded JSONPatch describing the changes to apply.
{"response":{"allowed":true,"patchType":"JSONPatch","patch":"<base64-patch>"}}failurePolicy Matters
failurePolicy: Fail blocks requests if the webhook is down, which is safe but can lock up the cluster; Ignore lets requests through, which is risky for security policies.
Scoping With Selectors
Use namespaceSelector and objectSelector to limit which requests hit your webhook, avoiding overhead and accidental scope creep.
Policy Engines as an Alternative
Instead of writing webhook servers, tools like OPA Gatekeeper and Kyverno provide declarative policies on top of the same admission mechanism.
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: require-limits
spec:
validationFailureAction: Enforce
rules:
- name: check-limits
match:
any:
- resources:
kinds: [Pod]
validate:
message: "CPU and memory limits are required"
pattern:
spec:
containers:
- resources:
limits:
memory: "?*"TLS Is Required
Webhook endpoints must serve HTTPS with a certificate trusted via the caBundle, since the API server only calls webhooks over TLS.
Quick Check
Test what you have learned.
Recap
You learned how admission webhooks extend the API server: mutating webhooks change objects and validating webhooks enforce policy, the AdmissionReview flow, failurePolicy and selectors, TLS requirements, and policy engines like Kyverno and Gatekeeper.
Frequently asked questions
Is the “Extending the API Server with Admission Webhooks” lesson free?
Yes — the full text of “Extending the API Server with Admission Webhooks” is free to read here on the web, and the Docker & Kubernetes for Developers course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Docker & Kubernetes for Developers course, upgrade to CoddyKit PRO.
What will I learn in “Extending the API Server with Admission Webhooks”?
Intercept and validate or mutate Kubernetes API requests in flight using validating and mutating admission webhooks to enforce cluster policy. You practise Docker & Kubernetes for Developers with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Docker & Kubernetes for Developers?
No prior experience is required. Docker & Kubernetes for Developers on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Extending the API Server with Admission Webhooks” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Docker & Kubernetes for Developers lesson?
Yes. Every Docker & Kubernetes for Developers lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Custom Resource Definitions (CRDs)
- The Operator Pattern in Kubernetes
- Serverless with Kubernetes (Knative)
- Extending the API Server with Admission Webhooks