Sub topic 14
Exceptions, exclusions and namespace scoping
This chapter assumes you can write a basic Kyverno ClusterPolicy and a Gatekeeper Constraint, and that you know how enforcement and audit modes differ. Real clusters always have workloads that break a rule for a good reason. System components, CNI plugins and monitoring agents often cannot meet a policy written for application teams. You need a controlled way to carve out those cases without turning the policy off.
Three levels of carve-out
Policy engines let you narrow a policy at three levels:
- Scope: the policy never targets certain namespaces or objects.
- Exclusion: the policy targets a broad set, then removes specific matches.
- Exception: a separate, reviewable object grants one workload a pass.
Prefer the narrowest level that solves the problem.
Scoping in Kyverno
Kyverno rules use match to select resources and exclude to remove some of them. This policy requires the team label everywhere except in system namespaces.
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: require-team-label
spec:
validationFailureAction: Enforce
background: true
rules:
- name: check-team-label
match:
any:
- resources:
kinds:
- Pod
exclude:
any:
- resources:
namespaces:
- kube-system
- monitoring
validate:
message: "The label 'team' is required."
pattern:
metadata:
labels:
team: "?*"Kyverno also offers a PolicyException resource. It lets a platform team grant an exemption for a named rule without editing the policy. You can restrict who may create these objects with RBAC, and every exemption stays visible in the cluster. Recent Kyverno versions disable PolicyException unless you enable it at install time, and you can limit it to one namespace.
Scoping in Gatekeeper
Gatekeeper puts scoping in the Constraint's match block. Besides kinds, you can use namespaces, excludedNamespaces, namespaceSelector and labelSelector.
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sRequiredLabels
metadata:
name: pods-must-have-team
spec:
enforcementAction: deny
match:
kinds:
- apiGroups: [""]
kinds: ["Pod"]
excludedNamespaces: ["kube-system", "monitoring"]
parameters:
labels: ["team"]Gatekeeper also has a cluster-wide Config resource. Its match section with excludedNamespaces can exempt namespaces from specific processes, such as audit, webhook and sync. This is separate from per-Constraint scoping.
Exclusion mechanisms compared
| Need | Kyverno | Gatekeeper |
|---|---|---|
| Skip a namespace in one policy | `exclude` in the rule | `excludedNamespaces` in the Constraint |
| Select by namespace label | `namespaceSelector` in `match` | `namespaceSelector` in `match` |
| Skip a namespace everywhere | Webhook `namespaceSelector` or resource filters | `Config` exempt namespaces |
| Per-workload exemption | `PolicyException` | Label selector on the Constraint |
Common mistakes
- Over-broad exclusions. Excluding a whole namespace also exempts every workload that someone later deploys there.
- Forgetting audit. An excluded resource is not reported as a violation. Keep an inventory of exclusions and review it.
- Exempting the engine itself. Policy engine namespaces should be excluded from webhooks so a failure cannot block its own recovery. The next chapter covers this.
Exercise
A monitoring agent in the monitoring namespace needs to run Pods without a team label. You want the exemption to be reviewable and removable without editing the shared policy. Which Kyverno feature fits, and why is it better than adding the namespace to exclude?
Show answer
Use a PolicyException. It is a separate object that names the policy and rule it exempts, so you can control it with RBAC, review it in version control and delete it without changing the policy. An exclude entry changes the shared policy and exempts every resource in that namespace.