Sub topic 15
Defaulting, validating and conversion webhooks
This chapter assumes you can write a CRD with a structural schema, know the status subresource, and have seen a version list with served and storage flags.
When schema rules are not enough
Structural schema defaults and CEL validation rules cover most needs. Reach for a webhook when the logic needs code or outside data. Examples are a default computed from other fields, a check against another object, or a conversion between versions whose fields differ in shape.
A webhook is an HTTPS endpoint that the API server calls during a request. Your operator process usually hosts it.
Where webhooks run
Defaulting and validating
A mutating webhook returns a patch. Use it for defaults the schema cannot express. A validating webhook only allows or denies, with a message. Validators see the object after defaulting, so they check the final values.
This configuration registers a validating webhook for WebApp:
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
metadata:
name: webapp-validation
annotations:
cert-manager.io/inject-ca-from: default/webhook-cert
webhooks:
- name: vwebapp.example.com
admissionReviewVersions: ["v1"]
sideEffects: None
failurePolicy: Fail
timeoutSeconds: 5
clientConfig:
service:
name: webapp-webhook
namespace: default
path: /validate-example-com-v1-webapp
rules:
- apiGroups: ["example.com"]
apiVersions: ["v1"]
operations: ["CREATE", "UPDATE"]
resources: ["webapps"]The API server requires TLS and a trusted CA bundle in clientConfig. The annotation above asks cert-manager to inject it.
Conversion webhooks
When a CRD serves more than one version, the API server converts objects between them. With the default None strategy it only changes apiVersion. If fields were renamed or restructured, switch to a webhook in the CRD:
spec:
conversion:
strategy: Webhook
webhook:
conversionReviewVersions: ["v1"]
clientConfig:
service:
name: webapp-webhook
namespace: default
path: /convertA conversion must be lossless in both directions and must not change the object's name or namespace. Keep unknown data in an annotation if the older version has no place for it.
Common errors
- Deadlock. A webhook that guards a resource its own Pod depends on can block its own startup. Exclude the webhook's namespace with a
namespaceSelector. failurePolicy: Failwith a down operator. All matching writes are rejected. UseIgnoreonly for non-critical checks, and run more than one replica.- Side effects. Set
sideEffects: Noneand keep handlers free of writes. Dry-run requests call webhooks too. - Slow handlers. Requests wait for the webhook. Keep timeouts short.
- Broken certificates. An expired or missing CA bundle makes every call fail. Automate rotation.
Exercise
Your validating webhook has failurePolicy: Fail, and the operator Deployment that serves it is scaled to zero. What happens to a kubectl apply of a WebApp, and how can you reduce this risk?
Show answer
The API server cannot reach the webhook, so it rejects the write with an error. To reduce the risk, run several replicas, exclude the operator's own namespace with a namespaceSelector, and move simple checks into CEL rules, which need no server.