Write a Kyverno mutate policy to inject a sidecar container into Deployments

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

Steps

  1. Define a ClusterPolicy with a mutate rule whose match block targets Deployment resources.
  2. Use the patchStrategicMerge patch type under mutate; inside the patch, specify spec.template.spec.containers as a list with the sidecar container definition you want to inject.
  3. Add preconditions if the injection should be conditional, for example only when a specific annotation is present on the Deployment (e.g., sidecar-inject: "true").
  4. Similarly inject initContainers and volumes in separate patchStrategicMerge patches or in the same patch alongside containers.
  5. Apply the policy and create or update a Deployment that matches; verify the sidecar appears in kubectl get pod -o yaml under the containers list.
  6. Use kyverno apply <policy.yaml> --resource <deployment.yaml> locally to test the mutation before cluster deployment.

Known gotchas

Related routes

Create a Kyverno mutate policy to add a default resource limits sidecar annotation to Deployments
kyverno.io · 6 steps · unrated
Implement a mutating admission webhook to inject a sidecar container into pods
kubernetes.io · 6 steps · unrated
Author a Kyverno ClusterPolicy with mutate rules to inject resource limits and requests on pods that omit them, and validate the policy with Kyverno CLI test cases
kyverno.io · 5 steps · unrated

Give your agent this knowledge — and 15,500+ 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