Admission Controllers Overview
Admission Controllers perform crucial tasks during the API request lifecycle:- Mutating Admission Controllers: Modify the object (mutate the request) before it is created.
- Validating Admission Controllers: Inspect a request to decide whether it should be allowed or rejected.
- Dual-Purpose Controllers: Some controllers support both mutating and validating functions.
If any admission controller rejects the request, an error message is returned and the request is not processed further.
Namespace and Storage Class Admission Controllers
Namespace Existence
The namespace existence (or namespace lifecycle) admission controller verifies if a namespace already exists. If it does not, the request is rejected.Default Storage Class Plugin
The default storage class admission controller is enabled by default. When you create a PersistentVolumeClaim (PVC) without specifying a storage class, the request is authenticated, authorized, and then passed through the admission controller. This controller mutates the request by adding the default storage class if none is provided. For example, consider the following PVC creation request:Inspecting Mutated Objects
When you inspect a PVC that has been modified by an admission controller, you can use thekubectl describe command:
External Admission Webhooks
While built-in admission controllers are compiled with Kubernetes, you can implement custom logic using external admission webhooks. Two specific controllers support webhooks:- Mutating Admission Webhook
- Validating Admission Webhook
How Admission Webhooks Work
- Webhook Configuration: After passing through all built-in admission controllers, requests are forwarded to the webhook server.
- Admission Review Request: The API server sends a JSON-formatted admission review object containing request details.
- Webhook Response: The webhook processes the request and sends back an AdmissionReview response.
Deploying an Admission Webhook Server
To start using webhooks, deploy your custom webhook server. This server handles your custom logic for mutating and/or validating requests. You can implement the server in any language that supports processing JSON admission review objects.Example: Go Webhook Server
Below is a sample snippet in Go that demonstrates setting up a webhook server:Example: Python Webhook Server
Below is pseudocode for a simple webhook server using Python and Flask. This example includes both a validating and a mutating webhook endpoint.The critical aspect of setting up webhooks is ensuring that your server processes the admission review objects and responds with a valid JSON object following the mutate and validate APIs.
Configuring the Webhook in Kubernetes
Once your webhook server is deployed, configure Kubernetes to route requests to your service by creating a webhook configuration object. For instance, the following example shows a [ValidatingWebhookConfiguration] that validates pod creation:- API Server Invocation: The API server calls the webhook service when a pod is created.
- Security: TLS is enforced by providing a valid certificate bundle.
- Rules: The
rulessection precisely specifies which operations (in this case, pod creation) trigger the webhook.
Final Thoughts
Admission controllers are a powerful mechanism for enforcing policies and modifying Kubernetes objects during their lifecycle. By integrating custom admission webhooks, you can extend Kubernetes functionality to meet your specific needs. For further practice, consider configuring and testing these webhooks in your environment to better understand their impact on Kubernetes operations.