Skip to content

feat: add auditAnnotations to MutatingPolicySpec - #119

Merged
realshuting merged 2 commits into
mainfrom
realshuting/mpol-audit-annotations
Jul 23, 2026
Merged

realshuting merged 2 commits into
mainfrom
realshuting/mpol-audit-annotations

Conversation

@realshuting

Copy link
Copy Markdown
Member

What this PR does

Adds AuditAnnotations []admissionregistrationv1.AuditAnnotation to MutatingPolicySpec (v1alpha1, exposed through the v1beta1/v1 aliases).

The field carries CEL expressions that Kyverno evaluates after mutations are applied successfully; the results are surfaced as properties in policy report results (results[].properties). It is intentionally not projected into generated Kubernetes MutatingAdmissionPolicy resources, since native MAP has no equivalent.

spec:
  mutations:
  - patchType: ApplyConfiguration
    applyConfiguration:
      expression: >-
        Object{metadata: Object.metadata{labels: {"env": "test"}}}
  auditAnnotations:
  - key: resource-name
    valueExpression: "'cm/' + string(object.metadata.name)"

Why

Migration parity for the legacy ClusterPolicy spec.rules[].reportProperties field (cpol mutate → mpol migration, kyverno/kyverno#16504). This mirrors the field already present on ValidatingPolicySpec, ImageValidatingPolicySpec and — as of #108/kyverno/kyverno#16505 — GeneratingPolicySpec.

Related

Changes

  • api/policies.kyverno.io/v1alpha1/mutating_policy.go: new optional AuditAnnotations field on MutatingPolicySpec
  • api/policies.kyverno.io/v1alpha1/zz_generated.deepcopy.go: regenerated (make deepcopy-gen)

Add AuditAnnotations []admissionregistrationv1.AuditAnnotation to
MutatingPolicySpec, following the same pattern used by ValidatingPolicy,
ImageValidatingPolicy and GeneratingPolicy.

The field carries CEL expressions evaluated by Kyverno after mutations
are applied successfully; results are surfaced as properties in policy
report results. It provides migration parity for the legacy ClusterPolicy
spec.rules[].reportProperties field.

Ref: kyverno/kyverno#16719
Signed-off-by: realshuting <shuting@nirmata.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds spec.auditAnnotations support to MutatingPolicySpec so Kyverno can evaluate CEL expressions after successful mutations and surface the evaluated values as policy report result properties, aligning mutate policies with other CEL-based policy types.

Changes:

  • Add optional AuditAnnotations []admissionregistrationv1.AuditAnnotation to MutatingPolicySpec (v1alpha1; aliased through v1beta1/v1).
  • Regenerate deepcopy code to copy the new AuditAnnotations slice.

Reviewed changes

Copilot reviewed 1 out of 2 changed files in this pull request and generated 1 comment.

File Description
api/policies.kyverno.io/v1alpha1/mutating_policy.go Introduces the new AuditAnnotations field on MutatingPolicySpec with documentation.
api/policies.kyverno.io/v1alpha1/zz_generated.deepcopy.go Updates autogenerated deepcopy logic to copy AuditAnnotations.
Files not reviewed (1)
  • api/policies.kyverno.io/v1alpha1/zz_generated.deepcopy.go: Generated file

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread api/policies.kyverno.io/v1alpha1/mutating_policy.go
…artifact on CI failure

- Regenerate CRDs, helm chart CRDs and API docs for the new
  MutatingPolicySpec auditAnnotations field (make codegen).
- Port the kyverno/kyverno codegen CI improvement: on verify-codegen
  failure, upload the regenerated diff as a patch artifact so
  contributors can 'git apply' the fix instead of re-running codegen
  locally.

Signed-off-by: realshuting <shuting@nirmata.com>
@realshuting realshuting reopened this Jul 23, 2026
@realshuting
realshuting marked this pull request as ready for review July 23, 2026 08:54
@realshuting
realshuting enabled auto-merge (squash) July 23, 2026 08:54
@realshuting
realshuting disabled auto-merge July 23, 2026 09:03
@realshuting
realshuting force-pushed the realshuting/mpol-audit-annotations branch from d83cd20 to ffdc0e8 Compare July 23, 2026 09:07
@realshuting
realshuting merged commit fb27857 into main Jul 23, 2026
10 checks passed
@realshuting
realshuting deleted the realshuting/mpol-audit-annotations branch July 23, 2026 09:08
realshuting added a commit to realshuting/kyverno that referenced this pull request Jul 23, 2026
Point the api dependency at the kyverno/api main commit that includes
the MutatingPolicySpec auditAnnotations field (kyverno/api#119, merged
as fb27857), replacing the PR-branch pseudo-version.

Signed-off-by: realshuting <shuting@nirmata.com>
realshuting added a commit to kyverno/kyverno that referenced this pull request Jul 24, 2026
* feat(mpol): add auditAnnotations support to MutatingPolicy

Add report properties support to MutatingPolicy following the
ValidatingPolicy/ImageValidatingPolicy/GeneratingPolicy auditAnnotations
pattern, providing migration parity for the legacy ClusterPolicy
spec.rules[].reportProperties field.

Changes:
- api: bump github.com/kyverno/api to include AuditAnnotations
  []admissionregistrationv1.AuditAnnotation on MutatingPolicySpec
- mpol compiler: compile auditAnnotations CEL programs with the shared
  compiler.CompileAuditAnnotations; store in the compiled Policy
- mpol compiler evaluate: add evaluateAuditAnnotations that evaluates
  each key's valueExpression after all mutations apply successfully
  (policy variables and the patched object in scope); empty results are
  omitted; results returned via EvaluationResult.AuditAnnotations
- mpol engine: pass evaluated annotations into the success RulePass
  response properties so they surface in mutation policy report results;
  covers both the admission path and the mutate-existing/background path
  through the shared handlePolicy
- mpol validate: include auditAnnotation valueExpressions in the HTTP
  expression scan for namespaced policy validation
- codegen: regenerate mpol/nmpol CRDs and CLI-embedded CRDs
- tests: compiler/evaluation/engine unit tests and a chainsaw E2E test
  asserting report result properties

Note: auditAnnotations is Kyverno-report-only and intentionally not
projected into generated Kubernetes MutatingAdmissionPolicy resources.

Closes: #16719
Ref: #16504

Signed-off-by: realshuting <shuting@nirmata.com>

* chore: bump github.com/kyverno/api to merged main commit

Point the api dependency at the kyverno/api main commit that includes
the MutatingPolicySpec auditAnnotations field (kyverno/api#119, merged
as fb27857), replacing the PR-branch pseudo-version.

Signed-off-by: realshuting <shuting@nirmata.com>

* chore: regenerate CRD reference docs for auditAnnotations

Apply the codegen-docs-patch artifact from the Verify codegen (docs) CI
job; the api dependency bump required codegen-all-docs in addition to
codegen-all-code (make codegen-api-bump covers both).

Signed-off-by: realshuting <shuting@nirmata.com>

---------

Signed-off-by: realshuting <shuting@nirmata.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants