From 966a6302c269a8e6ccb727b9a060d8b30eb3a73d Mon Sep 17 00:00:00 2001 From: arik Date: Thu, 9 Jan 2025 20:28:11 +0200 Subject: [PATCH] =?UTF-8?q?add=20a=20playbook=20that=20sends=20k8s=20resou?= =?UTF-8?q?rce=20manifests,=20as=20json,=20to=20a=20speci=E2=80=A6=20(#168?= =?UTF-8?q?3)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add a playbook that sends k8s resource manifests, as json, to a specified url. * Fix action docs --- .../actions/change-tracking.rst | 2 + .../playbook-track-changes.rst | 37 +++++++++++++++++++ playbooks/robusta_playbooks/babysitter.py | 30 ++++++++++++++- 3 files changed, 68 insertions(+), 1 deletion(-) diff --git a/docs/playbook-reference/actions/change-tracking.rst b/docs/playbook-reference/actions/change-tracking.rst index ac8c90fe6..e3b7c6579 100644 --- a/docs/playbook-reference/actions/change-tracking.rst +++ b/docs/playbook-reference/actions/change-tracking.rst @@ -12,4 +12,6 @@ These actions were built for tracking changes in your cluster .. robusta-action:: playbooks.robusta_playbooks.babysitter.resource_babysitter on_deployment_update +.. robusta-action:: playbooks.robusta_playbooks.babysitter.json_change_tracker on_deployment_update + diff --git a/docs/playbook-reference/kubernetes-examples/playbook-track-changes.rst b/docs/playbook-reference/kubernetes-examples/playbook-track-changes.rst index d5deab0b1..e670f215c 100644 --- a/docs/playbook-reference/kubernetes-examples/playbook-track-changes.rst +++ b/docs/playbook-reference/kubernetes-examples/playbook-track-changes.rst @@ -141,6 +141,43 @@ A Robusta notification will arrive in your configured :ref:`sinks `. + +**Testing**: + +Modify a Deployment image in your cluster. + +A notification with the Deployment manifest, as json, should be sent to the webhook url + + + Cleanup ------------------------------ Remove the playbook you added based on your specific use case from the ``customPlaybooks`` in your ``generated_values.yaml`` file. Then, perform a :ref:`Helm Upgrade `. diff --git a/playbooks/robusta_playbooks/babysitter.py b/playbooks/robusta_playbooks/babysitter.py index bc25908e7..1a4a400d6 100644 --- a/playbooks/robusta_playbooks/babysitter.py +++ b/playbooks/robusta_playbooks/babysitter.py @@ -4,8 +4,11 @@ # * https://github.com/google/diff-match-patch/wiki/Language:-Python (see output format here: https://neil.fraser.name/software/diff_match_patch/demos/diff.html) # * https://github.com/wagoodman/diff2HtmlCompare # * https://github.com/GerHobbelt/google-diff-match-patch +import json import logging -from typing import List +from typing import List, Optional, Dict + +import requests from robusta.api import ( ActionParams, @@ -79,3 +82,28 @@ def resource_babysitter(event: KubernetesAnyChangeEvent, config: BabysitterConfi title="Kubernetes Manifest Change", ) event.add_finding(finding) + + +class UrlParam(ActionParams): + """ + :var url: Url that should be used in the action + :var headers: A dictionary of headers to be added to the request. + """ + + url: str + headers: Optional[Dict[str, str]] = None + + +@action +def json_change_tracker(event: KubernetesAnyChangeEvent, params: UrlParam): + """ + Posts Kubernetes resource changes as JSON to a specified URL. + This action doesn't create a finding + """ + try: + event_dict = event.obj.to_dict() + event_dict["operation"] = event.operation.value + event_json = json.dumps(event_dict, indent=2) + requests.post(params.url, headers=params.headers, data=event_json) + except Exception as e: + logging.exception(f"Failed to post change event to {params.url}. event: {event_json}") \ No newline at end of file