Open Policy Agent (OPA) Fundamentals
What you'll learn: How OPA serves as a flexible, general-purpose policy engine that integrates with Kubernetes to enforce custom security and operational policies.
What is Open Policy Agent?
Open Policy Agent (OPA) is a policy-as-code framework that lets you write, test, and enforce rules across your infrastructure. Think of it as a security guard who checks every request against a rulebook you've written—but instead of guarding one door, OPA can guard any system that asks for its decision.
Unlike admission controllers that are built into Kubernetes with fixed logic, OPA is general-purpose. You write policies in a declarative language called Rego, and OPA evaluates requests against those policies, returning simple yes/no decisions (or more complex data).
Why Use OPA with Kubernetes?
While RBAC (which you've already learned) handles who can do what, OPA adds a richer layer: policy-based control. You can enforce rules like:
- "Containers must not run as root"
- "Only images from approved registries are allowed"
- "Services in production must have resource limits"
OPA integrates with Kubernetes through admission webhooks (validating and mutating, which you've just learned). When a resource is created or modified, Kubernetes sends it to OPA, which evaluates your custom policies and either allows or denies the request.
The OPA Workflow
- You write policies in Rego
- Deploy OPA as a sidecar or standalone service
- Register OPA as a validating/mutating admission webhook
- Kubernetes queries OPA before admitting resources
- OPA returns a decision based on your policies
Key Takeaway: OPA is a flexible, general-purpose policy engine that extends Kubernetes admission control beyond RBAC, allowing you to enforce custom, organization-specific policies written as code.