Use Kyverno Generate Rules
Use Kyverno generate rules to create Kubernetes resources automatically when a matching resource is created or updated. A generate rule defines:
- Which source resource to watch, using
match. - Which changes should trigger generation, using optional
preconditions. - Which target resource Kyverno should create, using
generate. - Whether Kyverno should keep the generated resource synchronized with the source resource.
This guide uses a ConfigMap update as the example workflow. When a specific ConfigMap in a specific namespace changes, Kyverno creates a platform NotificationMessage resource. The notification service then sends an email to the specified recipient.
For the upstream Kyverno reference, see the official Kyverno generate rules documentation.
TOC
How Generate WorksExample ScenarioPrerequisitesPrepare the Target Resource DependencyPrepare the Source ResourceGrant Kyverno PermissionCreate the Generate PolicyVerify the Generate RuleTroubleshootingHow Generate Works
A Kyverno generate rule is part of a ClusterPolicy or Policy.
Use synchronize: false when every trigger should create an independent target resource. Use synchronize: true only when the generated resource should be updated or removed along with the source resource.
Example Scenario
In this example:
- The source namespace is
app-team-a. - The source ConfigMap is
app-notification-source. - Only
UPDATEoperations are matched. - Only changes to
ConfigMap.datatrigger generation. - Kyverno creates a short-lived
NotificationMessageincpaas-system. - The generated message sends an email directly to
platform-operator@example.com.
The notification message API used in this example is notificationmessages.aitextensions.alauda.io/v1beta1. It is served by the platform notification API and is consumed quickly after creation.
Prerequisites
- Alauda Container Platform Compliance with Kyverno is installed.
- The platform notification capability is installed in the same cluster.
- An email notification server has been configured. The platform default email server is stored as the
platform-email-serverSecret in thecpaas-systemnamespace. - A notification template for email messages exists in the
cpaas-systemnamespace. - Kyverno has permission to create
NotificationMessageresources in the target namespace.
This example does not require a notification rule, notification policy, notification receiver, or notification sender resource. The recipient email address is specified directly in the generated NotificationMessage.
Prepare the Target Resource Dependency
The generated NotificationMessage references an email template. Create the template before applying the Kyverno policy.
Prepare the Source Resource
Create the namespace and ConfigMap that Kyverno will watch.
Grant Kyverno Permission
Kyverno creates generated resources through its background controller. Grant the controller permission to create NotificationMessage resources in the target namespace.
If Kyverno is installed in a different namespace or uses a different background controller ServiceAccount name, update the subjects section before applying the YAML.
Create the Generate Policy
Create a ClusterPolicy that generates a NotificationMessage only when app-team-a/app-notification-source is updated and its data field changes.
The generate rule uses these key fields:
match.resources: Watches only the specified ConfigMap and onlyUPDATEoperations.preconditions: Compares the new and olddatafields so metadata-only updates do not generate messages.generate.apiVersion,kind,name, andnamespace: Define the target resource identity.generate.data: Defines the full generated resource body.synchronize: false: Creates a separate target resource for each matching update.
The generated NotificationMessage uses spec.body.labels as template variables, ephemeral.methods to select email delivery, ephemeral.template to reference the email template, and receivers[].destination to specify the recipient email address.
Verify the Generate Rule
Update the watched ConfigMap.
Check the Kyverno background controller logs. The logs show that Kyverno created the generated target resource.
The log output contains a generated target similar to the following:
For this example, also check the notification API logs. A successful email delivery shows one task, the email sender and recipient, and a Done status.
The log output contains entries similar to the following:
NotificationMessage resources are short-lived and are consumed by the notification API. A normal kubectl get command may not show the generated message after it has been accepted.
Troubleshooting
If no target resource is generated, check the Kyverno update requests.
If an update request failed, inspect it for the detailed error.
Common causes include:
- The source resource update did not match the
match.resourcesfilter. - The ConfigMap update did not change the
datafield, so the precondition was not met. - The Kyverno background controller does not have permission to create
notificationmessages.aitextensions.alauda.ioresources incpaas-system. - The
notificationmessages.aitextensions.alauda.io/v1beta1API is not available in the target cluster. - The generated resource name exceeds the Kubernetes DNS label length limit.
- The email template referenced by
ephemeral.templatedoes not exist. - The email notification server is not configured or is not available.