Skip to main content
In this article, we explore the integration of OPA (Open Policy Agent) with Kubernetes using the Gatekeeper approach. This method leverages the OPA Constraint Framework alongside Kubernetes admission controllers for enhanced policy enforcement and governance.
The image illustrates the OPA Constraint Framework, showing interactions between Kubernetes components, OPA, and Gatekeeper for policy enforcement and governance.
With the Gatekeeper approach, the admission controller collaborates with the OPA Constraint Framework by using CRD-based (Custom Resource Definition) policies. This facilitates easier policy sharing and builds trust across your Kubernetes environment. Before diving into the details of the OPA Constraint Framework, let’s review how to deploy OPA Gatekeeper in Kubernetes.

Installing OPA Gatekeeper

Deploying OPA Gatekeeper is simple. Execute the following command to apply the Gatekeeper specification files:
After deployment, verify that all Gatekeeper components are installed and running in the gatekeeper-system namespace:
Expected output:
Ensure that you have adequate RBAC permissions before deploying Gatekeeper in your cluster.

Understanding the OPA Constraint Framework

The OPA Constraint Framework allows you to declare policies that specify required conditions, enforce those conditions at the appropriate locations, and define the checks to be performed. For example, if you want all objects in a specific namespace (e.g., “example”) to include a “billing” label, the framework will enforce this rule via the Kubernetes admission controller. When a pod creation request is submitted, the admission controller follows these steps:
  1. Retrieve the labels from the pod.
  2. Verify if the required label (e.g., “billing”) is present.
  3. Return an error if the label is missing.
The image outlines the OPA Constraint Framework, detailing requirements, enforcement location, and specification actions for Kubernetes admission control with namespace and label examples.

Implementing Label Validation with Rego

Below is an example of Rego code that validates the presence of a required label (e.g., “billing”) on a pod. The code compares the provided labels with a hard-coded required label.

Example 1

Example 2

A similar rule with a slightly different format:

Example 3

An alternative format with syntactical differences:
In these examples:
  • The provided variable extracts labels from the incoming pod object.
  • The required set is fixed to include “billing”.
  • The missing variable determines any labels from the required set that are absent.
  • If any required labels are missing (count(missing) > 0), an error message is generated.

Extending the Use Case with Parameterization

To support more dynamic scenarios—such as enforcing different labels based on the namespace—you can create a Constraint Template. This enables you to pass the required label as a parameter instead of hardcoding it. Below is an example Constraint Template that encapsulates the Rego code while exposing a parameter for the required label:
Once your Constraint Template is ready, define specific constraints to enforce policies for different namespaces. For instance:

Constraint for Billing Label

Constraint for Tech Label

These constraints dynamically pass the required labels via the input.parameters object in Rego based on the namespace.

Summary

Below is a quick reference table summarizing the key steps for integrating OPA with Kubernetes using Gatekeeper:
Any object creation that violates the defined policies will trigger an error during the admission phase, preventing non-compliant objects from being admitted into the cluster.

Example Files

requiredlabels-template.yaml

require-label-billing.yaml

Apply these configurations with the following commands:
With these steps in place, any new Kubernetes object that fails to meet the policy requirements will be rejected at admission time, ensuring continued compliance within your cluster. That concludes our exploration of integrating OPA with Kubernetes using Gatekeeper. Experiment with these policies in your environment to tailor enforcement to your specific needs. For further details on OPA and Kubernetes, consider visiting:

Watch Video

Practice Lab