Skip to main content
This lesson explains Kubernetes admission controllers, how they run during API-server request processing, and how to implement and configure webhook-based admission controllers. You’ll learn the difference between validating and mutating controllers, the AdmissionReview protocol the API server uses to call webhooks, and the steps required to deploy and register your webhook. What are admission controllers?
  • Admission controllers are plugins invoked by the API server after authentication and authorization but before an object is persisted.
  • They can either mutate the incoming object (mutating admission controllers) or accept/deny the request (validating admission controllers). Some controllers perform both roles.
For example, the NamespaceExists controller validates that a referenced namespace exists; it will reject the request if it does not. The DefaultStorageClass controller mutates PVC creation requests by adding a storageClassName when none is supplied.
A diagram titled "Validating Admission Controllers" showing a Kubernetes request flow: kubelet → Authentication → Authorization → Admission Controllers (NamespaceExists) → Create Pod. The graphic illustrates the checks performed before a pod is created.
Mutating vs Validating — key differences Important ordering note: mutating controllers are invoked before validating ones. For instance, a mutating controller that auto-provisions a namespace (NamespaceAutoProvision) must run before NamespaceExists; otherwise, NamespaceExists could reject the request for a non-existent namespace. If any admission controller rejects a request, the API server returns an error and the request is not applied. Webhook-based admission controllers Kubernetes also supports webhook-based admission controllers to allow custom logic hosted as external services:
  • MutatingAdmissionWebhook — can modify the request using JSONPatch and return patches.
  • ValidatingAdmissionWebhook — only allows or denies the request.
The API server sends an AdmissionReview object (JSON) to the webhook and expects an AdmissionReview response with a matching uid and an allowed boolean. For mutating webhooks, the response can include a base64-encoded JSONPatch and patchType: "JSONPatch". AdmissionReview request (example)
AdmissionReview response (allowed)
If allowed is false, you may include a status with a message explaining why the request was rejected. Two main steps to add a webhook-based admission controller
  1. Deploy the admission webhook server (your logic).
  2. Register the webhook with the API server using a MutatingWebhookConfiguration or ValidatingWebhookConfiguration object that points to your server.
Deploying the webhook server
  • Language: You can write the webhook server in any language that can accept and return AdmissionReview JSON objects.
  • Location: Host the webhook inside the cluster (Deployment + Service) or outside the cluster. If inside, the webhook registration can reference the in-cluster Service.
  • TLS: The API server calls webhooks over TLS. The webhook server must serve TLS and the webhook configuration must include the CA bundle that signed the webhook server certificate.
Compact Go example — webhook HTTP handler This Go snippet shows the shape of a webhook HTTP handler using the admission/v1 types. It reads and decodes the AdmissionReview request, calls an admit function, and writes back a matching AdmissionReview response.
Python example — building and returning a JSONPatch for a mutating webhook This Flask example builds a JSONPatch to add a label, JSON-encodes it, base64-encodes the bytes, and returns it in the AdmissionReview response. Note: the patch must be JSON-encoded bytes before base64 encoding.
From an exam perspective, you are not expected to implement webhook servers. Understand the concepts and how admission webhooks are invoked, and that the webhook must accept and respond with AdmissionReview objects.
Hosting considerations
  • In-cluster: deploy your webhook as a Deployment and expose it via a Service (for example, webhook-service).
  • TLS: the webhook must present a certificate whose Common Name (CN) or SAN matches the Service DNS name. The clientConfig.caBundle in the webhook configuration must contain the CA that signed the webhook server certificate (base64-encoded).
  • Out-of-cluster: register the external URL in the webhook configuration; TLS is still required.
A slide diagram titled "Configuring Admission Webhook" showing a Kubernetes cluster containing a webhook-service and a webhook-deployment, with an inner circle labeled "Admission Webhook Server." The components are represented by simple shapes (triangle for service, rounded square and circle for the deployment and server).
Configuring the API server to call your webhook
  • Use MutatingWebhookConfiguration for mutating webhooks and ValidatingWebhookConfiguration for validators.
  • Each webhook entry contains:
    • name: unique webhook name.
    • clientConfig: where the API server should call (Service or URL) and caBundle.
    • rules: which resources, API groups, versions, and operations the API server should send to the webhook.
    • admissionReviewVersions and sideEffects indicating supported review versions and side-effect semantics.
Configuration fields at a glance Example ValidatingWebhookConfiguration Replace the caBundle value with your base64-encoded CA certificate.
TLS is required for webhook communication. Ensure the webhook server’s certificate matches the service DNS name and that the caBundle in the webhook configuration contains the CA that signed that server certificate.
Operation and lifecycle
  • After creating the webhook configuration, the API server evaluates matching requests against the rules and sends AdmissionReview objects to the webhook.
  • Mutating webhooks may return patches; the API server applies them and then runs validators against the mutated object.
  • Webhook responses control whether requests are admitted; failure to contact a webhook may cause requests to be denied depending on the failurePolicy (if configured).
Try it hands-on
  • Test webhooks in a dev cluster first. Use self-signed certs for experiments and ensure your webhook serves TLS.
  • Log AdmissionReview requests and responses in your webhook to debug matching and patch application.
Links and references That concludes this lesson. Implement and register a webhook in a non-production environment to gain practical experience with AdmissionReview requests, TLS configuration, and webhook ordering.

Watch Video

Practice Lab