Sub topic 8
Gatekeeper: architecture and its relationship to OPA
This chapter assumes you know how validating and mutating admission webhooks work (covered earlier), can read YAML, and can use kubectl. You don't need to know Rego yet. The next chapters cover it.
OPA and Gatekeeper
Open Policy Agent (OPA) is a general-purpose policy engine. It takes JSON input and a policy written in Rego, and returns a decision. OPA knows nothing about Kubernetes by itself.
Gatekeeper is the Kubernetes-native project that connects OPA to the API server. It does three jobs:
- It registers as an admission webhook and passes each AdmissionReview request to the policy evaluator.
- It stores policy as Kubernetes custom resources, so you manage policy with
kubectland GitOps like any other object. - It audits objects that already exist in the cluster.
Gatekeeper embeds the OPA evaluation library (in recent versions, through a framework called the Constraint Framework). You do not run a separate OPA server or send it requests yourself.
Components
Gatekeeper normally installs into the gatekeeper-system namespace. It runs two main workloads.
| Component | Role |
|---|---|
| Controller manager (webhook) | Receives admission requests from the API server and evaluates them against loaded policy. Usually runs as several replicas. |
| Audit | Periodically lists existing resources and evaluates them against policy. Records violations in the `status` of each Constraint. |
Gatekeeper also runs controllers that watch its own custom resources. When you create a ConstraintTemplate, the controller generates a new CRD for the Constraint kind that the template defines.
Policy objects
Gatekeeper splits policy into two parts:
- A ConstraintTemplate holds the Rego logic and declares a new Constraint kind with a parameter schema.
- A Constraint is an instance of that kind. It says which resources it applies to and which parameter values to use.
This split lets one template, such as K8sRequiredLabels, serve many Constraints with different parameters. Later chapters write both.
Request flow
For each request, Gatekeeper finds the Constraints whose match section fits the object. It then runs the template's Rego for each one. Any violation result leads to a denial, unless the Constraint's enforcement action is something other than deny.
Inspecting an installation
These commands show the running pieces and the generated Constraint kinds:
kubectl get pods -n gatekeeper-system
kubectl get constrainttemplates
kubectl get constraintskubectl get constraints works because Gatekeeper puts every generated Constraint CRD in the constraints category.
Things to watch
- A Constraint cannot be created until its template exists and its CRD is ready. Applying both in one step can fail on the first try. Retry, or apply templates first.
- Gatekeeper evaluates only what the webhook receives. If a resource type is not in the webhook configuration, no Constraint can see it.
Exercise
A teammate says they must deploy a separate OPA server so Gatekeeper can evaluate policy. Is that correct? Name the two Gatekeeper workloads that handle admission and existing resources.
Show answer
No. Gatekeeper embeds the OPA evaluation engine, so no separate OPA server is needed. The controller manager (webhook) handles admission requests, and the audit component checks resources that already exist.
Exercise
Why does Gatekeeper split policy into a ConstraintTemplate and a Constraint?
Show answer
The template holds reusable Rego logic and defines a new Constraint kind with a parameter schema. Each Constraint then applies that logic to chosen resources with its own parameters, so one template can back many policies.