Kyverno Policy Authoring Prompt
Write, test, and roll out Kyverno policies (validate, mutate, generate) for Kubernetes admission control — enforcing org standards on infra manifests without writing Rego.
- Target user
- Platform engineers enforcing policy-as-code on Kubernetes clusters
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a Kubernetes security engineer who runs Kyverno across multiple clusters in Audit-then-Enforce mode, with every policy backed by a kyverno-test fixture so a policy change can't silently let bad manifests through (or block good ones). I will provide: - The rule(s) to enforce (e.g. require resource limits, disallow :latest, require team label, mutate default securityContext) - Target clusters and namespaces, and any exemptions (kube-system, vendor charts) - Whether we're greenfield or retrofitting onto existing noncompliant workloads Your job: 1. **Pick the policy type** — validate (reject/audit), mutate (inject defaults), or generate (create accompanying resources like NetworkPolicy/ResourceQuota). Choose the least-disruptive type that achieves the goal; prefer mutate-to-fix over validate-to-reject where safe. 2. **Write the ClusterPolicy** — with `match`/`exclude` selectors, `validationFailureAction` (start Audit), `background` scanning, and clear `message` + `deny.conditions`. Use `preconditions` and JMESPath for anything nontrivial. 3. **Rollout discipline** — Audit first, review the PolicyReports to size the blast radius, fix or exempt offenders, THEN flip to Enforce. Show how to scope exemptions narrowly (by namespace label, not by disabling the policy). 4. **Mutate safely** — when injecting defaults (securityContext, labels, imagePullPolicy), make rules idempotent and use `+(...)`/anchors so you don't clobber explicit user values. 5. **Testing** — author a `kyverno-test.yaml` with good and bad resource fixtures asserting pass/fail/skip for every rule. Run `kyverno test` in CI on every policy change. 6. **Exemptions & precedence** — PolicyException resources vs exclude blocks; how to grant a time-boxed exception with an owner and expiry annotation. 7. **Observability** — surface PolicyReports to a dashboard; alert when Enforce-mode denials spike (often a legitimate rollout being blocked). Output as: (a) the ClusterPolicy YAML, (b) the kyverno-test fixtures, (c) the Audit→Enforce rollout plan, (d) an example time-boxed PolicyException, (e) the CI test job. Bias toward: Audit before Enforce, idempotent mutations, every policy covered by a test, narrowly scoped exemptions with expiry.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
OPA Gatekeeper Admission Policy Prompt
Author OPA Gatekeeper ConstraintTemplates and Constraints that enforce cluster policy at admission — with dryrun rollout, audit, and mutation — without breaking existing workloads.
-
Crossplane Managed Resource Import & Observe-Only Prompt
Bring existing brownfield cloud resources under Crossplane management using the crossplane.io/external-name annotation and observe-only policies, without recreating or mutating live infrastructure.
-
Crossplane v2 Namespaced XR Migration Prompt
Plan the migration from Crossplane v1 cluster-scoped composite resources and claims to v2 namespaced XRs, updating XRDs, compositions, and RBAC without orphaning managed resources.
-
Policy-as-Code Rule Authoring from a Governance Requirement Prompt
Translate a written governance or compliance requirement into enforceable policy-as-code rules — choosing the right engine (OPA/Rego, Sentinel, Checkov, cfn-guard, Kyverno) and writing the rule with clear messages, tests, and a graduated rollout.
More Infrastructure as Code prompts & error guides
Browse every Infrastructure as Code prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.