Back to articles

Admission webhooks: how they fail, and when a ValidatingAdmissionPolicy is the safer choice

An admission webhook puts a network call to your own Deployment in the write path of the API server, and its configuration decides whether an outage of that Deployment blocks the cluster or silently skips the policy. This article covers how webhooks fail, the deadlock where a webhook blocks its own recovery, and which checks can move in-process with ValidatingAdmissionPolicy and MutatingAdmissionPolicy.

An admission webhook puts a network call to a process you run in the path of every matching write to the Kubernetes API. When that process is slow, unreachable or holding an expired certificate, the API server has to decide what to do with the request it was about to admit, and the default answer is to reject it. Policy engines, service meshes, certificate tooling and operators all install such webhooks, so it pays to know what each one in your cluster matches and what happens when it is down.

Who this is for: platform engineers who install or write admission webhooks, and anyone who wants to know how one can stop pod creation cluster-wide.

What you walk away with: where webhooks sit in the admission sequence, the settings that decide how a webhook fails, the outage pattern to design against, and which checks can move into the API server as a ValidatingAdmissionPolicy or MutatingAdmissionPolicy.

Versions: Kubernetes v1.37, admissionregistration.k8s.io/v1 throughout.

Where webhooks sit in a request

A write that has passed authentication and authorisation goes through admission in two phases (the full path is in the apply-to-pod article):

  1. Mutating admission. Built-in mutating plugins, mutating admission policies and mutating webhooks may change the object.
  2. Validation of the resulting object by the API server.
  3. Validating admission. Built-in validating plugins, validating admission policies and validating webhooks may reject the object. They cannot change it.
  4. The object is written to etcd.
Mutating webhook Validating webhook
Can change the object Yes, by returning a patch No
Sees the final object No. Later mutators can still change it Yes
How the API server calls them In sequence In parallel

The documentation draws the consequence: a webhook that must see the final state of an object to enforce a policy has to be a validating webhook.

Ordering and reinvocation

Mutating webhooks are called one after another, but the documentation says not to rely on the order: it is not consistent, and other webhooks can still modify what yours produced.

The API server may also run mutators more than once. If a mutating webhook changes the object, the built-in mutating plugins are run again, and a webhook with reinvocationPolicy: IfNeeded (the default is Never) may be called again too, with no guarantee about how often.

So a mutating webhook must be idempotent, able to process an object it has already modified, and a mutation that matters should be checked by a validating step, not assumed.

The settings that decide how a webhook fails

failurePolicy

failurePolicy says what the API server does when it cannot get a usable answer from the webhook: network errors, connection failures, timeouts, a non-2xx response, a malformed response.

It does not apply to an explicit denial: a webhook that is reached and answers allowed: false always denies the request.

Neither choice is free:

Fail Ignore
Webhook down Matching writes are rejected Matching writes pass unchecked
Risk Availability: an outage of the webhook becomes an outage of part of the API Integrity: the policy has a hole exactly when nobody is looking
Fits Security controls that must not be bypassed Defaults and conveniences

The good-practices guide recommends letting mutating webhooks fail open with Ignore and enforcing the outcome in a validating step, so that an outage of the mutating webhook does not block compliant objects. For a validating webhook that is the security control, Ignore means nothing is checked whenever it cannot be reached.

timeoutSeconds

timeoutSeconds is how long the API server waits for a response. The default is 10 seconds and the value must be between 1 and 30. On expiry, failurePolicy decides.

Mutating webhooks run in sequence and each call gets its own timeout, so three of them timing out with Ignore add half a minute to one request. The request as a whole has a deadline too: in the release-1.37 source a create gets at most 34 seconds, and when that passes the request fails with a timeout even though every webhook is set to fail open. The documentation asks for a small timeout and says webhooks should typically answer in milliseconds. Set it to a few seconds at most.

Narrowing what is matched

Every request a webhook does not receive is a request it cannot break. Four mechanisms narrow the match:

matchPolicy defaults to Equivalent, which means a request made through another API version of the same resource is converted and still sent to the webhook.

From v1.37 the API server by default no longer calls webhooks for the virtual authentication and authorisation resources such as TokenReview and SubjectAccessReview (feature gate ExcludeAdmissionWebhookVirtualResources, Beta, on). A failing webhook matching those could lock a cluster out of its own authorisation path.

Side effects and dry-run

A webhook declares sideEffects as None or NoneOnDryRun. kubectl apply --dry-run=server and kubectl diff send real requests through admission with dryRun: true. With either value the webhook still receives them and must not act on them.

Admission is not the end of the request either: the object can still be rejected later or fail to be stored, so a webhook with side effects needs a reconciliation loop that cleans up.

Network path, TLS and CA rotation

The call is an HTTPS request from the API server to the webhook's Service, so the control plane must be able to reach the webhook's pods. A firewall rule or NetworkPolicy that blocks that path makes the webhook unreachable while its pods are Ready, and failurePolicy applies.

The API server verifies the server certificate against clientConfig.caBundle. For a Service reference, the certificate must be valid for <service>.<namespace>.svc. An expired serving certificate, or a caBundle that no longer matches the certificate after a rotation, is a failure to call the webhook, so failurePolicy applies. Nothing was deployed, yet every matching write is rejected with an x509 error. Whatever issues the certificate has to update the serving secret and the caBundle together, and the expiry belongs in monitoring.

The classic outage

The pattern has several variants and one shape: a webhook with failurePolicy: Fail matches the objects needed to bring the webhook itself back.

  1. A webhook matches CREATE on pods in all namespaces, with failurePolicy: Fail.
  2. Its backend runs as one or two replicas, and a node drain, a node failure or an upgrade takes them down.
  3. The ReplicaSet controller tries to create replacement pods. The API server calls the webhook to admit them. Nothing answers. The request is rejected.
  4. The webhook cannot return because its own pods cannot be created, and neither can any other pod the webhook matches. Running pods keep running; nothing new starts.

A cluster upgrade is the worst moment for it, because every node is drained in turn; see upgrading without downtime.

The documentation describes two related loops: two webhooks that each match the other's pods, and a webhook that matches an add-on it depends on, such as the network plugin.

How it looks

The pods never exist, so kubectl get pods shows nothing. The evidence is on the controller that tried to create them:

kubectl describe replicaset <name>
kubectl get events -A --field-selector reason=FailedCreate

The FailedCreate event's message is Error creating: followed by the API server's error, which names the webhook and ends with the transport error (no endpoints available, connection refused, a timeout, an x509 failure):

Internal error occurred: failed calling webhook "<webhook name>": failed to call webhook: Post "https://<service>.<namespace>.svc:443/<path>?timeout=10s": <transport error>

To see what is installed and how each webhook fails:

kubectl get validatingwebhookconfigurations,mutatingwebhookconfigurations

kubectl get validatingwebhookconfigurations -o jsonpath='{range .items[*]}{range .webhooks[*]}{.name}{"\t"}{.failurePolicy}{"\t"}{.timeoutSeconds}{"s\t"}{.namespaceSelector}{"\n"}{end}{end}'

Run the second command with mutatingwebhookconfigurations as well. Fail, a long timeout and an empty selector together make a candidate for this outage. The metric apiserver_admission_webhook_rejection_count separates "the webhook could not be called" (error_type="calling_webhook_error") from a deliberate denial (no_error).

The way out of a live deadlock is to take the webhook out of the path: delete its configuration or patch failurePolicy to Ignore, let the backend start, then restore it. A broken webhook cannot block that write, because API-registered webhooks and policies are never invoked for webhook configurations, admission policies or their bindings. The check is off in the meantime, and an operator or a GitOps controller may put the old configuration straight back.

One exception from v1.37: webhooks and policies loaded from static manifest files on the API server (manifest-based admission control, Beta, on by default, active only where staticManifestsDir is configured) are not visible or changeable through the API and have to be fixed in those files.

Designing against it

namespaceSelector:
  matchExpressions:
  - key: kubernetes.io/metadata.name
    operator: NotIn
    values: ["kube-system", "kube-node-lease", "policy-system"]   # policy-system: the webhook's own namespace

Exclusions have a cost: what is excluded is unpoliced. Anyone who can create workloads in an excluded namespace bypasses the webhook, so access to those namespaces has to be tightly held.

ValidatingAdmissionPolicy: the check without the network call

A ValidatingAdmissionPolicy (VAP) is a validation rule written in CEL and evaluated inside the API server. It has been stable since v1.30. There is no backend, no Service and no certificate, so the outage above cannot happen: the policy is available whenever the API server is. The good-practices guide lists it as a way to avoid dependency loops, and states that the project recommends CEL-based admission control when possible.

A policy needs two objects to have any effect: the policy holds the logic, and a ValidatingAdmissionPolicyBinding says where it applies and what a violation does.

apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicy
metadata:
  name: require-team-label.example.com
spec:
  failurePolicy: Fail
  matchConstraints:
    resourceRules:
    - apiGroups:   ["apps"]
      apiVersions: ["v1"]
      operations:  ["CREATE", "UPDATE"]
      resources:   ["deployments"]
  validations:
  - expression: >-
      has(object.metadata.labels) && 'team' in object.metadata.labels
      && object.metadata.labels['team'] != ''
    messageExpression: >-
      'Deployment ' + object.metadata.name + ' must have a non-empty "team" label'
---
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicyBinding
metadata:
  name: require-team-label-binding.example.com
spec:
  policyName: require-team-label.example.com
  validationActions: [Warn, Audit]
  matchResources:
    namespaceSelector:
      matchExpressions:
      - key: kubernetes.io/metadata.name
        operator: NotIn
        values: ["kube-system"]

The binding's validationActions gives a rollout path. Warn returns a warning to the client, Audit records the failure in the audit event, Deny rejects the request. Deny and Warn cannot be combined. Start with [Warn, Audit], read the audit log to see what would have been rejected, then change the binding to [Deny].

The API server type-checks the expressions and reports problems in status.typeChecking, but the documentation notes the check does not change policy behaviour, does not cover CRDs and skips wildcard matches: a clean result is not proof.

failurePolicy exists here too, with a narrower meaning: it covers misconfiguration and CEL expressions that end in an error, not an unreachable backend. An expression that reads a field that may be absent, without a has() guard, errors at runtime; with Fail that error is enforced like a failed validation, according to the binding's validationActions.

What a VAP cannot replace

MutatingAdmissionPolicy

MutatingAdmissionPolicy (MAP) is the mutating counterpart: CEL expressions that produce either an apply configuration, merged with the server-side apply merge strategy, or a JSON patch. It has the same policy-plus-binding structure as a VAP. The feature gate was Beta and off by default in v1.34 and v1.35 and is stable and on by default from v1.36, with the API in admissionregistration.k8s.io/v1.

It suits mutations that need only the request: setting a label, adding a default, injecting a fixed sidecar definition. It does not suit mutations that depend on external state. The documentation points out that if the goal is only to prevent a kind of change, a VAP is the simpler tool.

Two cautions. Clusters on v1.35 or older have it off unless someone enabled the Beta feature, so a policy set for a fleet of mixed versions cannot depend on it yet. And other mutators can still change the result afterwards, so the rule about validating the final state holds.

Choosing

Checks that need only the object, the namespace or a parameter object belong in a ValidatingAdmissionPolicy; fixed labels, defaults and additions in a MutatingAdmissionPolicy (v1.36+); pod security baselines in the built-in Pod Security admission. What needs external data, such as image signatures or another API, stays a webhook: narrow in scope, short in timeout, replicated.

The remaining webhooks should be few, narrow, and known to whoever is on call.

Summary

A webhook fails the way its configuration says: failurePolicy chooses between blocking writes and skipping the check, timeoutSeconds (default 10, maximum 30) sets how long each request waits to find out, and the selectors decide how much of the cluster shares the fate of one backend. The outage to design against is the webhook that guards its own recovery. Checks and, since v1.36, mutations that need only the object in front of them can move into the API server as CEL policies.