Skip to content

Commit

Permalink
add a playbook that sends k8s resource manifests, as json, to a speci…
Browse files Browse the repository at this point in the history
…fied url.
  • Loading branch information
arikalon1 committed Jan 7, 2025
1 parent e363f6a commit d37d34b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/playbook-reference/actions/change-tracking.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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


Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,43 @@ A Robusta notification will arrive in your configured :ref:`sinks <Sinks Referen
:width: 600
:align: center

Use Case 3: Notification when a Deployment image change - including the Deployment manifest
********************************************************************************************
**Scenario**: You want to get the Deployment manifest, each time the image changes

**Implementation**:

Add the following YAML to the ``customPlaybooks`` Helm value:

.. code-block:: yaml
customPlaybooks:
- triggers:
- on_deployment_update:
change_filters:
include:
- image
actions:
- json_change_tracker:
url: "https://SOME-WEBHOOL-URL"
.. details:: How does it work?

1. **Initialize Custom Playbook**: Create a custom playbook where you'll outline the rules for when and how you'll be notified.
2. **Set Up the Deployment Trigger**: In your custom playbook, add the ``on_deployment_change`` trigger, with a ``scope`` including only image changes. This ensures you'll receive notifications for deployment image changes.

This playbook doesn't use a Sink! It sends the to the url specified in the action parameters.

Then perform a :ref:`Helm Upgrade <Simple Upgrade>`.

**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 <Simple Upgrade>`.
Expand Down
29 changes: 28 additions & 1 deletion playbooks/robusta_playbooks/babysitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -79,3 +82,27 @@ 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
"""

url: str
headers: Optional[Dict[str, str]] = None


@action
def json_change_tracker(event: KubernetesAnyChangeEvent, params: UrlParam):
"""
post change json to the 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}")

0 comments on commit d37d34b

Please sign in to comment.