Sub topic 6
Kyverno generate rules
This chapter assumes you can write a Kyverno validate rule, know how namespaces and NetworkPolicies work, and can use kubectl. Validate and mutate rules act on a request that is already in flight. A generate rule creates a different resource in response to a trigger, such as a new Namespace.
What a generate rule does
A generate rule watches for a trigger resource and creates a downstream resource. The usual use is to give every new namespace a baseline: a default-deny NetworkPolicy, a ResourceQuota, or a copy of a shared ConfigMap.
Kyverno creates the downstream resource itself, through the API server. The trigger request is not modified.
A default-deny policy per namespace
This policy creates a NetworkPolicy that denies all ingress in every new namespace.
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: add-default-deny
spec:
rules:
- name: default-deny-ingress
match:
any:
- resources:
kinds:
- Namespace
generate:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
name: default-deny-ingress
namespace: "{{request.object.metadata.name}}"
synchronize: true
data:
spec:
podSelector: {}
policyTypes:
- IngressThe generate block names the downstream resource. The {{request.object.metadata.name}} variable reads the name of the new Namespace, so the NetworkPolicy lands in that namespace. The data field holds the body of the resource.
Create a namespace and check the result:
kubectl create namespace team-a
kubectl get networkpolicy -n team-aSynchronize and clone
The synchronize field controls what happens after creation.
| Setting | Behavior |
|---|---|
| `synchronize: true` | Kyverno keeps the downstream resource matching the policy. If someone edits or deletes it, Kyverno restores it. Deleting the policy or the trigger removes the generated resource. |
| `synchronize: false` | Kyverno creates the resource once and does not manage it afterwards. |
To copy an existing resource instead of defining a new one, use clone in place of data. This is common for image pull Secrets or shared ConfigMaps:
generate:
apiVersion: v1
kind: ConfigMap
name: shared-config
namespace: "{{request.object.metadata.name}}"
synchronize: true
clone:
namespace: default
name: shared-configWith synchronize: true, a change to the source ConfigMap propagates to every clone.
Common errors
- Permission denied. Generating a resource needs RBAC permission. The Kyverno background controller must be allowed to create the target kind. Grant this with an additional ClusterRole, and check the controller logs when a resource does not appear.
- Deleting policies by surprise. With
synchronize: true, removing the policy can delete every generated resource. Review this before cleanup. - Fighting other tools. If a GitOps tool also manages the generated resource, the two will overwrite each other. Let one owner manage it.
- Existing namespaces. A generate rule on Namespace only fires for new ones. To cover existing namespaces, set
generateExisting: trueon the rule. Test this in a non-production cluster first.
Exercise
A generate rule uses synchronize: true. A user deletes the generated NetworkPolicy by hand. What does Kyverno do, and what happens if the policy itself is deleted?
Show answer
Kyverno recreates the NetworkPolicy, because synchronize keeps the downstream resource matching the policy. If the policy is deleted, Kyverno also removes the generated resources.