- Which combination is correct for mutating vs validating admission controllers?
NamespaceAutoProvisionis a mutating admission controller — it alters cluster state by automatically creating namespaces.NamespaceExists(ornamespace-exists) is a validating admission controller — it enforces checks to accept or reject requests based on namespace existence.
- 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-demonamespace - 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 thewebhook-demo namespace:
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:
Deploy the webhook server and Service
Apply the Deployment and Service manifests:Inspect and apply the MutatingWebhookConfiguration
The providedwebhook-configuration.yaml targets CREATE operations on pods. Key excerpt:
Warning:
admissionregistration.k8s.io/v1beta1 MutatingWebhookConfiguration is deprecated in v1.16+ and removed in v1.22+. Use admissionregistration.k8s.io/v1 where possible.What the demo mutating webhook does
The demo mutating webhook implements simple, common behavior around Pod security:- If a Pod has no
securityContextset, the webhook mutates the Pod to set:runAsNonRoot: truerunAsUser: 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: truewhilerunAsUser: 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:Test 2 — Pod with an explicit override allowing root
pod-with-override.yaml:runAsNonRoot: false setting.
Test 3 — Pod with conflicting security context (should be rejected)
pod-with-conflict.yaml:- The API server or validating admission controller rejects the Pod because
runAsNonRoot: trueconflicts withrunAsUser: 0.
Summary
- Mutating webhooks run before validating webhooks.
- Mutating webhooks are suitable to set secure defaults (for example,
runAsNonRoot: trueandrunAsUser: 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: truewithrunAsUser: 0).
Links and References
- Kubernetes Admission Controllers
- MutatingWebhookConfiguration API
- ValidatingWebhookConfiguration API