Skip to content

Commit 6a8baca

Browse files
Avi-Robustaaantn
andauthored
[MAIN-2842] added kubectl action in runner (#1677)
* working version * fixing the kubectl command * fix kubectl command * fixing finding * added debug log * updated description * Do not merge - remove '*' in event table enrichments (#1674) * Update event_enrichments.py * Update event_enrichments.py * Update event_enrichments.py * Update event_enrichments.py * Update event_enrichments.py * pr changes * improved logs --------- Co-authored-by: Natan Yellin <[email protected]>
1 parent 57c13b5 commit 6a8baca

File tree

2 files changed

+83
-6
lines changed

2 files changed

+83
-6
lines changed

playbooks/robusta_playbooks/event_enrichments.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def event_resource_events(event: EventChangeEvent):
134134
return
135135
obj = event.obj.regarding
136136
events_table = get_resource_events_table(
137-
"*Related Events*",
137+
"Related Events",
138138
obj.kind,
139139
obj.name,
140140
obj.namespace,
@@ -245,7 +245,7 @@ def resource_events_enricher(event: KubernetesResourceEvent, params: ExtendedEve
245245
[
246246
EventsBlock(
247247
events=events_row,
248-
table_name=f"*{kind} events:*",
248+
table_name=f"{kind} Events",
249249
column_renderers={"time": RendererType.DATETIME},
250250
headers=["reason", "type", "time", "kind", "name", "message"],
251251
rows=rows,
@@ -269,7 +269,7 @@ def pod_events_enricher(event: PodEvent, params: EventEnricherParams):
269269
return
270270

271271
events_table_block = get_resource_events_table(
272-
"*Pod events:*",
272+
"Pod Events",
273273
pod.kind,
274274
pod.metadata.name,
275275
pod.metadata.namespace,
@@ -292,7 +292,7 @@ def enrich_pod_with_node_events(event: PodEvent, params: EventEnricherParams):
292292
"""
293293
pod = event.get_pod()
294294
events_table_block = get_resource_events_table(
295-
"*Node events:*",
295+
"Node Events",
296296
kind="Node",
297297
name=pod.spec.nodeName,
298298
included_types=params.included_types,
@@ -325,7 +325,7 @@ def deployment_events_enricher(event: DeploymentEvent, params: ExtendedEventEnri
325325
selected_pods = pods if len(pods) <= params.max_pods else pods[: params.max_pods]
326326
for pod in selected_pods:
327327
events_table_block = get_resource_events_table(
328-
f"*Pod events for {pod.metadata.name}:*",
328+
f"Pod events for {pod.metadata.name}",
329329
"Pod",
330330
pod.metadata.name,
331331
pod.metadata.namespace,
@@ -341,7 +341,7 @@ def deployment_events_enricher(event: DeploymentEvent, params: ExtendedEventEnri
341341
)
342342
else:
343343
events_table_block = get_resource_events_table(
344-
"*Deployment events:*",
344+
"Deployment Events",
345345
dep.kind,
346346
dep.metadata.name,
347347
dep.metadata.namespace,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import logging
2+
import os
3+
import uuid
4+
5+
from hikaru.model.rel_1_26 import Container, PodSpec
6+
from robusta.api import (
7+
RUNNER_SERVICE_ACCOUNT,
8+
ExecutionBaseEvent,
9+
FileBlock,
10+
MarkdownBlock,
11+
PodRunningParams,
12+
RobustaJob,
13+
action,
14+
)
15+
from robusta.utils.parsing import format_event_templated_string
16+
17+
IMAGE: str = os.getenv("KUBECTL_IMAGE_OVERRIDE", f"bitnami/kubectl:latest")
18+
19+
20+
class KubectlParams(PodRunningParams):
21+
"""
22+
:var kubectl_command: The full kubectl command to run, formatted as a shell command string.
23+
:var description: A description of the command ran.
24+
:var timeout: The maximum time (in seconds) to wait for the kubectl command to complete. Default is 3600 seconds.
25+
"""
26+
27+
command: str = None
28+
description: str = None
29+
timeout: int = 3600
30+
31+
32+
@action
33+
def kubectl_command(event: ExecutionBaseEvent, params: KubectlParams):
34+
"""
35+
Runs a custom kubectl command inside a Kubernetes pod using a Job.
36+
"""
37+
38+
subject = event.get_subject()
39+
formatted_kubectl_command = format_event_templated_string(subject, params.command)
40+
logging.info(f"kubectl_command running '{params.description}'")
41+
logging.debug(f"kubectl_command: '{formatted_kubectl_command}'")
42+
43+
spec = PodSpec(
44+
serviceAccountName=RUNNER_SERVICE_ACCOUNT,
45+
containers=[
46+
Container(
47+
name="kubectl",
48+
image=IMAGE,
49+
imagePullPolicy="Always",
50+
command=["/bin/sh", "-c"],
51+
args=[formatted_kubectl_command]
52+
)
53+
],
54+
restartPolicy="Never",
55+
)
56+
57+
try:
58+
kubectl_response = RobustaJob.run_simple_job_spec(
59+
spec,
60+
f"robusta-kubectl-command-{str(uuid.uuid4())}",
61+
params.timeout,
62+
custom_annotations=params.custom_annotations,
63+
ttl_seconds_after_finished=43200, # 12 hours
64+
delete_job_post_execution=True,
65+
process_name=False,
66+
)
67+
descriptiont_text = params.description if params.description else "Kubectl Command"
68+
event.add_enrichment(
69+
[
70+
MarkdownBlock(f"*{formatted_kubectl_command}*"),
71+
FileBlock(f"kubectl.txt", kubectl_response.encode()),
72+
], title=descriptiont_text
73+
)
74+
except Exception:
75+
logging.exception("Error running kubectl command")
76+
77+

0 commit comments

Comments
 (0)