-
Notifications
You must be signed in to change notification settings - Fork 29
feat: adding new a trace command #74
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
Open
kushal9897
wants to merge
1
commit into
kyverno:main
Choose a base branch
from
kushal9897:trace
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
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 hidden or 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,134 @@ | ||
| # Kyverno Design Proposal (KDP): `kyverno trace` Command | ||
|
|
||
| ## What is the `trace` Command? | ||
|
|
||
| The `trace` command helps users understand **why a Kyverno policy worked or didn’t work** on a given resource. | ||
|
|
||
| It gives you a step-by-step breakdown: | ||
| - Which rules matched or didn’t | ||
| - What conditions were evaluated | ||
| - What CEL expressions returned | ||
| - Whether a Secret or ConfigMap was found | ||
| - If an attestor verified the image or not | ||
|
|
||
| This command is useful for **beginners**, **advanced users**, and **maintainers** to troubleshoot and understand policies better. | ||
|
|
||
| --- | ||
|
|
||
| ## Why Do We Need It? | ||
|
|
||
| Right now, if something goes wrong: | ||
| - You check logs | ||
| - You guess which rule failed | ||
| - You don’t know which CEL condition caused the problem | ||
|
|
||
| With `trace`, you just run a command — and Kyverno **tells you what happened and why**. | ||
|
|
||
| --- | ||
|
|
||
| ## How to Use It | ||
|
|
||
| ```bash | ||
| kyverno trace -p policy.yaml -r resource.yaml | ||
| ``` | ||
|
|
||
| This shows the evaluation of every rule in your policy against your resource. You can also control the output using flags. | ||
|
|
||
| --- | ||
|
|
||
| ## All Available Flags (Explained Simply) | ||
|
|
||
| | Flag | What it does | | ||
| |------|---------------| | ||
| | `-p`, `--policy` | Point to your policy file(s) or folder | | ||
| | `-r`, `--resource` | Point to the resource YAML(s) you want to test | | ||
| | `--focus` | Only trace one rule type: `mutate`, `validate`, `generate`, or `image` | | ||
| | `--output` | Show the result in `table` (default), `json`, or `yaml` | | ||
| | `--verbose` | Show full CEL expressions, evaluated values, and debug info | | ||
| | `--remove-color` | Print plain text without any color formatting | | ||
| | `--filter-result` | Only show results that are `pass`, `fail`, `skip`, or `error` | | ||
| | `--set` | Pass variables like `--set env=prod` if your policy needs them | | ||
| | `--values-file` | Provide values from a YAML/JSON file instead of `--set` | | ||
| | `--cluster` | Instead of giving a file, pull the resource directly from the Kubernetes cluster | | ||
| | `--watch` | (Future) Watch the resource live and keep tracing in real time like `kubectl get --watch` | | ||
|
|
||
| --- | ||
|
|
||
| ## Examples | ||
|
|
||
| ```bash | ||
| kyverno trace -p policy.yaml -r resource.yaml | ||
| ``` | ||
|
|
||
| Simple trace on a local file. | ||
|
|
||
| ```bash | ||
| kyverno trace -p policy.yaml -r resource.yaml --focus=image --verbose | ||
| ``` | ||
|
|
||
| Only show image verification logic and full CEL debug info. | ||
|
|
||
| ```bash | ||
| kyverno trace -p policy.yaml -r resource.yaml --filter-result=fail --output=json | ||
| ``` | ||
|
|
||
| Only show failed rules, in JSON format. | ||
|
|
||
| --- | ||
|
|
||
| ## Sample Output | ||
|
|
||
| ```plaintext | ||
| Policy: prod-policy | ||
| Rule: require-team-label | ||
| Matched: resource kind = Deployment | ||
| CEL: object.metadata.labels.team != "" → true | ||
| Passed | ||
|
|
||
| Rule: disallow-latest | ||
| CEL: image endsWith ":latest" → true | ||
| Failed - image uses ':latest' | ||
| ``` | ||
|
|
||
| For image verification: | ||
|
|
||
| ```plaintext | ||
| Image: nginx:1.23 | ||
| Attestor 1 (user@example.com) | ||
| CEL: request.userInfo.groups AnyIn ["prod"] → true | ||
| Signature verified → Attestor passed | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Why It Helps With Secrets | ||
|
|
||
| If your CEL or attestor logic uses `resource.Get(...)` to pull a Secret or ConfigMap, `trace` will show: | ||
| - Was the Secret found? | ||
| - Did the key exist? | ||
| - Did the expression return true or false? | ||
|
|
||
| This is extremely useful because many users struggle with dynamic Secret lookups. | ||
|
|
||
| --- | ||
|
|
||
| ## Who This Helps | ||
|
|
||
| | Role | Why it helps | | ||
| |------|---------------| | ||
| | Beginner | Understand why your policy didn’t apply | | ||
| | Advanced | Debug tricky CEL logic or image signatures | | ||
| | Maintainers | Save time answering “why did my policy fail?” questions | | ||
|
|
||
| --- | ||
|
|
||
| ## Final Note | ||
|
|
||
| This command will: | ||
| - Make Kyverno easier to use | ||
| - Reduce Slack questions | ||
| - Improve the CLI experience | ||
|
|
||
| It’s a game-changer for anyone writing or debugging policies. | ||
|
|
||
| Author: **Kushal Agrawal**, | ||
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.
Not sure why we have this section....are Secrets different than any other API call?