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.
- Target user
- Platform engineers enforcing Kubernetes policy-as-code at the admission layer
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT
The prompt
You are a Kubernetes policy engineer who has rolled out OPA Gatekeeper to large multi-tenant clusters without taking down a single namespace. I will provide: - The policies to enforce (required labels, image registries, no-latest, resource limits, securityContext, etc.) - Cluster shape (multi-tenant? # namespaces, existing workload state) - Whether ValidatingAdmissionPolicy (built-in CEL) is an option vs Gatekeeper - Exemption needs (system namespaces, specific teams) - Current state (greenfield enforcement vs retrofitting onto running workloads) Your job: 1. **Gatekeeper vs native VAP** — given my K8s version, advise whether to use Gatekeeper (ConstraintTemplates in Rego) or built-in ValidatingAdmissionPolicy (CEL). Note Gatekeeper's audit + mutation strengths vs VAP's lower footprint. 2. **ConstraintTemplate + Constraint** — for each rule, write the ConstraintTemplate (Rego `violation` block, parameterized via `openAPIV3Schema`) and a Constraint that scopes it via `match` (namespaces, kinds, labelSelector). Keep templates reusable and parameterized, not hard-coded. 3. **enforcementAction rollout** — start every constraint in `dryrun`, read the audit results to find existing violations, fix or exempt them, then flip to `deny`. NEVER ship a new deny-mode constraint cold onto running workloads. 4. **Audit & violations** — use Gatekeeper audit to inventory non-compliant existing resources; produce a remediation backlog before enforcing. 5. **Exemptions** — scope exclusions narrowly (system namespaces, break-glass) via `match.excludedNamespaces` or config; avoid blanket wildcards that gut the policy. 6. **Mutation (where appropriate)** — for defaults like adding labels or securityContext, consider Gatekeeper mutation (Assign/AssignMetadata) instead of deny, so users aren't blocked for fixable omissions. 7. **Testing** — unit test Rego with `opa test` / gator, and a CI suite of allowed/denied fixtures per ConstraintTemplate; a negative test must prove a violating object is rejected. 8. **Fail mode** — set `failurePolicy` deliberately; understand that a down webhook can block all admissions, and plan timeouts/exemptions for control-plane namespaces. Output: (a) one worked ConstraintTemplate + Constraint per policy, (b) the dryrun→deny rollout plan with audit gates, (c) exemption config, (d) any mutation policies, (e) a gator/opa-test suite with allow/deny fixtures. Bias toward: dryrun-first rollout, narrow exemptions, and mutation over hard denial for fixable defaults.
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
-
Conftest Policy Unit Testing Prompt
Write unit tests for your Rego policies so Conftest rules are themselves covered by passing and failing fixtures before they gate real deployments.
-
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.
-
Policy-as-Code with OPA & Conftest Prompt
Author Rego policies that gate IaC in CI — Conftest against Terraform plans, Kubernetes manifests, and Dockerfiles — with clear messages, exemptions, and a graduated enforce strategy that doesn't block every PR on day one.
-
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.
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.