Write and test an OPA Gatekeeper ConstraintTemplate with Rego v1 syntax

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

Steps

  1. Create a ConstraintTemplate YAML with apiVersion: templates.gatekeeper.sh/v1, kind: ConstraintTemplate, and spec.crd.spec.names.kind matching the constraint CRD name you want (e.g., K8sRequiredLabels).
  2. Under spec.targets, declare target: admission.k8s.gatekeeper.sh and a code block with engine: Rego and source.version: v1 to use Rego v1 syntax without needing an import rego.v1 statement.
  3. Write your Rego policy inside source.rego; use violation contains {"msg": msg} if ... pattern and reference input.review.object for the admission request object.
  4. Create a corresponding Constraint resource (the CRD generated by the template) with a match block scoping it to the desired resource kinds and namespaces.
  5. Use the gator CLI (gator verify) with a test suite YAML to run unit tests against the ConstraintTemplate locally before applying to a cluster.
  6. Apply both the ConstraintTemplate and the Constraint to the cluster and verify enforcement with kubectl describe constraint <name> to inspect violations.

Known gotchas

Related routes

Write an OPA Gatekeeper ConstraintTemplate and Constraint to require resource labels
open-policy-agent.github.io/gatekeeper · 6 steps · unrated
Deploy OPA Gatekeeper to a Kubernetes cluster, write a ConstraintTemplate and Constraint to block privileged containers, and test with a dry-run audit
open-policy-agent.github.io · 5 steps · unrated
Configure OPA Gatekeeper with a ConstraintTemplate and Constraint to enforce that all Deployments have a specified owner label and block admission for non-compliant resources
open-policy-agent.github.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