Skip to content

Validate with Admission Webhooks

Kubernetes admission webhooks enforce policies at deploy time by intercepting API requests. Policy engines like Kyverno and OPA Gatekeeper use admission webhooks to validate resources against policies or constraints defined in the cluster.

ConfigHub can call these same webhooks to validate configuration before it is deployed, using external functions that send AdmissionReview requests to a running webhook server. This gives you a single set of policies for both admission control and pre-deployment validation.

The admission-webhook example is a function bot that serves two such functions, vet-kyverno-server and vet-opa-gatekeeper. Its webhook package is the shared code for building your own, for another policy engine.

The functions

vet-kyverno-server

The vet-kyverno-server function validates Kubernetes resources against policies deployed in a Kyverno cluster. It supports both ClusterPolicy and ValidatingPolicy resources.

The function takes no parameters. It validates all resources in the unit against all matching Kyverno policies:

cub function vet vet-kyverno-server --space $SPACE --unit my-unit --channel "$SPACE/admission-bot"

A passing result looks like:

Function(s) succeeded on unit ...
OUTPUT
------
true 0

A failing result includes the policy and rule that failed:

Function(s) succeeded on unit ...
OUTPUT
------
false 0 policy "require-labels" rule "validation": The label 'team' is required.
Attributes:
<nil>   default/bad-pod v1/Pod

vet-opa-gatekeeper

The vet-opa-gatekeeper function works the same way, but validates against OPA Gatekeeper constraint templates and constraints:

cub function vet vet-opa-gatekeeper --space $SPACE --unit my-unit --channel "$SPACE/admission-bot"

A failing result includes the constraint name:

Function(s) succeeded on unit ...
OUTPUT
------
false 0 constraint "require-team-label": Missing required labels: {"team"}
Attributes:
<nil>   default/bad-deploy apps/v1/Deployment

Configuration

The bot declares both functions on its Channel and serves each one whose webhook URL it is given. Invoking a function whose URL is not set fails with a message naming the missing variable. The bot also needs access to the Kubernetes API, to discover which webhook configurations exist.

Variable Description
KYVERNO_URL Base URL of the Kyverno webhook (e.g., https://kyverno-svc.kyverno.svc:443); serves vet-kyverno-server
KYVERNO_CA_CERT_PATH Path to a CA certificate file for TLS verification
KYVERNO_SKIP_TLS_VERIFY Set to true to skip TLS certificate verification (development only)
GATEKEEPER_URL Base URL of the Gatekeeper webhook (e.g., https://gatekeeper-webhook-service.gatekeeper-system.svc:443); serves vet-opa-gatekeeper
GATEKEEPER_CA_CERT_PATH Path to a CA certificate file for TLS verification
GATEKEEPER_SKIP_TLS_VERIFY Set to true to skip TLS certificate verification (development only)

Running the bot

In a cluster

The bot runs in the same cluster as the policy engine. This is the recommended approach for production.

Build and push the image from the example's Dockerfile. The bot needs permission to list ValidatingWebhookConfigurations, which is how it finds the webhooks to call:

kubectl create clusterrole webhook-reader \
  --verb=list,watch --resource=validatingwebhookconfigurations.admissionregistration.k8s.io
kubectl create clusterrolebinding admission-bot-webhook-reader \
  --clusterrole=webhook-reader \
  --group="system:serviceaccounts:admission-bot"

Then run it as described in external functions: a service account with Claim on a Channel, the functions declared on the Channel, and a Deployment holding the key. The examples' deploy-bot.sh does it in one command:

deploy-bot.sh $SPACE admission-bot admission-bot my-registry/admission-webhook-bot:latest \
  KYVERNO_URL=https://kyverno-svc.kyverno.svc:443 \
  KYVERNO_SKIP_TLS_VERIFY=true

The bot then serves vet-kyverno-server on the Channel $SPACE/admission-bot. For complete demos on a kind cluster, see demo-kyverno.sh and demo-gatekeeper.sh.

Out of the cluster, for development

Use kubectl port-forward to reach the webhook service, then run the bot locally, as yourself:

kubectl -n kyverno port-forward svc/kyverno-svc 8443:443 &

cub channel create --space $SPACE admission-bot
KYVERNO_URL=https://localhost:8443 KYVERNO_SKIP_TLS_VERIFY=true ./admission-webhook $SPACE/admission-bot

The bot claims the Channel and declares its functions. The kubeconfig from your default context is used for webhook discovery.

Using validation in Triggers

Once the bot holds its Channel, you can use the validation function in Triggers to validate configuration automatically. Create a Mutation trigger so that units are validated whenever they are modified:

cub trigger create --space $SPACE \
  --channel "$SPACE/admission-bot" \
  validate-kyverno Mutation Kubernetes/YAML vet-kyverno-server

Any Kubernetes/YAML unit in the space will be validated against Kyverno policies when its configuration is changed. If validation fails, the change is flagged. While the bot does not hold its Channel, the Unit waits for the Trigger to run.

See validating configuration and enforcing policies for more on Triggers and validation errors.

Reference