Skip to content

Commit

Permalink
Ajout Status icon + filter namespace in notification selector
Browse files Browse the repository at this point in the history
  • Loading branch information
LawiK974 committed Mar 15, 2024
1 parent 6bf8f6c commit 84e404c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
2 changes: 2 additions & 0 deletions config.sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ notifications:
# - Pod
labels: # kube-notify merge event labels (should be null) with involvedObject labels if is Pod
- key: job-name
# namespaces:
# - kube-system
# types:
# - Normal
- name: customResources
Expand Down
16 changes: 12 additions & 4 deletions kube_notify/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ async def crds_stream(crd, namespace, kube_notify_config):
)
for obj in stream["items"]:
resource_name = str(obj["metadata"]["name"])
resource_namespace = str(obj["metadata"]["namespace"])
resource_kind = str(obj["kind"])
resource_apiversion = str(obj["apiVersion"])
creation_timestamp = datetime.datetime.strptime(
Expand All @@ -64,6 +65,7 @@ async def crds_stream(crd, namespace, kube_notify_config):
fields = add_fields_to_the_message(obj, crd)
last_timestamp = process_last_timestamp(obj, crd)
fields["Timestamp"] = last_timestamp.isoformat()
fields["Namespace"] = resource_namespace
event_type = (
"ADDED" if creation_timestamp == last_timestamp else "UPDATED"
)
Expand All @@ -74,6 +76,7 @@ async def crds_stream(crd, namespace, kube_notify_config):
resource_apiversion,
resource_kind,
resource_name,
resource_namespace,
)
if event_info in event_infos or last_event_info == event_info:
continue
Expand All @@ -89,6 +92,7 @@ async def crds_stream(crd, namespace, kube_notify_config):
kube_notify_config,
crd.get("type"),
dict(obj["metadata"].get("labels", {})),
resource_namespace,
)
await asyncio.sleep(0)
del stream
Expand Down Expand Up @@ -122,23 +126,26 @@ async def core_stream(kube_notify_config):
involved_object_namespace = str(obj.involved_object.namespace)

title = f"{event_type} {resource_kind}"
description = f"{event_type} {resource_kind} : {involved_object_kind} {involved_object_name} {reason}."
description = (
f"{involved_object_kind} {involved_object_name} {reason}."
)
fields = {
"Message": message,
"Reason": reason,
"Type": event_type,
"Object kind": involved_object_kind,
"Object name": involved_object_name,
"Timestamp": last_timestamp.isoformat(),
"Namespace": str(obj.metadata.namespace),
"Namespace": involved_object_namespace,
}
event_info = (
last_timestamp,
involved_object_namespace,
event_type,
resource_kind,
involved_object_kind,
involved_object_name,
reason,
resource_name,
event_type,
message,
)
labels = dict(obj.metadata.labels or {})
Expand All @@ -163,6 +170,7 @@ async def core_stream(kube_notify_config):
kube_notify_config,
event_type,
labels,
involved_object_namespace,
involved_object_kind,
)
await asyncio.sleep(0)
Expand Down
34 changes: 30 additions & 4 deletions kube_notify/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


def check_selector(
resource_name, resources, resource_type, labels, involved_object_kind
resource_name, resources, resource_type, labels, namespace, involved_object_kind
):
for resource in resources:
if resource["name"] == resource_name:
Expand All @@ -30,6 +30,10 @@ def check_selector(
selectors.get("involvedObjectKind") is None
or involved_object_kind in selectors.get("involvedObjectKind")
)
and (
selectors.get("namespaces") is None
or namespace in selectors.get("namespaces")
)
)
elif "excludeSelector" in resource:
selectors = resource.get("excludeSelector", {})
Expand All @@ -44,11 +48,30 @@ def check_selector(
for label in selectors.get("labels", [])
)
or involved_object_kind in selectors.get("involvedObjectKind", [])
or namespace in selectors.get("namespaces", [])
)
return True
return False


def get_status_icon(event_type, fields):
if event_type == "Warning":
return "⚠️"
if event_type == "Normal":
return "✅"
if fields.get("Status") == "Completed":
return "✅"
if fields.get("Status") == "InProgress":
return "⏳"
if fields.get("Status") == "New":
return "🆕"
if fields.get("Status") == "PartiallyFailed":
return "⚠️"
if fields.get("Status") == "Failed":
return "❌"
return ""


async def handle_notify(
resource_name,
title,
Expand All @@ -58,6 +81,7 @@ async def handle_notify(
kube_notify_config,
resource_type,
labels,
namespace,
involved_object_kind=None,
):
notifs = ["Skipping"]
Expand All @@ -70,9 +94,12 @@ async def handle_notify(
group.get("resources"),
resource_type,
labels,
namespace,
involved_object_kind,
):
notifs = []
status_icon = get_status_icon(resource_type, fields)
description = f"[{status_icon}] {description}"
if group.get("discord"):
notifs.append(f"{group_name}/discord")
discord = group.get("discord")
Expand All @@ -98,10 +125,9 @@ def send_gotify_message(url, token, title, description, fields):
url = f"{url}/message?token={token}&format=markdown"
headers = {"Content-Type": "application/json"}
message = f"**{description}**\n\n"
message += "| | |\n"
message += "| ---- | ---- |\n"

for key, value in fields.items():
message += f"| {key} | **{value}** |\n"
message += f"**{key} :**\n{value}\n\n"
data = {
"title": title,
"message": message,
Expand Down

0 comments on commit 84e404c

Please sign in to comment.