Skip to content

Commit

Permalink
Annotations and labels in event templated strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Szefler committed Jul 9, 2024
1 parent d1c9454 commit 9f3c9b3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
6 changes: 4 additions & 2 deletions playbooks/robusta_playbooks/common_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ def customise_finding(event: ExecutionBaseEvent, params: FindingOverrides):

class FindingFields(ActionParams):
"""
:var title: Finding title. Title can be templated with name/namespace/kind/node of the resource, if applicable
:var title: Finding title. Title can be templated with name/namespace/kind/node of the resource, if applicable.
|
Additionally, templating with labels and annotations of the resource is supported via $labels.label_name and $annotations.annotation_name.
:var aggregation_key: Aggregation Keys are used for grouping similar types of notifications together.
|
For example, all CrashLoopBackOff notifications should have the same Aggregation Key so that Sinks can group them together.
Expand All @@ -54,7 +56,7 @@ class FindingFields(ActionParams):
:var description: Finding description. Description can be templated
:var severity: Finding severity. Allowed values: DEBUG, INFO, LOW, MEDIUM, HIGH
:example title: "Job $name on namespace $namespace failed"
:example title: "Job $name (importance: $labels.importance) in namespace $namespace failed"
:example aggregation_key: "JobFailure"
:example severity: DEBUG
"""
Expand Down
17 changes: 14 additions & 3 deletions src/robusta/utils/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,32 @@
from robusta.core.reporting import FindingSubject


class ExtendedTemplate(Template):
# Add the possibility of using the "." character in placeholders in order to support
# annotations and labels. Note the pattern is case-insensitive (see the documentation
# of string.Template).
idpattern = r'(?a:[_a-z][_a-z0-9.]*)'


def format_event_templated_string(subject: Union[FindingSubject, ObjectReference], string_to_substitute) -> str:
"""
For templating strings based on event subjects
"""
labels: Dict[str, str] = defaultdict(lambda: "<missing>")
variables: Dict[str, str] = defaultdict(lambda: "<missing>")
kind = subject.kind if isinstance(subject, ObjectReference) else subject.subject_type.value
labels.update(
variables.update(
{
"name": subject.name,
"kind": kind,
"namespace": subject.namespace if subject.namespace else "<missing>",
"node": subject.node if isinstance(subject, FindingSubject) and subject.node else "<missing>",
}
)
return Template(string_to_substitute).safe_substitute(labels)
if isinstance(subject, FindingSubject):
variables.update({f"labels.{key}": value for key, value in subject.labels.items()})
variables.update({f"annotations.{key}": value for key, value in subject.annotations.items()})
return ExtendedTemplate(string_to_substitute).safe_substitute(variables)


def load_json(s: Union[str, bytes]) -> Any:
"""
Expand Down

0 comments on commit 9f3c9b3

Please sign in to comment.