- Validating Admission Controllers: These controllers check incoming requests and either allow or deny them based on predefined rules.
- Mutating Admission Controllers: These controllers modify requests by adjusting the object before it is persisted to the cluster.
Validating Admission Controllers
One example of a validating admission controller is the namespace existence (or namespace lifecycle) controller, which ensures that a namespace exists before allowing a request. If the namespace does not exist, the request is rejected. Another example is the default storage class admission controller, which is enabled by default. Consider the following scenario: when you submit a request to create a PersistentVolumeClaim (PVC) without specifying a storage class, the built-in admission controller intervenes by modifying the request to include the preconfigured default storage class.Example: PVC Request without a Storage Class
Mutating admission controllers modify the request (e.g., adding a default storage class), while validating admission controllers only verify the request against set policies. In some cases, controllers perform both actions.
Extending Admission Controllers with Webhooks
In addition to built-in admission controllers, Kubernetes allows you to implement custom logic via two types of external webhooks:- Mutating Admission Webhook
- Validating Admission Webhook
Admission Review JSON Object Example
Below is an example of the JSON object sent to a webhook server:Deploying an Admission Webhook Server
To utilize a custom admission controller, you must deploy your own webhook server, which contains the custom logic for mutation and/or validation. The server can be developed using any programming language that supports HTTPS (TLS is required for secure communication with the Kubernetes API server).Example: Go Webhook Server
Below is an excerpt from a sample admission webhook server written in Go:Example: Python Webhook Server
The following pseudocode demonstrates a simple webhook server in Python using Flask. It includes two routes: one for validation and another for mutation.Configuring the Webhook in Kubernetes
After deploying your webhook server, configure your Kubernetes cluster to use it by creating a webhook configuration object. Below is an example of a ValidatingWebhookConfiguration:- The webhook is triggered during pod creation.
- TLS is used for secure communication, as indicated by the
caBundle. - The API server references the webhook service by its name and namespace when deployed within the cluster.
kind: MutatingWebhookConfiguration.
Once applied, every time a pod is created (or another resource event specified in your rules), the API server calls your webhook server. Depending on whether the response indicates approval or rejection, the API server will allow or reject the request.
Conclusion
This lesson provided an overview of validating and mutating admission controllers in Kubernetes. Key takeaways include:- An understanding of built-in admission controllers and their roles in request validation and mutation.
- How external admission webhooks can extend Kubernetes by allowing custom logic.
- Practical examples of webhook servers implemented in Go and Python.
- Steps to configure your Kubernetes cluster to integrate with these webhooks.
Experimenting in a lab environment is crucial to reinforce these concepts. Continue exploring advanced scenarios to further enhance your Kubernetes security and operational flexibility.
