From 713898c108b410a41e65ee249f02ba1fdae71a21 Mon Sep 17 00:00:00 2001 From: Robert Szefler Date: Wed, 14 Feb 2024 11:58:59 +0100 Subject: [PATCH] Username & subteam name representation changes to make them compatible with k8s labels (#1286) * username & subteam name representation changes to make them compatible with k8s labels * update MentionParams docs * add ref to mention_enricher in docs --- .../creating-notifications.rst | 1 + .../robusta_playbooks/alerts_integration.py | 23 +++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/docs/configuration/defining-playbooks/creating-notifications.rst b/docs/configuration/defining-playbooks/creating-notifications.rst index d20a78648..e4022dedc 100644 --- a/docs/configuration/defining-playbooks/creating-notifications.rst +++ b/docs/configuration/defining-playbooks/creating-notifications.rst @@ -64,6 +64,7 @@ We can automate this with Robusta: 1. See :ref:`on_pod_oom_killed` 2. See :ref:`pod_graph_enricher` 3. See :ref:`pod_oom_killer_enricher` + 4. See :ref:`mention_enricher` Here is the result in Slack: diff --git a/playbooks/robusta_playbooks/alerts_integration.py b/playbooks/robusta_playbooks/alerts_integration.py index 9728eb204..73b0ae635 100644 --- a/playbooks/robusta_playbooks/alerts_integration.py +++ b/playbooks/robusta_playbooks/alerts_integration.py @@ -2,7 +2,7 @@ from collections import defaultdict from datetime import datetime from string import Template -from typing import Any, Dict, List, Optional +from typing import Any, Dict, List, Optional, Iterable import requests from hikaru.model.rel_1_26 import Node @@ -445,11 +445,11 @@ def foreign_logs_enricher(event: ExecutionBaseEvent, params: ForeignLogParams): class MentionParams(ActionParams): """ - :var static_mentions: List of Slack user ids/groups ids to be mentioned - :var mentions_label: A alert label, or Kubernetes resource label, in which the value contains a comma separated ids to mention + :var static_mentions: List of Slack user ids/subteam ids to be mentioned + :var mentions_label: An alert label, or Kubernetes resource label, in which the value contains a dot separated ids to mention :var message_template: Optional. Custom mention message. Default: `"Hey: $mentions"` - :example static_mentions: ["<@U44V9P1JJ1Z>", ""] + :example static_mentions: ["U44V9P1JJ1Z", "S22H3Q3Q111"] """ static_mentions: Optional[List[str]] @@ -500,5 +500,20 @@ def mention_enricher(event: KubernetesResourceEvent, params: MentionParams): if params.static_mentions: mentions = mentions.union(params.static_mentions) + mentions = mention_to_slack_format(mentions) + message = params.message_template.replace("$mentions", " ".join(mentions)) event.add_enrichment([MarkdownBlock(message)]) + + +def mention_to_slack_format(mentions: Iterable[str]) -> List[str]: + result = [] + for mentions_spec in mentions: + for mention in mentions_spec.split('.'): + if mention.startswith("U"): + result.append(f"<@{mention}>") + elif mention.startswith("S"): + result.append(f"") + else: + raise ValueError(f"unknown mention format: {mention}") + return result