Skip to main content
This lesson walks through a hands-on lab for Kubernetes mutating and validating admission controllers. It explains the concepts, shows the invocation order, and guides you through deploying a demo webhook server, creating TLS secrets, applying webhook configuration, and testing Pod admission behavior with three example manifests.
  1. Which combination is correct for mutating vs validating admission controllers?
  • NamespaceAutoProvision is a mutating admission controller — it alters cluster state by automatically creating namespaces.
  • NamespaceExists (or namespace-exists) is a validating admission controller — it enforces checks to accept or reject requests based on namespace existence.
Therefore, the correct pairing is: NamespaceAutoProvision = mutating, NamespaceExists = validating.
  1. What is the invocation order for admission controllers?
  • Mutating webhooks run first; once they mutate the object, validating webhooks run against the (potentially mutated) object.
Mutating webhooks can modify requests (for example, set secure defaults). Those modifications are then evaluated by validating webhooks, which may accept or reject the request.

Lab steps (overview)

  • Create the webhook-demo namespace
  • Create a TLS Secret for the webhook server
  • Deploy the webhook Deployment and Service
  • Apply the MutatingWebhookConfiguration
  • Test three Pod manifests to observe mutation, override behavior, and validation rejection

Create the namespace

Create the webhook-demo namespace:
Verify it exists:
Example output:

Create the TLS Secret for the webhook server

A TLS certificate and key were generated beforehand at /root/keys/webhook-server-tls.crt and /root/keys/webhook-server-tls.key. Create a TLS secret in the webhook-demo namespace:
Expected output:

Deploy the webhook server and Service

Apply the Deployment and Service manifests:
Example output:
Files in the working directory (context):

Inspect and apply the MutatingWebhookConfiguration

The provided webhook-configuration.yaml targets CREATE operations on pods. Key excerpt:
Apply the webhook configuration:
When applying, you may see deprecation warnings if an older API version is used.
Warning: admissionregistration.k8s.io/v1beta1 MutatingWebhookConfiguration is deprecated in v1.16+ and removed in v1.22+. Use admissionregistration.k8s.io/v1 where possible.
Example output when applying:

What the demo mutating webhook does

The demo mutating webhook implements simple, common behavior around Pod security:
  • If a Pod has no securityContext set, the webhook mutates the Pod to set:
    • runAsNonRoot: true
    • runAsUser: 1234
  • If a Pod explicitly sets runAsNonRoot: false, the webhook respects that explicit decision and does not override it.
  • A validating admission controller (deployed in the lab) enforces consistency and will reject Pods with conflicting settings (for example, runAsNonRoot: true while runAsUser: 0).

Tests

Below are the three test cases you can apply to verify mutation, override behavior, and validation.

Test 1 — Pod with no security context (defaults applied by the mutating webhook)

pod-with-defaults.yaml:
Apply it:
Inspect the Pod to see the mutated security context:
You should find the webhook-applied defaults under the Pod (or container) securityContext:

Test 2 — Pod with an explicit override allowing root

pod-with-override.yaml:
Apply it:
The Pod will be created, and the mutating webhook will not override the explicit runAsNonRoot: false setting.

Test 3 — Pod with conflicting security context (should be rejected)

pod-with-conflict.yaml:
Apply it:
Expected result:
  • The API server or validating admission controller rejects the Pod because runAsNonRoot: true conflicts with runAsUser: 0.
Example rejection message:

Summary

  • Mutating webhooks run before validating webhooks.
  • Mutating webhooks are suitable to set secure defaults (for example, runAsNonRoot: true and runAsUser: 1234).
  • Explicit Pod-level settings (e.g., runAsNonRoot: false) prevent the mutating webhook from overriding an explicit decision.
  • Validating admission controllers ensure configuration consistency and will reject conflicting security settings (e.g., runAsNonRoot: true with runAsUser: 0).

Watch Video