Documentskyverno
MutatingPolicy Resource Targeting
MutatingPolicy Resource Targeting
Type
Topic
Status
Published
Created
Jul 7, 2026
Updated
Jul 7, 2026
Created by
Dosu Bot
Updated by
Dosu Bot

MutatingPolicy Resource Targeting#

Overview#

Kyverno supports two distinct MutatingPolicy resource kinds:

  1. MutatingPolicy / NamespacedMutatingPolicy — CEL-based policy types from github.com/kyverno/api/api/policies.kyverno.io/v1beta1. These are the newer, dedicated mutation-only resources.
  2. 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#

KindScopeShort name
MutatingPolicyCluster-scopedmpol
NamespacedMutatingPolicyNamespace-scopednmpol

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 .

FieldTypeDescription
matchConstraints*admissionregistrationv1.MatchResourcesRequired. Specifies which admission requests (resources + operations) this policy evaluates.
matchConditions[]admissionregistrationv1.MatchConditionOptional CEL expressions that further filter matched requests. Max 64 conditions.
targetMatchConstraints*TargetMatchConstraintsOptional. Specifies target resources for mutateExisting operations.
mutations[]admissionregistrationv1alpha1.MutationThe mutation operations to apply (CEL-based patches).
variables[]admissionregistrationv1.VariableNamed CEL expressions reusable in other fields.
failurePolicy*FailurePolicyTypeIgnore or Fail (default: Fail).
reinvocationPolicyReinvocationPolicyTypeNever or IfNeeded.
evaluation*MutatingPolicyEvaluationConfigurationControls admission, background, and mutateExisting behavior.
autogen*MutatingPolicyAutogenConfigurationControls pod controller and MAP generation.
webhookConfiguration*WebhookConfigurationWebhook timeout and related settings.

matchConstraints (Admission Resource Targeting)#

matchConstraints maps directly to the Kubernetes admissionregistrationv1.MatchResources type . It contains:

  • resourceRules[]NamedRuleWithOperations: specifies apiGroups, apiVersions, resources, operations (CREATE, UPDATE, etc.), and optional resourceNames.
  • excludeResourceRules — same structure; resources matching these rules are excluded.
  • namespaceSelector*metav1.LabelSelector: filters by namespace labels.
  • objectSelector*metav1.LabelSelector: filters by object labels.
  • matchPolicyExact or Equivalent (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 — default true; toggles admission-time mutation.
  • background.enabled — default true; toggles background scan mutations.
  • mutateExisting.enabled — default false; must be set to true to activate existing-resource mutation via targetMatchConstraints.
  • skipBackgroundRequests — default true; 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:

FieldDescription
anyResourceFilters — logical OR across filters
allResourceFilters — 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:

FieldDescription
kindsList of resource kinds
namesResource names (wildcard: *, ?); replaces deprecated name
namespacesNamespace names (wildcard supported); forbidden in namespaced policies
annotationsAnnotation key/value map (wildcard supported)
selectorLabel selector on the resource
namespaceSelectorLabel selector on the resource's namespace
operationsCREATE, 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:

FieldDescription
ResourceSpec (inline)apiVersion, kind, namespace, name, uid
selector*metav1.LabelSelector for label-based target selection
contextVariables and data sources for rule execution
preconditionsany/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#

FilePurpose
github.com/kyverno/api/.../v1alpha1/mutating_policy.goCanonical MutatingPolicySpec definition
github.com/kyverno/api/.../v1beta1/mutating_policy.gov1beta1 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