skip to content

Generate and Cleanup

A generate rule creates a default NetworkPolicy or quota when a namespace appears and keeps it in step; a cleanup policy deletes on a schedule. Interviewers ask when the engine became a controller.

on this pageshow

questions

4

What does a Kyverno generate rule do that a validate rule cannot?

level: juniorimportance: must knowfreq 60%

answer

  1. Not a yes-or-no answer
  2. Something appears, something else appears
  3. Guardrail is supplied, not demanded
  4. Trigger object, downstream object
  5. Inline data block or cloned source

basics

~20 s

A Kyverno generate rule creates a companion object when a trigger appears, such as a default-deny NetworkPolicy in every new namespace. A validate rule can only accept or reject the request; generate supplies the resource instead of demanding it.

solid answer

~40 s

A `validate` rule answers yes or no about the object being admitted. A `generate` rule reacts to a matching object, the trigger, by creating other objects elsewhere in the cluster, the downstream resources. The platform case is supplying a guardrail rather than demanding one: match `kind: Namespace`, and generate a default-deny NetworkPolicy plus a ResourceQuota into that namespace, so a tenant never has to write either. The body is either an inline `data:` block or a `clone:` of a live object, which is how an image-pull Secret gets copied out of a platform namespace. Generation is not part of the admission answer: the request is admitted, and Kyverno's background controller creates the downstream shortly afterwards, using its own ServiceAccount rather than the requester's identity.

go deeper

for a junior

Be ready to say in one sentence that a generate rule creates a companion object when a matching object appears, and to give the standard example of a default-deny NetworkPolicy in every new namespace.

for a middle

Explain the two bodies, an inline data block versus a clone of a live object, and that generation happens in the background controller after admission rather than inside the admission answer.

for a senior

Show you know it runs under Kyverno's own ServiceAccount, that missing RBAC is the usual reason nothing appears, and that generate must be paired with a validate rule if the object also has to stay.

for a principal

Own the framing question: which guardrails the platform hands to tenants versus which it refuses to ship without, and what it costs the platform team to become the owner of every object it generates.

## Where generate sits among Kyverno's rule types A Kyverno policy (`ClusterPolicy` cluster-wide, or the namespaced `Policy`) holds a list of rules. Every rule has a `match` block selecting the objects it applies to, and exactly one action: `validate`, `mutate`, `generate`, or `verifyImages`. `validate` decides whether the incoming object is allowed. `generate` does something categorically different: when a matching object appears, Kyverno creates *other* objects somewhere in the cluster. The vocabulary is worth learning precisely, because interviewers use it: - **Trigger** - the object the rule matched. Frequently a `Namespace`. - **Downstream** - the object or objects Kyverno creates as a result. ## The canonical platform use: supply, do not demand A multi-tenant cluster wants every namespace to have a default-deny NetworkPolicy, a ResourceQuota sized for the tier, and an image-pull Secret for the internal registry. There are two ways to get there. The demanding version is a `validate` rule: reject any namespace that arrives without those objects. That is unworkable - a Namespace request cannot carry a NetworkPolicy, so the tenant would have to create the namespace, then be blocked on everything else until they applied three more manifests they did not write. The supplying version is a `generate` rule. The tenant creates a namespace; a moment later the namespace already contains the default-deny NetworkPolicy and the quota. The tenant did nothing, learned nothing, and is nonetheless compliant. That inversion - the guardrail is handed to people rather than required from them - is the single point of this rule type, and it is what an interviewer is listening for. ## Two ways to say what to create The `generate` block names the target `apiVersion`, `kind`, `name`, and (for a namespaced kind) `namespace`, usually interpolated from the trigger with a variable such as `{{request.object.metadata.name}}`. The body is then one of: - **`data:`** - the object's spec written inline in the policy. Right for something you author and version alongside the rule, such as a default-deny NetworkPolicy with an empty `podSelector` and no ingress or egress rules. - **`clone:`** - a `namespace` and `name` pointing at a live object to copy. Right for something whose contents are managed elsewhere, such as a registry pull Secret held in a platform namespace. `cloneList` copies several matching objects at once. ## It is not part of the admission answer This catches people out. The admission webhook does not create the downstream inline. Kyverno records the intent and its background controller reconciles it, so generation is asynchronous: the namespace exists for a short window before the NetworkPolicy does. Consequences worth stating out loud in an interview: a test that reads the downstream immediately after creating the trigger will flake, and a workload that races into a brand-new namespace can briefly run before its guardrail lands. By default the rule fires on triggers created from now on. `generateExisting: true` makes Kyverno also process matching objects that were already there when the policy was installed. ## Which identity creates the object The downstream is created by Kyverno's background controller under its own ServiceAccount, not the requesting user's. That account only holds the rights an administrator granted it, typically by adding an aggregated ClusterRole, so a brand-new generate rule that targets a kind nobody granted simply produces nothing and reports a failure - the most common first-day symptom. It also means the rule is a privilege boundary. A tenant who may create a namespace, and nothing else, can cause a Secret to be materialised that they could not have created themselves. That is usually the intent, but it is why the clone source and the generated content deserve the same review as any privileged automation. ## Generate does not replace validate A generate rule creates the object; it does not defend it. Nothing in it stops a tenant from deleting the NetworkPolicy an hour later, unless you also turn on synchronization, and even then there is a reconcile window. Mature setups pair the two: `generate` so every namespace starts correct, and a `validate` rule so removing or weakening the object is rejected. ## How to answer Name the trigger and downstream, say inline `data` versus `clone` of a live source, say that it runs in the background under Kyverno's own identity rather than in the admission response, and land the framing: validate demands, generate supplies.

  • Which identity creates the generated object, and why does that matter?
    Kyverno's background controller creates it under its own ServiceAccount, not the requesting user's. Administrators extend that account's rights, usually with an aggregated ClusterRole, so a rule targeting a kind nobody granted silently produces nothing. It also means a tenant who can only create a namespace can cause a Secret to be materialised that they could never create directly, so treat the clone source as privileged.
  • Does a generate rule stop a tenant from deleting the NetworkPolicy afterwards?
    No. Generation is a creation event, not a defence. Turning on synchronization makes Kyverno recreate a deleted or edited downstream, but only after a reconcile window. If removal must actually be refused, pair the generate rule with a validate rule that rejects delete or update requests against that object.
  • The policy is installed into a cluster that already has 200 namespaces. Do they get the NetworkPolicy?
    Not by default - the rule fires on triggers that appear after it exists. Setting `generateExisting: true` makes Kyverno process matching objects that were already present when the policy was installed, which on a large estate means a burst of background work, so roll it out to a subset of namespaces first.

A validate rule is a bouncer turning people away for not wearing a badge. A generate rule is a receptionist who prints the badge and hands it to them on the way in.

saying these in an interview costs you the question

  • Says the generate rule blocks the request until the resource exists
  • Thinks the downstream is created with the requesting user's permissions
  • Assumes the downstream appears synchronously in the admission response
  • Believes generate removes the need for any validate rule
  • Cannot say what a trigger and a downstream resource are

context

open as a page

In a Kyverno generate rule, what does synchronize: true actually guarantee?

level: middleimportance: should knowfreq 56%

basics

~20 s

Kyverno keeps the generated copy in step with its source: hand edits to the downstream are reverted and changes to the rule body or the cloned source propagate. With synchronize false, the object is created once and never touched again.

open as a page

Deleting a Kyverno ClusterPolicy removed every NetworkPolicy it generated. Why, and how do you avoid that?

level: seniorimportance: should knowfreq 42%

basics

~20 s

Synchronized downstream resources are owned by the policy that generated them, so removing the policy garbage-collects the copies. Set generate.orphanDownstreamOnPolicyDelete to true before decommissioning, or turn synchronization off first, so the objects are left behind.

open as a page

How does a Kyverno cleanup policy remove completed Jobs, and when do you use a TTL label instead?

level: middleimportance: nice to knowfreq 31%

basics

~20 s

A Kyverno ClusterCleanupPolicy matches a kind, narrows it with conditions such as a succeeded status, and carries a cron schedule; the cleanup controller deletes every match on each tick. A cleanup.kyverno.io/ttl label instead expires one specific object.

open as a page