ValidatingPolicy
The Kyverno ValidatingPolicy
type extends the Kubernetes ValidatingAdmissionPolicy
type for complex policy evaluations and other features required for Policy-as-Code. A ValidatingPolicy
is a superset of a ValidatingAdmissionPolicy
and contains additional fields for Kyverno specific features.
apiVersion: policies.kyverno.io/v1alpha1
kind: ValidatingPolicy
metadata:
name: check-labels
spec:
validationActions:
- Deny
matchConstraints:
resourceRules:
- apiGroups: [""]
apiVersions: [v1]
operations: [CREATE, UPDATE]
resources: [pods]
validations:
- message: label 'environment' is required
expression: "'environment' in object.metadata.?labels.orValue([])"
Comparison with ValidatingAdmissionPolicy
The table below compares major features across the Kubernetes ValidatingAdmissionPolicy
and Kyverno ValidatingPolicy
types.
Feature | ValidatingAdmissionPolicy | ValidatingPolicy |
---|---|---|
Enforcement | Admission | Admission, Background, Pipelines, … |
Payloads | Kubernetes | Kubernetes, Any JSON or YAML |
Distribution | Kubernetes | Helm, CLI, Web Service, API, SDK |
CEL Library | Basic | Extended |
Bindings | Manual | Automatic |
Auto-generation | - | Pod Controllers, ValidatingAdmissionPolicy |
External Data | _ | Kubernetes resources or API calls |
Caching | _ | Global Context, image verification results |
Background scans | - | Periodic, On policy creation or change |
Exceptions | - | Fine-grained exceptions |
Reporting | _ | Policy WG Reports API |
Testing | _ | Kyverno CLI (unit), Chainsaw (e2e) |
Additional Fields
The ValidatingPolicy
extends the Kubernetes ValidatingAdmissionPolicy with the following additional fields for Kyverno features. A complete reference is provided in the API specification.
evaluation
The spec.evaluation
field defines how the policy is applied and how the payload is processed. It can be used to enable, or disable, admission request processing and background processing for a policy. It is also used to manage whether the payload is processed as JSON or a Kubernetes resource.
1apiVersion: policies.kyverno.io/v1alpha1
2kind: ValidatingPolicy
3metadata:
4 name: sample
5spec:
6 evaluation:
7 admission:
8 enabled: false
9 background:
10 enabled: true
11 mode : Kubernetes
12 ...
The mode
can be set to JSON
for non-Kubernetes payloads.
Refer to the API Reference for details.
webhookConfiguration
The spec.webhookConfiguration
field defines properties used to manage the Kyverno admission controller webhook settings.
1apiVersion: policies.kyverno.io/v1alpha1
2kind: ValidatingPolicy
3metadata:
4 name: check-deployment-labels
5spec:
6 webhookConfiguration:
7 timeoutSeconds: 15
8 ...
In the policy above, webhookConfiguration.timeoutSeconds
is set to 15
, which defines how long the admission request waits for policy evaluation. The default is 10
seconds, and the allowed range is 1
to 30
seconds. After this timeout, the request may fail or ignore the result based on the failure policy. Kyverno reflects this setting in the generated ValidatingWebhookConfiguration
.
Refer to the API Reference for details.
autogen
The spec.autogen
field defines policy auto-generation behaviors, to automatically generate policies for pod controllers and generate ValidatingAdmissionPolicy
types for Kubernetes API server execution.
Here is an example of generating policies for deployments, jobs, cronjobs, and statefulsets and also generating a ValidatingAdmissionPolicy
from the ValidatingPolicy
declaration:
1 apiVersion: policies.kyverno.io/v1alpha1
2 kind: ValidatingPolicy
3 metadata:
4 name: disallow-capabilities
5 spec:
6 autogen:
7 validatingAdmissionPolicy:
8 enabled: true
9 podControllers:
10 controllers:
11 - deployments
12 - jobs
13 - cronjobs
14 - statefulsets
Generating a ValidatingAdmissionPolicy
from a ValidatingPolicy
provides the benefits of faster and more resilient execution during admission controls while leveraging all features of Kyverno.
Refer to the API Reference for details.
Kyverno CEL Libraries
Kyverno enhances Kubernetes’ CEL environment with libraries enabling complex policy logic and advanced features. For comprehensive documentation of all available CEL libraries, see the CEL Libraries documentation.
Exceptions
Policies are applied cluster-wide by default. However, there may be times when an exception is required. In such cases, the PolicyException can be used to allow select resources to bypass the policy, without modifying the policies themselves. This ensures that your policies remain secure while providing the flexibility to grant exceptions as needed.
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.