Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a playbook that sends k8s resource manifests, as json, to a speci… #1683

Merged
merged 3 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
arikalon1 marked this conversation as resolved.
Show resolved Hide resolved
this action doesn't create a finding
"""
try:
event_dict = event.obj.to_dict()
arikalon1 marked this conversation as resolved.
Show resolved Hide resolved
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}")
Loading