Skip to main content
In this lesson, you’ll learn the most common and fundamental way to validate resources in Kyverno: using a pattern-based validate rule. Meet Alex, a platform engineer with a simple but important requirement: he must ensure every new Namespace in the cluster is labeled with its intended purpose so cost allocation and governance remain accurate.
The image outlines a goal related to enforcing namespace labels to ensure cost allocation and governance, requiring a label 'purpose' with the value 'production' for new namespaces. It features a character named Alex along with a quotation explaining the goal.
Goal: enforce that every newly created Namespace must include a label purpose: production. This prevents accidental or untracked Namespace creation and is an ideal use case for a Kyverno validate rule. Below we build a cluster-scoped policy step-by-step to enforce this requirement.

ClusterPolicy: require-ns-purpose-label

We use ClusterPolicy because the rule must apply to all Namespaces regardless of where they are created.
What each section does:

How the pattern works

  • Kyverno compares the incoming Namespace resource to the pattern.
  • If the pattern is present and matches (exact purpose: production), Kyverno allows the request.
  • If the pattern is missing, the label value differs, or the labels block is absent, Kyverno rejects the request with the provided message.

Compliant example

A Namespace manifest that satisfies the policy:
Kyverno will find metadata.labels.purpose: production and allow the Namespace creation.

Non-compliant example

A Namespace manifest that fails validation:
Because purpose is development (not production), Kyverno will block the request.
If validation fails, users receive immediate feedback when they run kubectl apply, preventing misconfigurations before resources are created.
Example kubectl failure output for the non-compliant manifest:
The error identifies the admission webhook, the blocked resource, the policy that triggered it, and the custom message — giving the user a clear action to resolve the failure.

Further reading

That’s it for this lesson — a simple validate pattern can enforce important governance controls like required labels across your cluster.

Watch Video