Policy as code: the idea and the architecture
Open Policy Agent (OPA) is a general-purpose policy engine. You write rules as code in a language called Rego, give OPA some JSON describing a request or a configuration, and it returns a decision: allow or deny, plus reasons. In Kubernetes it usually runs as Gatekeeper, blocking non-compliant objects at admission time; in CI it runs as conftest, failing pipelines before bad manifests or Terraform plans reach production. Senior interviews expect you to explain policy-as-code, write a basic Rego rule, design a safe rollout of policies across clusters, and compare Gatekeeper with Kyverno and Kubernetes' built-in options.
Why policy as code
Organisations have rules: images only from our ECR, no privileged containers, every namespace has an owner, all S3 buckets encrypted, production deployments need resource limits. Enforcing them by wiki pages and code review is inconsistent, slow and easy to bypass. Policy as code means:
- Rules are written as code, versioned in Git, reviewed in PRs, unit-tested.
- They're enforced automatically at every change point — admission control, CI, API gateways.
- Violations produce clear messages so developers fix issues themselves.
- Audits can list every existing violation at any time.
To a manager: guardrails written as code that automatically stop risky changes, managed like any other software.
Decoupling decisions from enforcement
OPA separates two roles:
- Policy Decision Point (PDP) — OPA: evaluates policies against input and returns a decision.
- Policy Enforcement Point (PEP) — the caller: the Kubernetes API server (via a webhook), Envoy, a microservice, a CI job. It asks OPA and enforces the answer.
OPA doesn't know about Kubernetes, HTTP or Terraform. It evaluates JSON against rules. That's why the same engine serves Kubernetes admission, microservice authorisation, Terraform plan checks and more.
Inputs, data and deployment options
input— the document being evaluated: an AdmissionReview for Kubernetes, an HTTP request for Envoy, a Terraform plan JSON in CI.data— everything else OPA knows: loaded policies and external JSON (lists of approved registries, team ownership, existing cluster objects).- Bundles — tarballs of policies and data that OPA instances download periodically from an HTTP server or S3/OCI registry, so many OPAs stay in sync.
- Decision logs — OPA can ship every decision (input, result, policy version) to a log service for audit.
OPA runs as a sidecar, a host daemon, a library (Go), or behind an admission webhook (Gatekeeper). OPA is a CNCF graduated project (graduated in 2021).