Policy Engines for Platform Governance

CNPA Domain: Platform Observability, Security, and Conformance (20%) Topic: Policy Engines for Platform Governance

Overview

Platform governance requires enforcing organizational standards consistently across every workload, namespace, and team. Policy engines automate this enforcement by intercepting Kubernetes API requests and validating them against defined rules β€” before resources are ever created.

Without policy engines, platform teams rely on documentation, code reviews, and hope. With them, guardrails are enforced at the API layer.


Kubernetes Admission Control

Policy engines plug into the Kubernetes API server via admission webhooks:

kubectl apply -f deployment.yaml
     ↓
Kubernetes API Server
     ↓
[Authentication] β†’ [Authorization (RBAC)] β†’ [Admission Control]
                                                     ↓
                                         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                         β”‚  Validating Webhooks  β”‚ ← Policy Engine
                                         β”‚  Mutating Webhooks    β”‚ ← Policy Engine
                                         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                                     ↓
                                            etcd (persisted)

Mutating webhooks can modify resources (e.g., inject sidecars, set defaults). Validating webhooks accept or reject resources based on rules.


OPA / Gatekeeper

Open Policy Agent (OPA)arrow-up-right is a general-purpose policy engine. OPA Gatekeeperarrow-up-right is its Kubernetes-native integration.

Policies are written in Rego β€” a purpose-built policy language.

Gatekeeper Architecture

Example: Require Resource Limits on All Containers


Kyverno

Kyvernoarrow-up-right is a Kubernetes-native policy engine that uses YAML-based policies β€” no Rego required.

Policy Types

Type
Use Case

Validate

Reject non-compliant resources

Mutate

Auto-patch resources (add labels, set defaults)

Generate

Create related resources (e.g., NetworkPolicy on Namespace creation)

VerifyImages

Enforce container image signatures

Example: Disallow latest Image Tag

Example: Auto-Generate NetworkPolicy for New Namespaces


OPA Gatekeeper vs Kyverno

Aspect
OPA Gatekeeper
Kyverno

Policy language

Rego

YAML

Learning curve

Steep (Rego)

Low (YAML)

Flexibility

Very high

High

Mutation

Limited

Strong

Image verification

Via external tools

Built-in

Audit mode

βœ…

βœ…

Community

CNCF graduated

CNCF graduated

Guidance: Use Kyverno for most Kubernetes-native governance use cases. Use OPA for complex multi-system policy needs.


Policy-as-Code Patterns

Audit Mode vs Enforce Mode

Best practice: Start in Audit mode to find violations, then switch to Enforce once policies are refined.

Policy Library

Platform teams maintain a policy library in Git:

Policies are deployed via GitOps alongside application workloads.


Key Takeaways

  • Policy engines plug into admission webhooks to validate/mutate resources before they're accepted into the cluster

  • OPA Gatekeeper uses Rego for flexible, powerful policy logic suited for complex rules

  • Kyverno uses YAML policies β€” lower barrier to entry, strong mutation and generate capabilities

  • Start policies in Audit mode to measure compliance before enforcing

  • Treat policies as code β€” version-controlled, reviewed, deployed via GitOps

  • Generate policies in Kyverno can automatically create companion resources (NetworkPolicies, ResourceQuotas) when namespaces are created


Further Reading

Last updated