-
Notifications
You must be signed in to change notification settings - Fork 135
docs: Add docs for tracking policy activity #2449
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
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
ca4a5a5
docs: Add docs for tracking policy activity
tianfeng92 4308962
chore: nitpicking
tianfeng92 a0b9f25
chore: nitpicking
tianfeng92 a993a09
chore: remove configuration part since it is impossible for customer
tianfeng92 a4a1e6a
docs: add more query examples
tianfeng92 58b98c6
docs: add more detailed query samples
tianfeng92 ffdfe3e
chore: polish the sample query
tianfeng92 e4b1817
Update calico-enterprise/observability/elastic/policy-activity.mdx
tianfeng92 6b0e53a
Update calico-enterprise/observability/elastic/policy-activity.mdx
tianfeng92 cedf0c4
Update calico-enterprise/observability/elastic/policy-activity.mdx
tianfeng92 c1d4711
Update calico-enterprise/observability/elastic/policy-activity.mdx
tianfeng92 359ce91
Update calico-enterprise/observability/elastic/policy-activity.mdx
tianfeng92 4686146
Update calico-enterprise/observability/elastic/policy-activity.mdx
tianfeng92 006003d
docs: rewrite according to suggestions
tianfeng92 6a88102
docs: add limitations
tianfeng92 d17e2d9
chore: format note section
tianfeng92 289817a
chore: nit
tianfeng92 a24b0d8
chore: describe generation
tianfeng92 e50743a
Update calico-enterprise/observability/elastic/policy-activity.mdx
tianfeng92 46f9e42
fix: set max size of the returned log
tianfeng92 868a562
docs: add notes for query large size of data
tianfeng92 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
Some comments aren't visible on the classic Files Changed page.
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
119 changes: 119 additions & 0 deletions
119
calico-enterprise/observability/elastic/policy-activity.mdx
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,119 @@ | ||
| --- | ||
| description: Learn how to use policy activity logs to gain visibility into policy and rule activity. | ||
| --- | ||
|
|
||
| # Policy activity logs | ||
|
|
||
| :::note | ||
|
|
||
| This feature is in tech preview. Tech preview features may be subject to significant changes before they become GA. | ||
|
|
||
| ::: | ||
|
|
||
| ## About policy activity logs | ||
|
|
||
| Policy activity logs provide granular visibility into network policy behavior within your cluster. By tracking exactly which rules are evaluated and triggered, these logs offer policy-centric context that complements standard Flow Logs. | ||
|
|
||
| Regularly reviewing policy activity, especially unused policies and rules, helps you maintain a clean and secure cluster by identifying and removing obsolete resources. | ||
|
|
||
| ## Log structure | ||
|
|
||
| Each log entry contains the following key fields: | ||
|
|
||
| | Field | Description | Example | | ||
| | ----------------- | -------------------------------------------------------------------------------------------- | -------------------------------- | | ||
| | `policy.kind` | The type of the policy. Possible values: `NetworkPolicy`, `GlobalNetworkPolicy`, `KubernetesNetworkPolicy`, `KubernetesAdminNetworkPolicy`, `KubernetesBaselineAdminNetworkPolicy`, `StagedNetworkPolicy`, `StagedGlobalNetworkPolicy`, `StagedKubernetesNetworkPolicy` | `NetworkPolicy` | | ||
| | `policy.namespace`| The namespace where the policy is defined. | `my-test` | | ||
| | `policy.name` | The unique name of the policy. | `my-tier.my-networkpolicy` | | ||
| | `rule` | The identifier for the specific rule that was triggered, formatted as `{generation}-{direction}-{rule_index}`. | `2-ingress-4` | | ||
ctauchen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| | `cluster` | The name of the cluster where the activity occurred. | `cluster` | | ||
| | `last_evaluated` | The timestamp when the rule was last evaluated. | `2025-12-01T23:09:28.384714204Z` | | ||
|
|
||
|
|
||
| :::note | ||
|
|
||
| The generation number in the `rule` identifier (e.g., the `2` in `2-ingress-4`) reflects the `metadata.generation` of the policy at the time of evaluation. | ||
|
|
||
| It increments whenever the spec of the policy is updated, allowing you to correlate activity logs with the exact policy configuration that was active when the traffic occurred. | ||
|
|
||
| ::: | ||
|
|
||
| ## Find unused policies | ||
|
|
||
| Policy activity logs record only policies and rules that have been evaluated by actual network traffic. To identify unused policies or rules in your cluster, follow these steps: | ||
|
|
||
| ### Step 1: Retrieve active policies/rules | ||
|
|
||
| Use the following Elasticsearch query to list all policies that have been evaluated in the last 90 days: | ||
|
|
||
| ``` | ||
| POST /tigera_secure_ee_policy_activity*/_search | ||
| { | ||
| "size": 0, | ||
| "query": { | ||
| "range": { | ||
| "last_evaluated": { | ||
| "gte": "now-90d" | ||
| } | ||
| } | ||
| }, | ||
| "aggs": { | ||
| "active_policies": { | ||
| "composite": { | ||
| "size": 1000, | ||
| "sources": [ | ||
| { "kind": { "terms": { "field": "policy.kind" } } }, | ||
| { "namespace": { "terms": { "field": "policy.namespace" } } }, | ||
| { "name": { "terms": { "field": "policy.name" } } } | ||
| ] | ||
| }, | ||
| "aggs": { | ||
| "latest_activity": { | ||
| "max": { | ||
| "field": "last_evaluated" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| To see which rules have been triggered in the last 90 days, use this query: | ||
tianfeng92 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ``` | ||
| POST /tigera_secure_ee_policy_activity*/_search | ||
| { | ||
| "size": 10000, | ||
| "query": { | ||
| "range": { | ||
| "last_evaluated": { | ||
| "gte": "now-90d" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ::: note | ||
|
|
||
| If you need to retrieve more than 10,000 logs, do not simply increase the size. Instead, use pagination (especially `scroll API`) to scroll through results efficiently. | ||
|
|
||
| [Learn more](https://www.elastic.co/docs/reference/elasticsearch/rest-apis/paginate-search-results) | ||
| ::: | ||
|
|
||
| ### Step 2: Retrieve all network policies and rules | ||
|
|
||
| Export your current policy and rule inventory using `kubectl get ...` commands. | ||
|
|
||
| ### Step 3: Compare lists | ||
|
|
||
| Compare the output from the steps above to identify unused resources: | ||
| - Match: Check if each policy (or specific rule ID) in your Cluster exists in the active lists from Elasticsearch. | ||
| - Identify Unused: Any policy or rule present in the inventory but missing from the active list has not been evaluated by traffic in the last 90 days. | ||
| - Verify: Before deleting any resource, ensure it is not a newly deployed policy that simply hasn't received traffic yet. | ||
|
|
||
| ## Limitations | ||
|
|
||
| - **Logs require traffic**: Policy activity logs are only generated when a rule is evaluated by traffic. Resources that have never been hit will not appear in these logs. You cannot identify them by querying for "old" logs; you must identify them by their absence from the active log data (as performed in the comparison steps above). | ||
| - **Long-lived connections**: A policy evaluation is logged only when a connection is established. If a connection remains open for a long duration (e.g., longer than your 90-day query window), the associated policy may not generate new logs, potentially making it appear "unused" despite actively carrying traffic. | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.