Cluster-Scoped Resources
Kyverno reports violations on namespaced resources in PolicyReports and violations on cluster-scoped resources, such as Namespace, ClusterRole, ClusterRoleBinding, StorageClass, or CustomResourceDefinition, in ClusterPolicyReports. Starting with nirmata-agent v0.2.11, the Remediator Agent reads ClusterPolicyReports as well. It finds the manifest of each failing cluster-scoped resource in Git and opens a pull request that fixes it, just like it does for Deployments and other namespaced workloads.
This page explains how cluster-scoped resources are attributed to a repository, how to enable them in localCluster mode, and how to verify the setup end to end.
How Cluster-Scoped Resources Are Attributed
A cluster-scoped resource has no namespace, so the agent can’t use the namespace to find the repository that owns it. Each environment mode attributes it differently:
| Mode | How a cluster-scoped resource is matched to a repository |
|---|---|
localCluster | Repo-mapping entries with "clusterScoped": true (opt-in, see below) |
argoHub | Resources with an empty namespace in the Application’s status.resources. An Application without destination.namespace is remediated if it manages cluster-scoped resources |
fluxHub (Kustomization) | Cluster-scoped entries in the Kustomization’s status.inventory, or, without an inventory, the kustomize.toolkit.fluxcd.io/name and kustomize.toolkit.fluxcd.io/namespace labels on the live object. A Kustomization without targetNamespace remediates only cluster-scoped resources |
fluxHub (HelmRelease) | Live objects labelled helm.toolkit.fluxcd.io/name and helm.toolkit.fluxcd.io/namespace with the release’s name and namespace |
Label-based attribution reads the live object, so the agent needs get on that resource type in the target cluster.
Prerequisites
nirmata-agent v0.2.11 or later, configured as described in Getting Started and Configuration.
Kyverno 1.10 or later, which writes per-resource ClusterPolicyReports (
wgpolicyk8s.io/v1alpha2). The aggregated ClusterPolicyReports of older Kyverno versions, which list several resources per report, are not supported: their violations are not remediated.The agent can list ClusterPolicyReports. The nirmata-agent chart grants this by default. Without it, a collector that also has a namespace still remediates its namespaced resources and says in its
ViolationsCollectedcondition that cluster-scoped resources were skipped. A cluster-scoped-only collector fails.Kyverno’s reports controller can read the cluster-scoped kinds you want reported. By default it can’t list or watch kinds such as
ClusterRoleandClusterRoleBinding, so it never writes reports for them and the agent has nothing to remediate. Kyverno logsreports-controller is missing RBAC to list/watch this resourcewhen this happens. Grant access through the Kyverno Helm chart:# kyverno-values.yaml reportsController: rbac: clusterRole: extraResources: - apiGroups: ["rbac.authorization.k8s.io"] resources: ["clusterroles", "clusterrolebindings"] verbs: ["get", "list", "watch"] # Add any other cluster-scoped kinds your policies cover, for example: # - apiGroups: ["storage.k8s.io"] # resources: ["storageclasses"] # verbs: ["get", "list", "watch"]# Pin the chart version you already run, so the upgrade changes only the RBAC. KYVERNO_CHART_VERSION=$(helm list -n kyverno -f '^kyverno$' -o json | jq -r '.[0].chart' | sed 's/^kyverno-//') helm upgrade kyverno kyverno/kyverno -n kyverno --version "$KYVERNO_CHART_VERSION" \ --reuse-values -f kyverno-values.yaml # Restart the reports controller so it starts watching the new kinds. kubectl -n kyverno rollout restart deploy/kyverno-reports-controllerWithout
--version, Helm upgrades to the newest chart in your repository index, which may be a new Kyverno minor version (see the note on Kyverno 1.20).Namespaceis reported without extra RBAC.
Enable in Local Cluster Mode
In localCluster mode, cluster-scoped remediation is opt-in for each entry of the repo-namespace mapping ConfigMap. Add "clusterScoped": true to the entry whose repository, branch, and path hold your cluster-scoped manifests:
apiVersion: v1
kind: ConfigMap
metadata:
name: repo-namespace-mapping
namespace: nirmata
data:
mapping: |
[
{
"repo": "https://github.com/your-org/platform",
"branch": "main",
"path": "cluster",
"targetNamespace": "",
"clusterScoped": true
},
{
"repo": "https://github.com/your-org/platform",
"branch": "main",
"path": "apps",
"targetNamespace": "team-a"
}
]
| Field | Behavior |
|---|---|
"clusterScoped": true | The entry also reads ClusterPolicyReports. Every failing cluster-scoped resource whose manifest is found under the entry’s repo/branch/path is remediated there |
"targetNamespace": "" | Allowed only together with "clusterScoped": true. The entry then remediates only cluster-scoped resources |
targetNamespace set and clusterScoped: true | The entry remediates both its namespace and the cluster-scoped resources under its path |
No clusterScoped | Unchanged: the entry reads only the namespaced PolicyReports of targetNamespace |
A failing cluster-scoped resource whose manifest is not under any clusterScoped entry’s path is skipped. No pull request is opened for it.
Overlapping entries
Only one clusterScoped entry may cover a given manifest, so two collectors never open pull requests for the same resource. Two clusterScoped entries overlap when:
- they have the same
repo, ignoring a trailing/or.git; - they have the same
branch(an empty branch meansmain); and - one
pathequals or contains the other (an emptypathis the repository root).
The first entry in the list keeps cluster-scoped remediation. Each later overlapping entry is reported as a build error in the ProcessingTargetInformation condition and in lastRunSummary.errors, for example:
1 collector(s) failed to build: mapping 2 for repo https://github.com/your-org/platform.git: clusterScoped
overlaps the earlier clusterScoped mapping for https://github.com/your-org/platform branch "main" path
"cluster"; cluster-scoped resources are remediated only by the earlier mapping
The overlapping entry is still built for its targetNamespace, if it has one. If its targetNamespace is empty, it is skipped.
Verify the Setup
1. Check that Kyverno reports the resources
kubectl get clusterpolicyreports \
-o custom-columns=KIND:.scope.kind,NAME:.scope.name,PASS:.summary.pass,FAIL:.summary.fail
Each failing cluster-scoped resource has a row with FAIL greater than 0:
KIND NAME PASS FAIL
ClusterRole demo-orphan 0 1
ClusterRole demo-reader 0 1
ClusterRoleBinding demo-reader-binding 0 1
Namespace team-a 0 1
Namespace team-b 0 1
This output is from the example walkthrough below.
If the ClusterRole or ClusterRoleBinding rows are missing, Kyverno’s reports controller lacks RBAC for those kinds. See Prerequisites.
2. Check the Remediator run
Creating or updating the Remediator resource starts a run right away. Editing the repo-namespace mapping ConfigMap does not: the change is picked up on the next scheduled run. To start a run now, restart the agent:
kubectl -n nirmata rollout restart deploy/nirmata-agent
After the run, check the summary and the per-collector conditions:
kubectl -n nirmata get remediator <name> -o jsonpath='{.status.lastRunSummary}{"\n"}'
kubectl -n nirmata get remediator <name> -o json \
| jq -r '.status.conditions[] | "\(.type) \(.status) [\(.collectorId // "run")] \(.message)"'
A cluster-scoped-only entry shows up as a collector whose ID has an empty namespace, for example local-cluster//https://github.com/your-org/platform/cluster/main. Its ViolationsCollected condition reports the violations it collected and the files it mapped.
Failing cluster-scoped resources that no collector of the run selected are listed in a run-level ViolationsCollected condition (shown as [run] above, because it has no collector ID).
The agent logs show which ClusterPolicyReports each collector selected:
kubectl -n nirmata logs deploy/nirmata-agent | grep 'ClusterPolicyReports for'
Selected 5 of 5 ClusterPolicyReports for local-cluster//https://github.com/your-org/platform/cluster/main; 0 failing cluster-scoped resource(s) not attributed
3. Review the pull requests
The pull request for a cluster-scoped resource looks like any other remediation PR. Its description marks the resource as cluster-scoped:
**Namespace:** (cluster-scoped)
**File:** cluster/namespaces.yaml
The diff changes only what the policy requires, for example adding the missing owner, cost-center, or team labels in the walkthrough below.
4. Confirm the fix
After the pull request is merged and the change is deployed (by your GitOps tool, or with kubectl apply in localCluster mode), Kyverno re-evaluates the resource and its ClusterPolicyReport row turns to PASS.
On the next run, resources that haven’t changed are not remediated again. Their RemediationRecords show Skipped, and no new pull requests are opened.
Example Walkthrough
The following end-to-end example uses a localCluster agent and one Git repository. It shows both a cluster-scoped mapping and a namespaced control case.
Repository layout:
policies/ ClusterPolicies (two for cluster-scoped kinds, one namespaced control)
cluster/ namespaces.yaml (team-a, team-b), rbac.yaml (ClusterRole + ClusterRoleBinding)
apps/ deployment.yaml (Deployment web in team-a)
Policies and violations:
| Resource | Scope | Policy | Violation |
|---|---|---|---|
Namespace/team-a, Namespace/team-b | Cluster | require-namespace-owner | Missing owner or cost-center label |
ClusterRole/demo-reader | Cluster | require-rbac-team-label | Missing team label |
ClusterRoleBinding/demo-reader-binding | Cluster | require-rbac-team-label | Missing team label |
ClusterRole/demo-orphan | Cluster | require-rbac-team-label | Missing team label; its manifest is not under cluster/ |
Deployment/web (namespace team-a) | Namespaced | require-deployment-team-label | Missing team label |
Mapping: the two entries shown in Enable in Local Cluster Mode, with path: cluster (cluster-scoped only) and path: apps (namespace team-a).
Expected results:
| Scenario | Result |
|---|---|
Namespaces, ClusterRole, and ClusterRoleBinding under cluster/ | Pull requests add the missing labels; each description shows Namespace: (cluster-scoped) |
Deployment under apps/ | Pull request adds the team label (namespaced remediation is unchanged) |
ClusterRole/demo-orphan, whose manifest is outside every clusterScoped path | Skipped; no pull request |
| Second run with nothing changed | RemediationRecords show Skipped; no new pull requests |
| Pull requests merged and applied | All mapped resources pass; only demo-orphan still fails |
A third entry for the same repository with "path": "" and clusterScoped: true | Rejected with the overlap build error; no duplicate pull requests |
Kyverno 1.20 and kyverno.io/v1 policies
Kyverno 1.20 no longer runs legacykyverno.io/v1 policy types such as ClusterPolicy. If you write the example policies as ClusterPolicy, use Kyverno 1.19 or earlier, or write them as ValidatingPolicy. The Remediator Agent supports both.Troubleshooting
| Symptom | Cause and fix |
|---|---|
No ClusterPolicyReport rows for ClusterRole, ClusterRoleBinding, or another cluster-scoped kind | Kyverno’s reports controller lacks RBAC for that kind. Add it under reportsController.rbac.clusterRole.extraResources and restart kyverno-reports-controller |
| A cluster-scoped violation gets no pull request | Its manifest is not under any clusterScoped: true entry’s repo/branch/path. Set logging.verbosity: 2 in the agent’s Helm values and look for No file found for resource <Kind>/<name> in the agent logs |
A collector’s ViolationsCollected condition says cluster-scoped resources were skipped | The agent can’t list ClusterPolicyReports. Check that the agent’s ClusterRole grants get and list on clusterpolicyreports in the wgpolicyk8s.io API group, and that the ClusterPolicyReport CRD is installed |
ProcessingTargetInformation shows clusterScoped overlaps the earlier clusterScoped mapping | Two clusterScoped entries cover the same path of the same repository and branch. Narrow the paths, or remove clusterScoped from one entry |
| A failed run is fixed, but the resource is not retried | Failed resources are retried only after they change. To force a retry, first merge or close the collector’s open remediation pull requests, then delete its RemediationRecord and restart the agent: kubectl -n nirmata delete remediationrecords <name> and kubectl -n nirmata rollout restart deploy/nirmata-agent. Deleting the record clears the state of every resource of that collector, so any resource that still fails gets a new pull request, even if one is already open |
Known limitation: several violating resources in one file
In AI remediation mode, if one file holds several violating resources (for example two Namespaces in namespaces.yaml), the agent can open one pull request per resource, each fixing the whole file. Merge one of them and close the others, or keep one violating resource per file. This applies to namespaced resources as well.
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.