Skip to main content
In this lesson you’ll learn how to use Kyverno pattern wildcards to enforce that every container in a Pod declares CPU and memory requests and limits. This is a common policy need to ensure predictable scheduling and resource accounting across a cluster. Overview
  • We create a ClusterPolicy named all-containers-need-requests-and-limits that enforces CPU and memory requests and limits for every container in a Pod.
  • The policy uses Kyverno pattern wildcards to match any container and require that the resource fields exist and are non-empty.
ClusterPolicy (ensures every container in a Pod has non-empty CPU and memory requests and limits)
How the pattern and wildcards work
  • The policy targets Pod resources via match and applies a pattern to spec.containers.
  • Using - name: "*" applies the pattern to every element in the containers list.
  • The ?* wildcard is applied to string fields to require presence and non-empty values (we are not validating format, only that some value exists).
Wildcard summary
In Kyverno patterns, using ?* on a string field ensures the field is present and contains at least one character (examples: 100m, 128Mi). Use this when you need to validate presence without enforcing a specific value format.
This policy uses validationFailureAction: enforce, so requests that violate the rule are rejected by the admission webhook. During development, consider validationFailureAction: audit if you want to observe violations without blocking resources.
Apply the policy
Expected output
Test a Pod that violates the policy (no resources defined) pod-bad-resource.yaml:
Apply the bad pod (using stdin for demonstration)
Expected rejection (admission webhook denies the request)
Why it was rejected
  • The policy pattern requires limits.memory, limits.cpu, requests.memory, and requests.cpu to be present and non-empty for every container.
  • The example pod lacked the resources block entirely, so Kyverno denied creation and returned the policy message.
Create a Pod that complies with the policy pod-good-resource.yaml:
Apply the good pod
Expected output
Explanation
  • The pod was created successfully because each container included all four resource fields (requests.cpu, requests.memory, limits.cpu, limits.memory) and each value matched the ?* requirement (non-empty string).
Best practices and summary
  • Use - name: "*" to apply a pattern to every element in a list (e.g., every container).
  • Use ?* on string fields to require that they exist and are non-empty without enforcing a specific format.
  • Use validationFailureAction: enforce to actively block invalid resources; use audit to log violations without blocking during policy rollout.
  • This pattern avoids per-container rules and enforces resource declarations cluster-wide with a single, maintainable policy.
Links and references

Watch Video