> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Data Source

> Explains embedding Kubernetes resource manifests in Kyverno generate rules to automatically create and synchronize downstream resources like NetworkPolicy, plus RBAC permission setup for Kyverno background controller.

In this lesson we show how to embed an entire Kubernetes resource manifest directly inside a Kyverno generate rule using the data source. This lets Kyverno create downstream resources (for example, a NetworkPolicy) automatically when a trigger event occurs (for example, creating a Namespace).

Why this matters: embedding the resource in the policy ensures the desired resource is created consistently and can be reconciled over time when you enable synchronization.

## Trigger vs Source — the two parts of a generate rule

Every generate rule has two distinct responsibilities:

* The trigger — when should the rule run? (Defined in `match` or via `existing` when reconciling existing resources.)
* The source — what should the rule create? (Either `data` for an embedded manifest or `clone` to copy an existing cluster resource.)

When you want Kyverno to create a new resource from a manifest embedded in the policy, you use the `data` source. Alternatively, `clone` copies an existing resource in the cluster. These two options are mutually exclusive — a rule can only use one.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/D-p9N2q-xD7686Bp/images/Prep-Course-Kyverno-Certified-Associate-KCA-Certification/Generate-Rules/Data-Source/alexs-plan-understanding-basics-triggers.jpg?fit=max&auto=format&n=D-p9N2q-xD7686Bp&q=85&s=42e8c0f3e69484429ecd30987462a270" alt="The image is an informational slide titled &#x22;Alex's Plan: Understanding the Basics,&#x22; discussing triggers for rule execution, either on admission of new resources or on existing resources." width="1920" height="1080" data-path="images/Prep-Course-Kyverno-Certified-Associate-KCA-Certification/Generate-Rules/Data-Source/alexs-plan-understanding-basics-triggers.jpg" />
</Frame>

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/D-p9N2q-xD7686Bp/images/Prep-Course-Kyverno-Certified-Associate-KCA-Certification/Generate-Rules/Data-Source/alexs-plan-data-clone-basics.jpg?fit=max&auto=format&n=D-p9N2q-xD7686Bp&q=85&s=fd8a871ee2806de3cf031fa256cec6c0" alt="The image is a slide from a presentation titled &#x22;Alex's Plan: Understanding the Basics,&#x22; explaining two concepts: &#x22;Data&#x22; (focusing on defining a resource inside a policy) and &#x22;Clone&#x22; (copying an existing resource from a cluster)." width="1920" height="1080" data-path="images/Prep-Course-Kyverno-Certified-Associate-KCA-Certification/Generate-Rules/Data-Source/alexs-plan-data-clone-basics.jpg" />
</Frame>

<Callout icon="warning" color="#FF6B6B">
  `data` and `clone` are mutually exclusive: a single generate rule must use either `data` (embedded manifest) or `clone` (copy from cluster), not both.
</Callout>

A common term in Kyverno documentation is downstream resource — this is the resource that Kyverno generates (for example, a ConfigMap, Secret, or NetworkPolicy). When we say Kyverno manages downstream resources, we mean the resources generated by generate rules.

## Generate rule anatomy

At a high level, a generate rule contains:

* `match` (or `existing`) — defines the trigger (the incoming resource that activates the rule).
* `generate` — describes the downstream resource identity (kind, apiVersion, name, namespace) and contains the source (`data` or `clone`).

Use the table below to quickly map concept to YAML location:

| Concept                                   | YAML location                           | Example                                                  |
| ----------------------------------------- | --------------------------------------- | -------------------------------------------------------- |
| Trigger (when)                            | `match` / `existing`                    | `match: resources: kinds: - Namespace`                   |
| Downstream resource identity (what/where) | `generate:` top-level fields            | `apiVersion: networking.k8s.io/v1` `kind: NetworkPolicy` |
| Source (resource content)                 | `generate.data:` (or `generate.clone:`) | `data:` with nested `spec`                               |

Here is a minimal example that demonstrates these parts (focus on the `rules` block):

```yaml theme={null}
rules:
  - name: create-networkpolicy-for-namespace
    # TRIGGER: run when a Namespace is created
    match:
      any:
        - resources:
            kinds:
              - Namespace

    # DOWNSTREAM RESOURCE identity and SOURCE:
    generate:
      kind: NetworkPolicy
      apiVersion: networking.k8s.io/v1
      name: deny-all-traffic
      namespace: "{{request.object.metadata.name}}"
      data:
        spec: ...
```

In this example:

* `match` triggers the rule when a Namespace is created.
* `generate` declares the NetworkPolicy to create and contains `data` for the NetworkPolicy spec.
* Note the use of `{{request.object.metadata.name}}` to place the generated NetworkPolicy in the newly created Namespace.

## Prerequisite: permissions for the Kyverno background controller

Generate rules are executed by Kyverno's background controller. By default, that controller only has a limited set of RBAC permissions. If you want Kyverno to create resource types not already allowed (for example, `NetworkPolicy`), grant the background controller the necessary permissions.

Kyverno supports cluster role aggregation. Instead of editing built-in Kyverno roles, create a separate ClusterRole that grants the required verbs for the resource and add the aggregation label `rbac.kyverno.io/aggregate-to-background-controller: "true"`. Kubernetes will merge (aggregate) these rules into the background controller role.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/D-p9N2q-xD7686Bp/images/Prep-Course-Kyverno-Certified-Associate-KCA-Certification/Generate-Rules/Data-Source/kyverno-networkpolicy-permissions-rbac-slide.jpg?fit=max&auto=format&n=D-p9N2q-xD7686Bp&q=85&s=aae5ce0b2f867f8430f557b5b6b2e96c" alt="The image is a slide explaining the prerequisite of granting permissions for Kyverno to create a NetworkPolicy, which involves setting RBAC permissions and creating a ClusterRole with a specific label." width="1920" height="1080" data-path="images/Prep-Course-Kyverno-Certified-Associate-KCA-Certification/Generate-Rules/Data-Source/kyverno-networkpolicy-permissions-rbac-slide.jpg" />
</Frame>

Create a ClusterRole like this (note: `apiGroups` uses the group `networking.k8s.io`, not `networking.k8s.io/v1`):

```yaml theme={null}
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: kyverno:generate-networkpolicies
  labels:
    rbac.kyverno.io/aggregate-to-background-controller: "true"
rules:
- apiGroups: ["networking.k8s.io"]
  resources: ["networkpolicies"]
  verbs: ["get", "list", "watch", "create", "update", "delete"]
```

<Callout icon="lightbulb" color="#1CB2FE">
  By adding the label `rbac.kyverno.io/aggregate-to-background-controller: "true"`, Kubernetes will aggregate these rules into Kyverno's background controller ClusterRole so you don't need to modify Kyverno's built-in roles.
</Callout>

## Complete example — create a default deny-all NetworkPolicy for new Namespaces

Below is a practical generate rule that creates a deny-all `NetworkPolicy` in every newly created Namespace. The `synchronize: true` field creates a durable link between the policy and the generated resource: when the policy changes, Kyverno will reconcile the generated resources to match the policy (preventing configuration drift).

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/D-p9N2q-xD7686Bp/images/Prep-Course-Kyverno-Certified-Associate-KCA-Certification/Generate-Rules/Data-Source/solving-alex-challenge-network-policy.jpg?fit=max&auto=format&n=D-p9N2q-xD7686Bp&q=85&s=0d9cbc17fe645231a010be70d350a310" alt="The image is about &#x22;Solving Alex's Challenge: Automating the Network Policy&#x22; and includes a character named Alex stating a plan to create a 'deny-all' network policy using a generate rule." width="1920" height="1080" data-path="images/Prep-Course-Kyverno-Certified-Associate-KCA-Certification/Generate-Rules/Data-Source/solving-alex-challenge-network-policy.jpg" />
</Frame>

Use this `rules` snippet in a policy:

```yaml theme={null}
rules:
  - name: default-deny-all-networkpolicy
    match:
      resources:
        kinds:
        - Namespace

    generate:
      synchronize: true
      apiVersion: networking.k8s.io/v1
      kind: NetworkPolicy
      name: default-deny-all
      namespace: "{{request.object.metadata.name}}"
      data:
        spec:
          podSelector: {}
          policyTypes:
          - Ingress
          - Egress
```

With this single policy rule, every new Namespace automatically receives the `default-deny-all` NetworkPolicy at creation time.

<Callout icon="warning" color="#FF6B6B">
  When you enable `synchronize: true`, the generated resources are continuously reconciled to the policy `data` block. If you plan to allow manual edits to generated resources, consider the implications carefully — synchronization will overwrite deviations.
</Callout>

## Quick recap

| Topic                | Key points                                                                                                                                              |
| -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Trigger (when)       | Defined in `match` (or `existing`); determines when the rule runs.                                                                                      |
| Source (what)        | `data` embeds the resource manifest inside the policy; `clone` copies an existing cluster resource.                                                     |
| Downstream resource  | The resource Kyverno creates (e.g., `NetworkPolicy`) as declared in the `generate` block.                                                               |
| Executor             | Kyverno background controller performs generation — ensure it has proper RBAC.                                                                          |
| Granting permissions | Create a `ClusterRole` with `rbac.kyverno.io/aggregate-to-background-controller: "true"` to aggregate permissions into Kyverno's background controller. |
| Synchronization      | `synchronize: true` keeps generated resources reconciled to policy `data`.                                                                              |

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/D-p9N2q-xD7686Bp/images/Prep-Course-Kyverno-Certified-Associate-KCA-Certification/Generate-Rules/Data-Source/policy-framework-trigger-source-downstream.jpg?fit=max&auto=format&n=D-p9N2q-xD7686Bp&q=85&s=d6448524f15dfe3b97920ba9c86cc75a" alt="The image is a summary diagram explaining three concepts: Trigger, Source, and Downstream Resource within a policy framework. Each concept is briefly defined with associated descriptions." width="1920" height="1080" data-path="images/Prep-Course-Kyverno-Certified-Associate-KCA-Certification/Generate-Rules/Data-Source/policy-framework-trigger-source-downstream.jpg" />
</Frame>

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/D-p9N2q-xD7686Bp/images/Prep-Course-Kyverno-Certified-Associate-KCA-Certification/Generate-Rules/Data-Source/kyverno-background-controller-summary-slide.jpg?fit=max&auto=format&n=D-p9N2q-xD7686Bp&q=85&s=2d7755ce70ff9ad58bb402922ed5ef02" alt="The image is a summary slide with points on the &#x22;Kyverno Background Controller&#x22; being responsible for executing generate rules and instructions on granting permissions for generation by creating a ClusterRole with a specific label." width="1920" height="1080" data-path="images/Prep-Course-Kyverno-Certified-Associate-KCA-Certification/Generate-Rules/Data-Source/kyverno-background-controller-summary-slide.jpg" />
</Frame>

## Next steps and further reading

* To learn more about `synchronize` behaviors, refer to the Kyverno docs on generate rules.
* For RBAC aggregation and ClusterRole design patterns, review Kubernetes RBAC aggregation documentation.
* Kyverno docs: [https://kyverno.io/docs/](https://kyverno.io/docs/)

With these fundamentals, you can author generate rules that embed resource blueprints directly in policies and automate consistent, cluster-wide resource creation. This sets the stage for more advanced generation scenarios (templating, conditional generation, or cloning existing resources).

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/kyverno-certified-associate/module/9024a647-736d-4de4-8b1a-d0efe638df18/lesson/f00fe73d-1e31-40ef-9066-9a601ecaf73a" />
</CardGroup>
