This skill covers implementing Open Policy Agent (OPA) and Gatekeeper for policy-as-code enforcement in Kubernetes and CI/CD pipelines. It addresses writing Rego policies, deploying OPA Gatekeeper as a Kubernetes admission controller, testing policies in development, and integrating policy evaluation into deployment pipelines.
Install with Codex or Claude Copy this prompt, paste it into Codex, Claude, or another assistant, and let it review the skill page and install it for you.
A direct command skips the review prompt. Inspect the source before running it.
This skill covers implementing Open Policy Agent (OPA) and Gatekeeper for policy-as-code enforcement in Kubernetes and CI/CD pipelines. It addresses writing Rego policies, deploying OPA Gatekeeper as a Kubernetes admission controller, testing policies in development, and integrating policy evaluation into deployment pipelines.
Implementing Policy as Code with Open Policy Agent
When to Use
When enforcing organizational security policies across Kubernetes clusters programmatically
When requiring admission control that blocks non-compliant resources from being created
When implementing policy governance that can be version-controlled, tested, and audited
When standardizing security rules across multiple clusters and environments
When needing a flexible policy engine that extends beyond Kubernetes to APIs and CI/CD
Do not use for vulnerability scanning (use Trivy/Checkov), for runtime threat detection (use Falco), or for network policy enforcement (use Kubernetes NetworkPolicy or Calico).
Common Misconfigurations & Verification
A policy engine left in advisory mode enforces nothing:
Constraints stuck in enforcementAction: warn (or dryrun). Warn/dryrun only annotate the audit log; non-compliant resources are still admitted. Flip to enforcementAction: deny once the remediation window closes, and verify with a test apply.
conftest exit code ignored in CI.conftest test returns non-zero on a deny[...] rule, but a trailing || true, continue-on-error: true, or using only warn[...] rules means the pipeline passes regardless. Use deny rules for hard gates and let the non-zero exit fail the job.
Constraint match: too narrow. If match.kinds omits Deployment/StatefulSet/DaemonSet (only Pod), workloads bypass the policy entirely. Over-broad excludedNamespaces (beyond kube-system/gatekeeper-system) also creates blind spots.
Gatekeeper webhook not actually intercepting (failurePolicy Ignore, or template/constraint not applied) so admission silently allows everything.
conftest pointed at the wrong --policy dir or --parser (e.g. HCL not parsed) → zero rules evaluated, green by accident.
Concrete verification:kubectl apply a Pod that clearly violates a deny constraint (a privileged: true container, or a container with no CPU/memory limit) and confirm the API server with the policy's violation message. In CI, run on a manifest using and confirm the job .
rejects it
conftest test
:latest
exits non-zero and fails
Prerequisites
Kubernetes cluster with admin access for Gatekeeper installation
Open Policy Agent — general-purpose policy engine using Rego language for policy decisions
Rego
OPA's declarative query language for writing policy rules
Gatekeeper
Kubernetes-native OPA integration implementing admission control via ConstraintTemplates
ConstraintTemplate
CRD defining the Rego policy logic and parameters schema for a class of constraints
Constraint
Instance of a ConstraintTemplate with specific parameters and scope (which resources to check)
Admission Controller
Kubernetes component that intercepts API requests before persistence and can allow or deny them
conftest
CLI tool for testing structured data (YAML, JSON, HCL) against OPA policies
Tools & Systems
Open Policy Agent (OPA): General-purpose policy engine for unified policy enforcement
Gatekeeper: Kubernetes admission controller built on OPA with CRD-based configuration
conftest: Testing framework for OPA policies against configuration files
Kyverno: Alternative Kubernetes policy engine using YAML-based policies (no Rego required)
Styra DAS: Commercial OPA management platform with policy authoring, testing, and distribution
Common Scenarios
Scenario: Enforcing Container Security Standards Across Clusters
Context: Multiple development teams deploy to shared Kubernetes clusters. Some teams run privileged containers and images without resource limits, causing security and stability issues.
Approach:
Deploy Gatekeeper on all clusters via GitOps (Helm chart in a FluxCD repository)
Create ConstraintTemplates for: no privileged containers, required resource limits, required labels, no latest tag
Start with enforcementAction: warn to identify violations without blocking deployments
Notify teams of violations and provide a 2-week remediation window
Switch to enforcementAction: deny after the remediation period
Add excludedNamespaces for kube-system and monitoring namespaces
Pitfalls: Deploying Gatekeeper with deny mode immediately can break existing workloads. Always start with warn mode. Overly restrictive policies without exemptions for system namespaces can prevent cluster components from functioning.