-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add audit mode and reporting in mutate and generate rules #63
Open
vishal-chdhry
wants to merge
2
commits into
kyverno:main
Choose a base branch
from
vishal-chdhry:reports-mutate-generate
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
# Meta | ||
[meta]: #meta | ||
- Name: Audit mode and reporting in mutate and generate rules | ||
- Start Date: 12 August 2024 | ||
- Update data (optional): 12 August 2024 | ||
- Author(s): vishal-chdhry | ||
|
||
# Table of Contents | ||
[table-of-contents]: #table-of-contents | ||
- [Meta](#meta) | ||
- [Table of Contents](#table-of-contents) | ||
- [Overview](#overview) | ||
- [Motivation](#motivation) | ||
- [Proposal](#proposal) | ||
- [Implementation](#implementation) | ||
- [Link to the Implementation PR](#link-to-the-implementation-pr) | ||
- [Drawbacks](#drawbacks) | ||
- [Alternatives](#alternatives) | ||
- [Unresolved Questions](#unresolved-questions) | ||
|
||
# Overview | ||
[overview]: #overview | ||
This proposal discusses ways to capture mutate and generate rule results in existing policy reports. | ||
|
||
# Motivation | ||
[motivation]: #motivation | ||
|
||
Currently, reporting is only supported in validate rules. While this is nice in that it allows a report to be generated showing the cluster resources that violate the validation rules, it doesn't show the resources that would have either been mutated or generated by Kyverno policy. As an operator, administrator, or manager who may have multiple clusters under management, this makes it impossible to know if the mutate or generate rules about to be pushed out are safe and effective. | ||
|
||
# Proposal | ||
|
||
The `properties` field in policy report result is used to add information regarding mutation and generation. A `type` property is used to indicate if the report is for mutate, validate or generate. | ||
For mutate rules, the policy report should indicate, minimally,: | ||
- jsonpatch of the mutation | ||
- target of the mutation | ||
|
||
The report results will have a property called `patches` which has 2 properties: `jsonpatches` and `target`, `target` is the resource that is being mutated and `jsonpatches` are the patches applied to the target. | ||
```json | ||
{ | ||
"patches": [ | ||
{ | ||
"target": { | ||
"apiVersion": "v1", | ||
"kind": "Pod", | ||
"namespace": "default", | ||
"name": "sample-resource", | ||
"uid": "bb228314-0e3e-42c2-b945-63efe9279ad4" | ||
}, | ||
"jsonpatches": [ | ||
{ | ||
"op": "replace", | ||
"path": "/baz", | ||
"value": "boo" | ||
}, | ||
{ | ||
"op": "add", | ||
"path": "/hello", | ||
"value": ["world"] | ||
}, | ||
{ | ||
"op": "remove", | ||
"path": "/foo" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
``` | ||
|
||
Example: | ||
```yaml | ||
apiVersion: wgpolicyk8s.io/v1alpha2 | ||
kind: PolicyReport | ||
metadata: | ||
name: sample-mutation-policy-report | ||
summary: | ||
pass: 8 | ||
fail: 2 | ||
warn: 0 | ||
error: 0 | ||
skip: 0 | ||
results: | ||
- policy: mutation-policy | ||
message: resource `sample-resource` mutated | ||
result: pass | ||
rule: mutation-rule | ||
scored: true | ||
severity: medium | ||
source: kyverno | ||
timestamp: | ||
nanos: 0 | ||
seconds: 1681659534 | ||
properties: | ||
type: mutation | ||
jsonpatch: '[{ "op": "replace", "path": "/baz", "value": "boo" },{ "op": "add", "path": "/hello", "value": ["world"] },{ "op": "remove", "path": "/foo" }]' | ||
- policy: mutate-target-policy | ||
message: resource `sample-resource` mutated | ||
result: pass | ||
rule: mutation-target-rule | ||
scored: true | ||
severity: medium | ||
source: kyverno | ||
timestamp: | ||
nanos: 0 | ||
seconds: 1681659534 | ||
properties: | ||
type: mutation | ||
patches: '[{"target":{"apiVersion": "v1", "kind": "Pod", "namespace": "default", "name": "sample-resource", "uid": "bb228314-0e3e-42c2-b945-63efe9279ad4"},"jsonPatches": [{ "op": "replace", "path": "/baz", "value": "boo" },{ "op": "add", "path": "/hello", "value": ["world"] },{ "op": "remove", "path": "/foo" }]}]' | ||
``` | ||
|
||
For generate rules: | ||
- List of resources that were generated by the policy | ||
Example: | ||
```yaml | ||
apiVersion: wgpolicyk8s.io/v1alpha2 | ||
kind: PolicyReport | ||
metadata: | ||
name: sample-generate-policy-report | ||
summary: | ||
pass: 8 | ||
fail: 2 | ||
warn: 0 | ||
error: 0 | ||
skip: 0 | ||
results: | ||
- policy: generate-policy | ||
message: resource `sample-resource` mutated | ||
result: pass | ||
rule: generate-rule | ||
scored: true | ||
severity: medium | ||
source: kyverno | ||
timestamp: | ||
nanos: 0 | ||
seconds: 1681659534 | ||
properties: | ||
type: generation | ||
resources: '[{"apiVersion": "v1", "kind": "Pod", "namespace": "default", "name": "sample-resource", "uid": "bb228314-0e3e-42c2-b945-63efe9279ad4"}]' | ||
``` | ||
|
||
# Implementation | ||
|
||
This is the technical portion of the KDP, where you explain the design in sufficient detail. | ||
|
||
The section should return to the examples given in the previous section, and explain more fully how the detailed proposal makes those examples work. | ||
|
||
## Link to the Implementation PR | ||
|
||
# Drawbacks | ||
|
||
Changes the structure of how we manages engine response and patches internally for mutation and resource generation in generate. | ||
|
||
# Alternatives | ||
|
||
For mutation, we can show the final resource in the reports instead of the jsonpatches, as the jsonpatch can get overwritten by other rules | ||
|
||
# Unresolved Questions | ||
|
||
- Currently, when the policy is deleted, the report for that policy also gets deleted. When OrphanDownstreamOnPolicyDelete is set to true, the resource stays after the policy is deleted, should the report also get orphaned when OrphanDownstreamOnPolicyDelete is `true`? | ||
- Should the report for mention details such as | ||
For generate: | ||
- Has the resource has been cloned from a list? | ||
- Is it being synchronised? | ||
For mutate: | ||
- Is it a mutate existing rule? |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The target is only applicable for mutate existing rules.