Estendere l'API server con gli admission webhook
Intercetti e convalidi o modifichi al volo le richieste all'API di Kubernetes usando admission webhook validating e mutating, per applicare le policy del cluster.
Estendere l'API server con gli admission webhook è una lezione Docker & Kubernetes for Developers gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Docker & Kubernetes for Developers, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Docker & Kubernetes for Developers include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
What Is Admission Control
After authentication and authorization, requests pass through admission controllers that can validate or modify objects before they are persisted.
Two Kinds of Webhooks
Dynamic admission uses webhooks: mutating webhooks change objects, and validating webhooks accept or reject them.
Where They Sit in the Pipeline
The API server runs mutating webhooks first, then validating webhooks, so a mutation can add defaults before validation checks the final object.
A Use Case
Examples: inject a sidecar (mutating), enforce that every Pod sets resource limits (validating), or block images from untrusted registries.
The MutatingWebhookConfiguration
This resource registers your webhook endpoint with the API server.
apiVersion: admissionregistration.k8s.io/v1
kind: MutatingWebhookConfiguration
metadata:
name: add-defaults
webhooks:
- name: defaults.example.com
clientConfig:
service:
name: webhook-svc
namespace: default
path: /mutate
caBundle: <base64-ca>
rules:
- operations: ["CREATE"]
apiGroups: [""]
apiVersions: ["v1"]
resources: ["pods"]The AdmissionReview Payload
The API server sends an AdmissionReview JSON to your webhook and expects an AdmissionReview response with allowed true/false, and for mutation a JSONPatch.
Returning a JSON Patch
A mutating webhook returns a base64-encoded JSONPatch describing the changes to apply.
{"response":{"allowed":true,"patchType":"JSONPatch","patch":"<base64-patch>"}}failurePolicy Matters
failurePolicy: Fail blocks requests if the webhook is down, which is safe but can lock up the cluster; Ignore lets requests through, which is risky for security policies.
Scoping With Selectors
Use namespaceSelector and objectSelector to limit which requests hit your webhook, avoiding overhead and accidental scope creep.
Policy Engines as an Alternative
Instead of writing webhook servers, tools like OPA Gatekeeper and Kyverno provide declarative policies on top of the same admission mechanism.
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: require-limits
spec:
validationFailureAction: Enforce
rules:
- name: check-limits
match:
any:
- resources:
kinds: [Pod]
validate:
message: "CPU and memory limits are required"
pattern:
spec:
containers:
- resources:
limits:
memory: "?*"TLS Is Required
Webhook endpoints must serve HTTPS with a certificate trusted via the caBundle, since the API server only calls webhooks over TLS.
Quick Check
Test what you have learned.
Recap
You learned how admission webhooks extend the API server: mutating webhooks change objects and validating webhooks enforce policy, the AdmissionReview flow, failurePolicy and selectors, TLS requirements, and policy engines like Kyverno and Gatekeeper.
Impara Docker & Kubernetes for Developers con un tutor IA — gratis
Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.
- Corsi
- 12
- Lezioni
- 48
Domande Frequenti
La lezione «Estendere l'API server con gli admission webhook» è gratuita?
Sì — il testo completo di «Estendere l'API server con gli admission webhook» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Docker & Kubernetes for Developers, passa a CoddyKit PRO. Il corso Docker & Kubernetes for Developers include 4 lezioni in totale.
Cosa imparerò in «Estendere l'API server con gli admission webhook»?
Intercetti e convalidi o modifichi al volo le richieste all'API di Kubernetes usando admission webhook validating e mutating, per applicare le policy del cluster. Eserciti Docker & Kubernetes for Developers con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Docker & Kubernetes for Developers?
Non è richiesta alcuna esperienza precedente. Docker & Kubernetes for Developers su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Estendere l'API server con gli admission webhook»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Docker & Kubernetes for Developers?
Sì. Ogni lezione Docker & Kubernetes for Developers include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Custom Resource Definition (CRD)
- Il pattern Operator in Kubernetes
- Serverless con Kubernetes (Knative)
- Estendere l'API server con gli admission webhook