Open Policy Agent (OPA) & Gatekeeper Constraints
Enforce Rego policies cluster-wide with OPA Gatekeeper constraint templates.
In plain words
Gatekeeper is a programmable rulebook publisher: templates define the law, constraints choose where it applies, and Rego evaluates each case. The simple Kubernetes rule is: OPA Gatekeeper compiles Rego policy templates into ConstraintTemplate CRDs and instantiates Constraints that evaluate matching Kubernetes requests.
Think of it like…
Gatekeeper is a programmable rulebook publisher: templates define the law, constraints choose where it applies, and Rego evaluates each case.
Where you’ve seen it
During an access review, the security engineer runs kubectl get constrainttemplates; kubectl get constraints; kubectl get violations --all-namespaces. Expected evidence: OPA Gatekeeper compiles Rego policy templates into ConstraintTemplate CRDs and instantiates Constraints that evaluate matching Kubernetes requests. If the run instead shows this symptom, stop and restore the last known state: A wrong match, excluded system namespace, or reusable ConstraintTemplate regression can either deny critical controllers or silently miss the resource kind.
Words you’ll hear
- OPA
- For this lesson, OPA is the controlling idea: OPA Gatekeeper compiles Rego policy templates into ConstraintTemplate CRDs and instantiates Constraints that evaluate matching Kubernetes requests.
- Gatekeeper
- Gatekeeper is the checkpoint; run
kubectl get constrainttemplates; kubectl get constraints; kubectl get violations --all-namespacesand inspect the owner chain. A wrong match, excluded system namespace, or reusable ConstraintTemplate regression can either deny critical controllers or silently miss the resource kind. - Rego
- Rego is the outcome to preserve; the cost is explicit: Gatekeeper supports expressive cross-resource policy with one engine, but Rego expertise, sync-data cost, and admission latency raise the policy maintenance bar.
- Verification evidence
- The evidence command is
kubectl get constrainttemplates; kubectl get constraints; kubectl get violations --all-namespaces. It connects the declaration to this behavior: Enforce Rego policies cluster-wide with OPA Gatekeeper constraint templates.
Visual diagram
Theory & deep dive
How OPA reaches the intended state
Enforce Rego policies cluster-wide with OPA Gatekeeper constraint templates. OPA Gatekeeper compiles Rego policy templates into ConstraintTemplate CRDs and instantiates Constraints that evaluate matching Kubernetes requests.
The concrete checkpoint is kubectl get constrainttemplates; kubectl get constraints; kubectl get violations --all-namespaces; compare the OPA owner, Gatekeeper state, and Rego outcome instead of treating YAML acceptance as proof.
Run kubectl get constrainttemplates; kubectl get constraints; kubectl get violations --all-namespaces after the change and correlate the result with Events and the owning object. OPA Gatekeeper compiles Rego policy templates into ConstraintTemplate CRDs and instantiates Constraints that evaluate matching Kubernetes requests.
Gatekeeper: failure boundary and production cost
A wrong match, excluded system namespace, or reusable ConstraintTemplate regression can either deny critical controllers or silently miss the resource kind. The prerequisite contract is concrete: OPA Gatekeeper compiles Rego policy templates into ConstraintTemplate CRDs and instantiates Constraints that evaluate matching Kubernetes requests.
Gatekeeper supports expressive cross-resource policy with one engine, but Rego expertise, sync-data cost, and admission latency raise the policy maintenance bar. A safe design limits blast radius, records the last good configuration, and uses kubectl get constrainttemplates; kubectl get constraints; kubectl get violations --all-namespaces as a deterministic rollback checkpoint.
Capture the current state with kubectl get constrainttemplates; kubectl get constraints; kubectl get violations --all-namespaces, then correct the controlling manifest rather than patching a child object; the relevant divergence signal is specific: A wrong match, excluded system namespace, or reusable ConstraintTemplate regression can either deny critical controllers or silently miss the resource kind.
How it works, step by step
Step 1 of 41. Model the trust boundary
Name the identity, action, resource, and trust boundary before choosing a control: OPA Gatekeeper compiles Rego policy templates into ConstraintTemplate CRDs and instantiates Constraints that evaluate matching Kubernetes requests.
- Goal
- Enforce Rego policies cluster-wide with OPA Gatekeeper constraint templates.
- Owner
- OPA
When to use it & trade-offs
What it is
Enforce Rego policies cluster-wide with OPA Gatekeeper constraint templates. OPA Gatekeeper compiles Rego policy templates into ConstraintTemplate CRDs and instantiates Constraints that evaluate matching Kubernetes requests.
Why it matters: A wrong match, excluded system namespace, or reusable ConstraintTemplate regression can either deny critical controllers or silently miss the resource kind. The invariant at risk is specific: OPA Gatekeeper compiles Rego policy templates into ConstraintTemplate CRDs and instantiates Constraints that evaluate matching Kubernetes requests. The quickest fact check before changing the design is kubectl get constrainttemplates; kubectl get constraints; kubectl get violations --all-namespaces.
When to reach for it
- Use OPA to enforce this invariant: OPA Gatekeeper compiles Rego policy templates into ConstraintTemplate CRDs and instantiates Constraints that evaluate matching Kubernetes requests.
- Use Gatekeeper evidence from
kubectl get constrainttemplates; kubectl get constraints; kubectl get violations --all-namespacesin reviews and incident timelines. Enforce Rego policies cluster-wide with OPA Gatekeeper constraint templates. - Choose the hosted security service route only when this cost is justified by governance or scale: Gatekeeper supports expressive cross-resource policy with one engine, but Rego expertise, sync-data cost, and admission latency raise the policy maintenance bar.
When to avoid or be careful
- Avoid assuming OPA is complete after YAML submission; the required behavior is concrete: OPA Gatekeeper compiles Rego policy templates into ConstraintTemplate CRDs and instantiates Constraints that evaluate matching Kubernetes requests.
- Avoid treating the next symptom as random; check the owner and state with
kubectl get constrainttemplates; kubectl get constraints; kubectl get violations --all-namespacesfirst: A wrong match, excluded system namespace, or reusable ConstraintTemplate regression can either deny critical controllers or silently miss the resource kind. - Avoid adopting a heavier platform wrapper before measuring its cost: Gatekeeper supports expressive cross-resource policy with one engine, but Rego expertise, sync-data cost, and admission latency raise the policy maintenance bar.
Native, manual, and managed paths for OPA
kubectl get constrainttemplates; kubectl get constraints; kubectl get violations --all-namespaces.Strengths: OPA Gatekeeper compiles Rego policy templates into ConstraintTemplate CRDs and instantiates Constraints that evaluate matching Kubernetes requests.
Watch out: Gatekeeper supports expressive cross-resource policy with one engine, but Rego expertise, sync-data cost, and admission latency raise the policy maintenance bar. Recovery must also recognize this signal: A wrong match, excluded system namespace, or reusable ConstraintTemplate regression can either deny critical controllers or silently miss the resource kind.
kubectl get constrainttemplates; kubectl get constraints; kubectl get violations --all-namespaces; the expected invariant is: OPA Gatekeeper compiles Rego policy templates into ConstraintTemplate CRDs and instantiates Constraints that evaluate matching Kubernetes requests.Strengths: kubectl get constrainttemplates; kubectl get constraints; kubectl get violations --all-namespaces gives immediate feedback without first building a platform layer.
Watch out: A manual change can hide drift, and unlike OPA, it does not make this symptom self-documenting: A wrong match, excluded system namespace, or reusable ConstraintTemplate regression can either deny critical controllers or silently miss the resource kind.
kubectl get constrainttemplates; kubectl get constraints; kubectl get violations --all-namespaces.Strengths: Platform controls can wrap this exact behavior in policy or dashboards: OPA Gatekeeper compiles Rego policy templates into ConstraintTemplate CRDs and instantiates Constraints that evaluate matching Kubernetes requests.
Watch out: The added platform ownership buys value at this cost: Gatekeeper supports expressive cross-resource policy with one engine, but Rego expertise, sync-data cost, and admission latency raise the policy maintenance bar.
Interview signals & numbers to know
When this topic comes up
- Interview anchor: OPA Gatekeeper compiles Rego policy templates into ConstraintTemplate CRDs and instantiates Constraints that evaluate matching Kubernetes requests.
- Trace OPA to Rego through Gatekeeper; the invariant is: OPA Gatekeeper compiles Rego policy templates into ConstraintTemplate CRDs and instantiates Constraints that evaluate matching Kubernetes requests.
- Recognize the symptom: A wrong match, excluded system namespace, or reusable ConstraintTemplate regression can either deny critical controllers or silently miss the resource kind.
- Justify the cost: Gatekeeper supports expressive cross-resource policy with one engine, but Rego expertise, sync-data cost, and admission latency raise the policy maintenance bar.
- Verify with kubectl get constrainttemplates; kubectl get constraints; kubectl get violations --all-namespaces instead of only inspecting YAML.
Benchmarks & rules of thumb
- 2
- Gatekeeper objects to author: ConstraintTemplate and Constraint
- 1
- Rego engine evaluates one matching request at a time
- 0
- constraints protect resources if the match and enforcement mode exclude them
Common mistakes & follow-ups
Common interview mistakes
- Debugging only the child object misses the OPA owner contract: OPA Gatekeeper compiles Rego policy templates into ConstraintTemplate CRDs and instantiates Constraints that evaluate matching Kubernetes requests.
- Ignoring this telltale condition leads to unrelated changes: A wrong match, excluded system namespace, or reusable ConstraintTemplate regression can either deny critical controllers or silently miss the resource kind.
- Repeating
kubectl get constrainttemplates; kubectl get constraints; kubectl get violations --all-namespaceswithout an owner for remediation wastes retry cost. Gatekeeper supports expressive cross-resource policy with one engine, but Rego expertise, sync-data cost, and admission latency raise the policy maintenance bar.
Likely interviewer follow-ups
- How would API output and runtime evidence prove this behavior: OPA Gatekeeper compiles Rego policy templates into ConstraintTemplate CRDs and instantiates Constraints that evaluate matching Kubernetes requests?
- Which owner object, logs, and Events would you inspect for this symptom-A wrong match, excluded system namespace, or reusable ConstraintTemplate regression can either deny critical controllers or silently miss the resource kind-?
- At what threshold would this cost become unacceptable: Gatekeeper supports expressive cross-resource policy with one engine, but Rego expertise, sync-data cost, and admission latency raise the policy maintenance bar?
kubectl get constrainttemplates; kubectl get constraints; kubectl get violations --all-namespaces, name the failure, then justify the cost. Invariant: OPA Gatekeeper compiles Rego policy templates into ConstraintTemplate CRDs and instantiates Constraints that evaluate matching Kubernetes requests. Failure: A wrong match, excluded system namespace, or reusable ConstraintTemplate regression can either deny critical controllers or silently miss the resource kind. Cost: Gatekeeper supports expressive cross-resource policy with one engine, but Rego expertise, sync-data cost, and admission latency raise the policy maintenance bar.Knowledge check
Put it into practice
Practice related interview Q&A
Apply Open Policy Agent (OPA) & Gatekeeper Constraints with generative AI interview Q&A and hands-on practice.