Determining the Correct Controller Actions
The first step in this lab is to understand which admission controller action is mutating and which one is validating. Consider the following:- The “namespace auto-provision” admission controller is mutating because it automatically creates and modifies a namespace’s configuration.
- The “namespace exists” admission controller is validating as it solely checks for the existence of a namespace.
- Mutating: Namespace auto-provision
- Validating: Namespace exists
Creating the Namespace
Next, create a namespace calledwebhook-demo by running:
Creating the TLS Secret
For secure webhook communication, you need to create a TLS secret namedwebhook-server-tls in the webhook-demo namespace. This secret uses a certificate and key from specific file paths:
Deploying the Webhook Deployment
Deploy the webhook server next using the provided deployment definition inwebhook-deployment.yaml. First, you can inspect the file with:
Deploying the Webhook Service
A service definition for the webhook server is provided in thewebhook-service.yaml file. Deploy it by running:
Deploying the Mutating Webhook Configuration
Next, configure the mutating webhook with the settings provided inwebhook-configuration.yaml. This file defines a webhook that intercepts pod creation events. For instance, it uses the following rule to match create operations for pods:
Warning: admissionregistration.k8s.io/v1beta1 MutatingWebhookConfiguration is deprecated in v1.16+ and will be unavailable in v1.22+. Please use admissionregistration.k8s.io/v1 MutatingWebhookConfiguration.
runAsNonRoot value is specified, the webhook mutates the pod by setting runAsNonRoot to true and defaults the user ID to 1234. However, if the security context explicitly sets runAsNonRoot to false, the webhook will not override it.
Testing the Default Pod Security Context
First, test the webhook by deploying a pod without a security context. The filepod-with-defaults.yaml contains a pod definition that, by default, would run as root. However, the webhook mutates it:
kubectl describe pod pod-with-defaults) to confirm that the security context has been mutated — runAsNonRoot should be true and runAsUser should be set to 1234.
Deploying a Pod with an Explicit Security Context
Now, deploy a pod that explicitly permits running as root. The filepod-with-override.yaml contains the configuration that sets runAsNonRoot to false, preventing the webhook from applying its defaults:
pod-with-conflict.yaml with:
Deploying a Pod with a Conflicting Security Context
The final test involves deploying a pod with a conflicting security context. In this scenario, the pod configuration requires the container to run as a non-root user by settingrunAsNonRoot to true while explicitly requesting a user ID of 0 (root). Without the webhook, this could lead to a CreateContainerConfigError. With the webhook enabled, the pod creation is rejected:
Conclusion
In this lab, you learned how to:- Distinguish between mutating and validating admission controllers.
- Create a namespace and configure TLS for secure webhook communication.
- Deploy a webhook server along with its service and webhook configurations.
- Test the webhook behavior by deploying pods with default, overridden, and conflicting security contexts.