Skip to content

Commit

Permalink
Feature/helm health monitoring (#907)
Browse files Browse the repository at this point in the history
* Improved notification handling
  • Loading branch information
ganeshrvel authored May 28, 2023
1 parent 475c503 commit 0874349
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 24 deletions.
27 changes: 20 additions & 7 deletions playbooks/robusta_playbooks/helm_release_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
def helm_status_enricher(event: HelmReleasesEvent):
logging.debug(f"received - helm releases change event: {event.helm_release.namespace}/{event.helm_release.name}")

title = f'Helm release {event.helm_release.namespace}/{event.helm_release.name} - version: {event.helm_release.chart.metadata.version} - status: {event.helm_release.info.status}'
title = f'Helm release {event.helm_release.namespace}/{event.helm_release.name}'
title += f' - version: {event.helm_release.chart.metadata.version}' if event.helm_release.chart else ''
title += f' - status: {event.helm_release.info.status}'

finding = Finding(
title=title,
Expand All @@ -20,9 +22,18 @@ def helm_status_enricher(event: HelmReleasesEvent):
severity=HelmReleasesEvent.get_severity(event.helm_release),
)

chart_info = f"\nChart Information:\n\n" \
f" ● *Name*: `{event.helm_release.chart.metadata.name}`\n\n" \
f" ● *Version*: `{event.helm_release.chart.metadata.version}`\n\n\n"
chart_info = ""
if event.helm_release.chart:
chart_info = f"\nChart Information:\n\n" \
f" ● *Name*: `{event.helm_release.chart.metadata.name}`\n\n" \
f" ● *Version*: `{event.helm_release.chart.metadata.version}`\n\n\n"

detailed_info = ""
if event.helm_release.info.description:
detailed_info = f" ● *Description*: `{event.helm_release.info.description}`\n\n"
if event.helm_release.info.notes:
notes = event.helm_release.info.notes.replace("\n", '')
detailed_info += f" ● *Notes*: `{notes}`\n\n"

finding.add_enrichment(
[
Expand All @@ -31,9 +42,11 @@ def helm_status_enricher(event: HelmReleasesEvent):
f"Release information:\n\n"
f" ● *Name*: `{event.helm_release.name}`\n\n"
f" ● *Namespace*: `{event.helm_release.namespace}`\n\n"
f" ● *First Deployed*: `{event.helm_release.info.get_first_deployed().strftime('%b %d, %Y, %I:%M:%S %p%z')}`\n\n"
f" ● *Last Deployed*: `{event.helm_release.info.get_last_deployed().strftime('%b %d, %Y, %I:%M:%S %p%z')}`\n\n"
f" ● *Description*: `{event.helm_release.info.description}`\n\n"
f" ● *Status*: `{event.helm_release.info.status}`\n\n"
f" ● *Revision*: `{event.helm_release.version}`\n\n"
f" ● *First Deployed*: `{event.helm_release.info.first_deployed.strftime('%b %d, %Y, %I:%M:%S %p%z')}`\n\n"
f" ● *Last Deployed*: `{event.helm_release.info.last_deployed.strftime('%b %d, %Y, %I:%M:%S %p%z')}`\n\n"
f"{detailed_info}"
f"{chart_info}"
),
]
Expand Down
30 changes: 15 additions & 15 deletions src/robusta/core/model/helm_release.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional, List
from typing import Optional, List, Any, Dict
from pydantic import BaseModel
from base64 import b64decode
from datetime import datetime
Expand All @@ -9,42 +9,42 @@
class Metadata(BaseModel):
name: str
version: str
description: str
apiVersion: str
appVersion: str
description: Optional[str]
apiVersion: Optional[str]
appVersion: Optional[str]


class Chart(BaseModel):
metadata: Metadata


class Info(BaseModel):
first_deployed: str
last_deployed: str
first_deployed: datetime
last_deployed: datetime
deleted: str
description: str
description: Optional[str]
status: str
notes: Optional[str]

def get_last_deployed(self):
return datetime.strptime(self.last_deployed, '%Y-%m-%dT%H:%M:%S.%f%z')

def get_first_deployed(self):
return datetime.strptime(self.first_deployed, '%Y-%m-%dT%H:%M:%S.%f%z')


class HelmRelease(BaseModel):
name: str
info: Info
chart: Chart
chart: Optional[Chart]
version: int
namespace: str
deleted: bool = False

@staticmethod
def list_to_json(releases: List["HelmRelease"]):
def list_to_dict(releases: List["HelmRelease"]):
return [release.dict() for release in releases]

def dict(self, *args: Any, **kwargs: Any) -> Dict[str, Any]:
release_dict = super().dict()
release_dict["info"]["first_deployed"] = self.info.first_deployed.isoformat()
release_dict["info"]["last_deployed"] = self.info.last_deployed.isoformat()
return release_dict

@classmethod
def from_api_server(cls, encoded_release_data: str) -> "HelmRelease":
# k8 base64 dec
Expand Down
2 changes: 1 addition & 1 deletion src/robusta/core/triggers/helm_releases_triggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def should_fire(self, event: TriggerEvent, playbook_id: str):
if self.namespace and event.helm_release.namespace != self.namespace:
return False

last_deployed_utc = event.helm_release.info.get_last_deployed().astimezone(pytz.utc)
last_deployed_utc = event.helm_release.info.last_deployed.astimezone(pytz.utc)

if self.duration:
# if the last deployed time is lesser than the `duration` time then dont append to the filtered list
Expand Down
2 changes: 1 addition & 1 deletion src/robusta/runner/web_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def send_helm_release_events(release_data: List[HelmRelease], retries=1, timeout

url = f"http://127.0.0.1:{PORT}/api/helm-releases"
data = {
"data": HelmRelease.list_to_json(release_data),
"data": HelmRelease.list_to_dict(release_data),
}

try:
Expand Down

0 comments on commit 0874349

Please sign in to comment.