Skip to the content.

Deep dive admission controllers

What are admission controllers

validating-webhook-flow

Ref: k8s-blog

How to write basic validation webhook from beginning

Experiments

Missions

Prerequisites

Write a webhook server

The code is modified based on link. It has been forked in to github.

docker build --no-cache -f Dockerfile -t sample-webhook-server:v1 --rm=true .

kind load docker-image sample-webhook-server:v1

Generate certs and keys

Note: You have to change the "/CN=sample-webhook-server.webhook.svc" in genkeys.sh to be "/CN=<service-name>.<namespace>.svc"

cd $GOPATH/src/github.com/danniel1205/sample-webhook-server/
mkdir -p keys
./hacks/genkeys.sh keys

tree keys
keys
├── ca.crt
├── ca.key
├── ca.srl
├── webhook-server-tls.crt
└── webhook-server-tls.key

Create a namespace

kubectl create namespace webhook

Create secrets from the keys generated

kubectl create secret tls webhook-tls --key=./keys/webhook-server-tls.key --cert=./keys/webhook-server-tls.crt -n webhook

Create the webhook service

kubectl apply -f $GOPATH/src/github.com/danniel1205/sample-webhook-server/deploy/01-deployment.yaml
kubectl get svc,deployment,pod -n webhook
NAME                            TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)   AGE
service/sample-webhook-server   ClusterIP   10.99.16.47   <none>        443/TCP   2m33s

NAME                                    READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/sample-webhook-server   1/1     1            1           2m33s

NAME                                         READY   STATUS    RESTARTS   AGE
pod/sample-webhook-server-6449948fcb-d9pq9   1/1     Running   0          25s

Create ValidatingWebhookConfiguration

Note: Update the CABundle in 02-validating-webhook-config.yaml to be base64 encoded of keys/ca.crt

kubectl apply -f $GOPATH/src/github.com/danniel1205/sample-webhook-server/deploy/02-validating-webhook-config.yaml

Try to create the test pod

kubectl apply -f $GOPATH/src/github.com/danniel1205/sample-webhook-server/deploy/test-validating-pod.yaml

Error from server: error when creating "deploy/test-pod.yaml": admission webhook "sample-webhook-server.example.com" denied the request: the namespace must be specified to create pod

Create MutatingWebhookConfiguration

Note: Update the CABundle in 03-mutating-webhook-config.yaml to be base64 encoded of keys/ca.crt

kubectl apply -f $GOPATH/src/github.com/danniel1205/sample-webhook-server/deploy/03-mutating-webhook-config.yaml

Q&A