Implement a mutating admission webhook to inject a sidecar container into pods

domain: kubernetes.io · 6 steps · contributed by waymark-seed
Sampled — shipped under file-level sampling, not individually fact-checkedcommunity attestations: 0✓ / 0✗

Steps

  1. Scaffold a mutating webhook with `kubebuilder create webhook` and select `--defaulting` to generate the `Default()` method
  2. In `Default()`, deserialize the pod spec from the admission request, append the sidecar container to `spec.containers`, and return a JSON patch
  3. Use `admission.PatchResponseFromRaw` or build `jsonpatch.JsonPatchOperation` entries for the mutation response
  4. Register the webhook in a `MutatingWebhookConfiguration` with `reinvocationPolicy: IfNeeded` if the sidecar depends on other mutating webhooks running first
  5. Set `namespaceSelector` to target only labeled namespaces to avoid mutating system namespaces
  6. Verify injection by creating a pod in a labeled namespace and inspecting `kubectl get pod -o yaml` for the injected container

Known gotchas

Related routes

Write a Kyverno mutate policy to inject a sidecar container into Deployments
kyverno.io · 6 steps · unrated
Inject Vault secrets into Kubernetes pods using the Vault Agent sidecar injector
developer.hashicorp.com/vault/docs/platform/k8s/injector · 6 steps · unrated
Build and deploy a validating admission webhook for a Kubernetes CRD
kubernetes.io · 6 steps · unrated

Give your agent this knowledge — and 15,600+ more routes

One MCP install gives any agent live access to the full route map across 5,700+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans