Opscraft
Open Policy Agent · Lesson 1 of 9 · 4 min read

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.

Enlarge diagramPull requestconftest in CI: fastfeedbackAdmissionGatekeeper / Kyvernoblocks itAuditfinds what already exists
Figure 1. Policy as code checks the same rules at every point a change can enter: in CI, at admission, and in a running audit.
Swipe sideways to see all of it, or tap to open it full screen.

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.
Enlarge diagramService (PEP)API server, Envoy, CI jobOPA (PDP)evaluatespolicies + dataRego rules, JSONlistsinput (JSON)decision
Figure 2. OPA decides; the caller enforces. The service sends JSON input, OPA evaluates it against Rego policies and data, and returns a JSON decision.
Swipe sideways to see all of it, or tap to open it full screen.

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.
Enlarge diagraminputthe thing being judged: AdmissionReview, HTTPrequest, plan JSONdatapolicies + reference JSON: approved registries, ownersbundlespolicies + data downloaded from S3 / OCIdecision logsevery decision recorded for audit
Figure 3. Everything OPA works with: the input being judged, data it already knows, and bundles that keep many OPAs in sync.
Swipe sideways to see all of it, or tap to open it full screen.

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