Write an OPA Gatekeeper ConstraintTemplate and Constraint to require resource labels

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` manifest with `apiVersion: templates.gatekeeper.sh/v1` (v1 is current since Gatekeeper 3.6.0) and define the Rego policy in `spec.targets[].rego`.
  2. The Rego in the template must use `package` matching the `spec.crd.spec.names.kind` lowercased, and define a `violation[{"msg": msg}]` rule.
  3. Apply the ConstraintTemplate with `kubectl apply -f constraint-template.yaml`; Gatekeeper CRDs the generated constraint kind within seconds.
  4. Create a `Constraint` manifest using the new CRD kind (e.g., `kind: K8sRequiredLabels`) referencing the ConstraintTemplate by its kind, listing required label keys in the parameters.
  5. Test the constraint by applying a non-compliant resource and confirming Gatekeeper returns an admission denial message.
  6. Use `kubectl get constraints` and `kubectl describe <ConstraintKind> <name>` to view violation counts and recent audit results.

Known gotchas

Related routes

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
Author an OPA Gatekeeper ConstraintTemplate and Constraint to enforce image signature requirements in Kubernetes
open-policy-agent.github.io/gatekeeper · 6 steps · unrated
Write and test an OPA Gatekeeper ConstraintTemplate with Rego v1 syntax
open-policy-agent.github.io/gatekeeper · 6 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