All-Things-Docker-and-Kubernetes

Admission Controllers

Admission Controllers

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:

Types of Admission Controllers

Kubernetes supports several built-in admission controllers, each serving a specific purpose. Some common ones include:

Enabling Admission Controllers

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

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

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

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 first, then Validating

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.

Webhooks for External Admission Controllers

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.

Configuring Admission Webhook

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

Registration

  1. The external admission controller runs on the admission webhook server.
  2. It registers itself with the Kubernetes API server to receive admission control requests.
  3. The controller provides information about the webhook, including its URL and the type of admission control (validating or mutating) it performs.

AdmissionReview Object

  1. When a user sends a request to create, update, or delete a resource in the Kubernetes API server, the admission control process is triggered.
  2. The API server creates an AdmissionReview object, encapsulating the admission request and an empty response.
  3. The API server sends the AdmissionReview object to the registered external admission controller webhook’s URL.
  4. The external admission controller receives the AdmissionReview object.
  5. Depending on its type (validating or mutating), the controller performs custom logic on the admission request.

Validating Admission Controller Logic

Mutating Admission Controller Logic

AdmissionResponse Object

  1. The external admission controller constructs an AdmissionResponse object based on its logic.

  2. 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

  1. The external admission controller sends the AdmissionResponse object back to the Kubernetes API server.

  2. The API server receives the AdmissionResponse from the external admission controller.

  3. If the response indicates permission (allowed), the API server continues processing the request, and the object is persisted.

  4. If the response indicates denial, the API server rejects the admission request, and the object is not persisted.


Back to first page