-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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]>
- Loading branch information
1 parent
57c13b5
commit 6a8baca
Showing
2 changed files
with
83 additions
and
6 deletions.
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
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,77 @@ | ||
import logging | ||
import os | ||
import uuid | ||
|
||
from hikaru.model.rel_1_26 import Container, PodSpec | ||
from robusta.api import ( | ||
RUNNER_SERVICE_ACCOUNT, | ||
ExecutionBaseEvent, | ||
FileBlock, | ||
MarkdownBlock, | ||
PodRunningParams, | ||
RobustaJob, | ||
action, | ||
) | ||
from robusta.utils.parsing import format_event_templated_string | ||
|
||
IMAGE: str = os.getenv("KUBECTL_IMAGE_OVERRIDE", f"bitnami/kubectl:latest") | ||
|
||
|
||
class KubectlParams(PodRunningParams): | ||
""" | ||
:var kubectl_command: The full kubectl command to run, formatted as a shell command string. | ||
:var description: A description of the command ran. | ||
:var timeout: The maximum time (in seconds) to wait for the kubectl command to complete. Default is 3600 seconds. | ||
""" | ||
|
||
command: str = None | ||
description: str = None | ||
timeout: int = 3600 | ||
|
||
|
||
@action | ||
def kubectl_command(event: ExecutionBaseEvent, params: KubectlParams): | ||
""" | ||
Runs a custom kubectl command inside a Kubernetes pod using a Job. | ||
""" | ||
|
||
subject = event.get_subject() | ||
formatted_kubectl_command = format_event_templated_string(subject, params.command) | ||
logging.info(f"kubectl_command running '{params.description}'") | ||
logging.debug(f"kubectl_command: '{formatted_kubectl_command}'") | ||
|
||
spec = PodSpec( | ||
serviceAccountName=RUNNER_SERVICE_ACCOUNT, | ||
containers=[ | ||
Container( | ||
name="kubectl", | ||
image=IMAGE, | ||
imagePullPolicy="Always", | ||
command=["/bin/sh", "-c"], | ||
args=[formatted_kubectl_command] | ||
) | ||
], | ||
restartPolicy="Never", | ||
) | ||
|
||
try: | ||
kubectl_response = RobustaJob.run_simple_job_spec( | ||
spec, | ||
f"robusta-kubectl-command-{str(uuid.uuid4())}", | ||
params.timeout, | ||
custom_annotations=params.custom_annotations, | ||
ttl_seconds_after_finished=43200, # 12 hours | ||
delete_job_post_execution=True, | ||
process_name=False, | ||
) | ||
descriptiont_text = params.description if params.description else "Kubectl Command" | ||
event.add_enrichment( | ||
[ | ||
MarkdownBlock(f"*{formatted_kubectl_command}*"), | ||
FileBlock(f"kubectl.txt", kubectl_response.encode()), | ||
], title=descriptiont_text | ||
) | ||
except Exception: | ||
logging.exception("Error running kubectl command") | ||
|
||
|