Sub topic 1
How Kubernetes admission control works
Prerequisites
This chapter assumes you know basic Kubernetes objects (Pods, Deployments, Namespaces, labels). It also assumes you can read YAML and use kubectl apply and kubectl get. You should know that every change to the cluster goes through the API server. Custom resources are not needed yet.
Where admission fits
Admission control is a step inside the API server. It runs after a request is authenticated and authorized, and before the object is saved to etcd. Authorization answers "may this user create Pods?" Admission answers "is this particular Pod acceptable?"
Admission runs in two phases, in this order:
- Mutating admission. Controllers can change the object, for example by adding a label or a default value.
- Validating admission. Controllers can only accept or reject the final object.
Between the two phases the API server validates the object against its schema. Validating controllers therefore see the result of all mutations.
Webhooks
Built-in admission plugins cover fixed behavior, such as applying a default StorageClass. To add your own logic, Kubernetes provides two dynamic admission webhooks. A webhook is an HTTPS service that the API server calls with the request. Both Kyverno and Gatekeeper install one.
- A
MutatingWebhookConfigurationtells the API server which requests to send to a mutating service. The service replies with a patch. - A
ValidatingWebhookConfigurationdoes the same for validation. The service replies with allowed or denied, plus a message.
The API server sends an AdmissionReview object containing the operation (CREATE, UPDATE, DELETE), the new object, and for updates the old object. The reply echoes the request uid and sets allowed.
The rules in a webhook configuration decide which requests are sent. You can match on API groups, resources and operations, and you can filter by namespace with a namespaceSelector. Requests that do not match are never sent to the engine.
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
metadata:
name: example-policy-webhook
webhooks:
- name: validate.example.com
admissionReviewVersions: ["v1"]
sideEffects: None
failurePolicy: Fail
timeoutSeconds: 10
rules:
- apiGroups: [""]
apiVersions: ["v1"]
operations: ["CREATE", "UPDATE"]
resources: ["pods"]
clientConfig:
service:
namespace: policy-system
name: policy-webhook
path: /validatePolicy engines generate configurations like this for you. Knowing the fields helps when you debug.
Common errors and gotchas
- Failure policy.
failurePolicy: Failrejects requests when the webhook is unreachable or times out.Ignorelets them through.Failis safer for security but can block the cluster if the engine is down. - Self-blocking. If the webhook rejects requests for its own Pods or for system namespaces, you may be unable to repair it. Exclude critical namespaces such as
kube-system. - Timeouts. Every matching request waits for the webhook. A slow policy slows every
kubectl apply. - Existing objects. Admission only sees new requests. Resources created before a policy existed are not affected until they change. Audit features, covered later, scan them.
- Order. Mutations from one webhook can change what another sees, so do not assume a validation sees the original manifest.
Exercise
A policy engine's Pod is crashing and failurePolicy is Fail on a webhook matching all Pods in all namespaces. What happens when you try to create a new Pod, and how would you have reduced the risk?
Show answer
The API server cannot reach the webhook, so it rejects the request, including Pods needed to restore the engine. You can reduce the risk by excluding the engine's own namespace and system namespaces with a namespaceSelector, running multiple replicas of the webhook, or using Ignore for low-risk policies.