Interview's MateKubernetes
Interview's Mate

Kubernetes

K8s · Workloads · GitOps · Cloud

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Kubernetes topics
Lesson 110/ 160
06. Security, RBAC & Governance

Open Policy Agent (OPA) & Gatekeeper Constraints

Enforce Rego policies cluster-wide with OPA Gatekeeper constraint templates.

AdvancedOPA · Gatekeeper · Rego3 quiz questions
Practice DevOps Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

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-namespaces and 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

Loading interactive 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.

Verify Gatekeeper, not only object creation

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.

Key takeaway: OPA is implemented only when this invariant remains observable: 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.

Recover from Gatekeeper drift

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.

Key takeaway: Accept the trade-off consciously: Gatekeeper supports expressive cross-resource policy with one engine, but Rego expertise, sync-data cost, and admission latency raise the policy maintenance bar.

How it works, step by step

Step 1 of 4
Identity owner

1. 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.

OPA contract → owned state
Key numbers at this step
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-namespaces in 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-namespaces first: 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

Admission and least privilege
Best for: Enforce Rego policies cluster-wide with OPA Gatekeeper constraint templates. A concrete incident check is 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.

Runtime-only detection
Best for: Quickly testing the Gatekeeper assumption with 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.

Hosted security service
Best for: Teams that need Rego integrated with guardrails; the evidence path is 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-namespaces without 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?
Interview tip: Interview sequence: state the invariant, run 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

Loading quiz

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.

Open DevOps Q&A →Tech Learn Q&A
Previous topic
Policy-as-Code with Kyverno
Next topic
Image Supply Chain Security: Admission Verification with Cosign & Kyverno