MutatingPolicy Resource Targeting#
Overview#
Kyverno supports two distinct MutatingPolicy resource kinds:
MutatingPolicy/NamespacedMutatingPolicy— CEL-based policy types fromgithub.com/kyverno/api/api/policies.kyverno.io/v1beta1. These are the newer, dedicated mutation-only resources.- Mutation rules inside
Policy/ClusterPolicy— The classic Kyverno v1 approach where mutation is one rule type (mutate) within a unified policy.
This article covers both. The CEL-based MutatingPolicy is the newer, purpose-built kind; the v1 Policy/ClusterPolicy remains the established approach for patch-based mutations.
CEL-based MutatingPolicy (policies.kyverno.io/v1beta1)#
Resource Kinds and Scope#
| Kind | Scope | Short name |
|---|---|---|
MutatingPolicy | Cluster-scoped | mpol |
NamespacedMutatingPolicy | Namespace-scoped | nmpol |
Both share the same MutatingPolicySpec (defined in v1alpha1, re-exported as type aliases in v1beta1) .
MutatingPolicySpec Fields#
Source: api/policies.kyverno.io/v1alpha1/mutating_policy.go . Note that v1beta1 re-exports MutatingPolicySpec and related types as aliases from v1alpha1 .
| Field | Type | Description |
|---|---|---|
matchConstraints | *admissionregistrationv1.MatchResources | Required. Specifies which admission requests (resources + operations) this policy evaluates. |
matchConditions | []admissionregistrationv1.MatchCondition | Optional CEL expressions that further filter matched requests. Max 64 conditions. |
targetMatchConstraints | *TargetMatchConstraints | Optional. Specifies target resources for mutateExisting operations. |
mutations | []admissionregistrationv1alpha1.Mutation | The mutation operations to apply (CEL-based patches). |
variables | []admissionregistrationv1.Variable | Named CEL expressions reusable in other fields. |
failurePolicy | *FailurePolicyType | Ignore or Fail (default: Fail). |
reinvocationPolicy | ReinvocationPolicyType | Never or IfNeeded. |
evaluation | *MutatingPolicyEvaluationConfiguration | Controls admission, background, and mutateExisting behavior. |
autogen | *MutatingPolicyAutogenConfiguration | Controls pod controller and MAP generation. |
webhookConfiguration | *WebhookConfiguration | Webhook timeout and related settings. |
matchConstraints (Admission Resource Targeting)#
matchConstraints maps directly to the Kubernetes admissionregistrationv1.MatchResources type . It contains:
resourceRules—[]NamedRuleWithOperations: specifiesapiGroups,apiVersions,resources,operations(CREATE, UPDATE, etc.), and optionalresourceNames.excludeResourceRules— same structure; resources matching these rules are excluded.namespaceSelector—*metav1.LabelSelector: filters by namespace labels.objectSelector—*metav1.LabelSelector: filters by object labels.matchPolicy—ExactorEquivalent(how API versions are matched).
This is identical to the matchConstraints field on a Kubernetes MutatingAdmissionPolicy.
targetMatchConstraints (MutateExisting Targeting)#
targetMatchConstraints is a TargetMatchConstraints struct that extends admissionregistrationv1.MatchResources with an additional CEL expression field :
type TargetMatchConstraints struct {
Expression string `json:"expression,omitempty"` // CEL expression for target selection
admissionregistrationv1.MatchResources `json:",inline"`
}
When spec.evaluation.mutateExisting.enabled is true, this field identifies existing resources to mutate. If targetMatchConstraints is absent, Kyverno uses matchConstraints to select existing targets .
matchConditions#
CEL expressions evaluated after resource matching. A request is only processed if all conditions evaluate to true. Useful for fine-grained filtering that can't be expressed with label selectors alone .
Evaluation Configuration#
spec.evaluation (MutatingPolicyEvaluationConfiguration) controls when the policy runs :
admission.enabled— defaulttrue; toggles admission-time mutation.background.enabled— defaulttrue; toggles background scan mutations.mutateExisting.enabled— defaultfalse; must be set totrueto activate existing-resource mutation viatargetMatchConstraints.skipBackgroundRequests— defaulttrue; skips requests from the background controller.
Classic Policy/ClusterPolicy Mutation Rules (kyverno.io/v1)#
In the v1 API, mutation is configured via a mutate block inside a Rule. The Rule.match field drives resource targeting.
MatchResources (Rule-level)#
Defined in match_resources_types.go. The MatchResources struct supports two patterns:
| Field | Description |
|---|---|
any | ResourceFilters — logical OR across filters |
all | ResourceFilters — logical AND across filters |
resources (inline ResourceDescription) | Deprecated direct spec; prefer any/all |
UserInfo (inline) | Deprecated; filter by requester roles, subjects |
The any/all pattern is preferred. any and all cannot be used simultaneously .
ResourceDescription Fields#
Defined in resource_description_types.go:
| Field | Description |
|---|---|
kinds | List of resource kinds |
names | Resource names (wildcard: *, ?); replaces deprecated name |
namespaces | Namespace names (wildcard supported); forbidden in namespaced policies |
annotations | Annotation key/value map (wildcard supported) |
selector | Label selector on the resource |
namespaceSelector | Label selector on the resource's namespace |
operations | CREATE, UPDATE, DELETE, CONNECT |
Mutation Targets (mutateExisting)#
For existing-resource mutation (outside of admission), the Mutation struct has a targets field :
Targets []TargetResourceSpec `json:"targets,omitempty"`
TargetResourceSpec (defined in resource_spec_types.go) contains:
| Field | Description |
|---|---|
ResourceSpec (inline) | apiVersion, kind, namespace, name, uid |
selector | *metav1.LabelSelector for label-based target selection |
context | Variables and data sources for rule execution |
preconditions | any/all conditions gating rule application to each target |
The presence of targets is what distinguishes a mutateExisting rule — r.Mutation.Targets != nil . For namespaced policies, target namespaces must match the policy's own namespace .
GenericPolicy Abstraction#
Both kinds are unified under the GenericPolicy interface in pkg/engine/api/policy.go. The engine calls AsMutatingPolicyLike() to retrieve a MutatingPolicy or NamespacedMutatingPolicy, and AsKyvernoPolicy() for v1 Policy/ClusterPolicy objects. Constructor functions NewMutatingPolicy, NewNamespacedMutatingPolicy, and NewMutatingPolicyFromLike wrap these types.
Key Source Files#
| File | Purpose |
|---|---|
github.com/kyverno/api/.../v1alpha1/mutating_policy.go | Canonical MutatingPolicySpec definition |
github.com/kyverno/api/.../v1beta1/mutating_policy.go | v1beta1 CRD types + interface implementations |
api/kyverno/v1/match_resources_types.go | v1 MatchResources |
api/kyverno/v1/resource_description_types.go | v1 ResourceDescription |
api/kyverno/v1/resource_spec_types.go | TargetResourceSpec for mutateExisting |
api/kyverno/v1/common_types.go | v1 Mutation struct |
api/kyverno/v1/rule_types.go | v1 Rule with match/exclude/mutate fields |
pkg/engine/api/policy.go | GenericPolicy interface and genericPolicy wrapper |