In Kubernetes, an admission controller is a plug-in component that intercepts requests to the Kubernetes API server prior to the persistence of the object, but after the request is authenticated and authorized.
Admission controllers are responsible for making decisions about whether to accept or reject requests based on custom logic and policies.
Request Flow:
When a user or system sends a request to the Kubernetes API server to create, update, or delete a resource (such as a Pod, Deployment, or Service), the request goes through a series of checks and processes.
Admission controllers are part of this request flow, allowing customization and enforcement of policies before the request is persisted in the cluster.
Kubernetes supports several built-in admission controllers, each serving a specific purpose. Some common ones include:
NamespaceLifecycle: Enforces policies related to the lifecycle of namespaces.
ResourceQuota: Enforces resource constraints on namespaces.
PodSecurityPolicy: Enforces policies related to the security context of Pods.
MutatingAdmissionWebhook: Allows external webhooks to modify objects before they are persisted.
ValidatingAdmissionWebhook: Allows external webhooks to validate and potentially reject objects.
To view enabled admission controllers:
kube-apiserver -h | grep enable-admission-plugins
To enable an admission controller, add it onto the kube-apiserver.service.
## /etc/kubernetes/manifests/kube-apiserver.yaml
spec:
containers:
- command:
- kube-apiserver
- ...
- --enable-admission-plugins=NodeRestriction,NamespaceAutoProvision
To disable an admission controller, add the disable parameter:
## /etc/kubernetes/manifests/kube-apiserver.yaml
spec:
containers:
- command:
- kube-apiserver
- ...
- --enable-admission-plugins=NodeRestriction,NamespaceAutoProvision
- --disable-admission-plugins=DefaultStorageClass
Validating and mutating admission controllers are two types of admission controllers in Kubernetes, and they serve different purposes in the admission control process.
Validating admission controllers are responsible for validating and potentially rejecting requests to create or modify resources based on predefined policies.
When They Are Invoked:
Examples of Use Cases:
Mutating admission controllers are responsible for modifying requests before they are persisted in the cluster. They can alter the content of the object being created or modified.
When They Are Invoked:
Examples of Use Cases:
Mutating Admission Controllers are generally invoked first, followed by Validating Admission Controllers. It is done this way so that any change done by the Mutating Admission Controllers can be considere during the validation process.
To support external admission controllers, we can leverage two available admission controllers:
These webhooks can be configured to point to aan Admission Webhook Server where our custom admission webhook service is running with our own custom code. The server can be running within the Kubernetes cluster or an external server.
Creating a validating webhook admission controller involves several components:
Sample Validating Webhook Configuration:
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
metadata:
name: my-validating-webhook
webhooks:
- name: validating.pods.example.com
clientConfig:
url: "https://<your-webhook-service>/validate"
caBundle: "<your-ca-bundle>"
rules:
- operations: ["CREATE"]
apiGroups: [""]
apiVersions: ["v1"]
resources: ["pods"]
failurePolicy: Ignore
Sample Mutating Webhook Configuration:
apiVersion: admissionregistration.k8s.io/v1
kind: MutatingWebhookConfiguration
metadata:
name: my-mutating-webhook
webhooks:
- name: mutating.pods.example.com
clientConfig:
url: "https://your-webhook-service/mutate"
caBundle: "<your-ca-bundle>"
rules:
- operations: ["CREATE"]
apiGroups: [""]
apiVersions: ["v1"]
resources: ["pods"]
failurePolicy: Ignore
Validating Admission Controller Logic
Mutating Admission Controller Logic
The external admission controller constructs an AdmissionResponse object based on its logic.
The response includes a decision (allowed or denied) and, in the case of a mutating admission controller, the modified object.
Returning AdmissionResponse to API Server
The external admission controller sends the AdmissionResponse object back to the Kubernetes API server.
The API server receives the AdmissionResponse from the external admission controller.
If the response indicates permission (allowed), the API server continues processing the request, and the object is persisted.
If the response indicates denial, the API server rejects the admission request, and the object is not persisted.