|
4 | 4 | import subprocess |
5 | 5 | from typing import Optional, List |
6 | 6 |
|
| 7 | +from core.reporting.consts import SlackAnnotations |
| 8 | +from robusta.core.playbooks.common import get_resource_events_table |
7 | 9 | from robusta.core.model.env_vars import KUBECTL_CMD_TIMEOUT_SEC |
8 | 10 | from robusta.core.model.base_params import ActionParams |
9 | 11 | from robusta.core.playbooks.actions_registry import action |
10 | 12 | from robusta.core.model.events import ExecutionBaseEvent |
11 | 13 | from robusta.core.reporting.blocks import FileBlock, JsonBlock, TableBlock, MarkdownBlock |
12 | | -from robusta.core.reporting.base import Finding |
| 14 | +from robusta.core.reporting.base import Finding, EnrichmentType |
13 | 15 | from robusta.utils.error_codes import ActionException, ErrorCodes |
14 | 16 |
|
15 | 17 |
|
@@ -126,47 +128,20 @@ def fetch_resource_events(event: ExecutionBaseEvent, params: ResourceParams): |
126 | 128 | ) |
127 | 129 |
|
128 | 130 | try: |
129 | | - cmd = ["kubectl", "get", "events", |
130 | | - f"--field-selector=involvedObject.name={params.name},involvedObject.kind={params.kind}", |
131 | | - "-o", "json"] |
132 | | - if params.namespace: |
133 | | - cmd.extend(["-n", params.namespace]) |
134 | | - |
135 | | - output = run_kubectl_command(cmd) |
136 | | - |
137 | | - events_data = json.loads(output) |
138 | | - items = events_data.get("items", []) |
139 | | - |
140 | | - if not items: |
141 | | - finding.add_enrichment([MarkdownBlock(f"No events found for {params.kind}/{params.name}")]) |
142 | | - else: |
143 | | - headers = ["Type", "Reason", "Age", "From", "Message"] |
144 | | - rows = [] |
145 | | - |
146 | | - for item in items: |
147 | | - event_type = item.get("type", "Unknown") |
148 | | - reason = item.get("reason", "Unknown") |
149 | | - |
150 | | - last_timestamp = item.get("lastTimestamp") or item.get("eventTime", "") |
151 | | - age = last_timestamp.split("T")[0] if last_timestamp else "Unknown" |
152 | | - |
153 | | - source = item.get("source", {}) |
154 | | - from_str = source.get("component", "Unknown") |
155 | | - if source.get("host"): |
156 | | - from_str += f" ({source.get('host')})" |
157 | | - |
158 | | - message = item.get("message", "No message") |
159 | | - |
160 | | - rows.append([event_type, reason, age, from_str, message]) |
161 | | - |
162 | | - rows.reverse() |
163 | | - |
164 | | - table_block = TableBlock( |
165 | | - rows=rows, |
166 | | - headers=headers |
| 131 | + events_table = get_resource_events_table( |
| 132 | + "Resource Events", |
| 133 | + params.kind, |
| 134 | + params.name, |
| 135 | + params.namespace, |
| 136 | + ) |
| 137 | + if events_table: |
| 138 | + finding.add_enrichment( |
| 139 | + [events_table], |
| 140 | + {SlackAnnotations.ATTACHMENT: True}, |
| 141 | + enrichment_type=EnrichmentType.k8s_events, |
| 142 | + title="Resource Events", |
167 | 143 | ) |
168 | 144 |
|
169 | | - finding.add_enrichment([table_block]) |
170 | 145 |
|
171 | 146 | event.add_finding(finding) |
172 | 147 |
|
|
0 commit comments