Skip to content

Commit 2e841a1

Browse files
committed
running and working with a simple sink config & commented out demo alert
1 parent 9908819 commit 2e841a1

15 files changed

Lines changed: 150 additions & 98 deletions

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "robusta-cli"
3-
version = "0.1.0"
3+
version = "0.0.0"
44
description = ""
55
authors = ["Natan Yellin <aantn@users.noreply.github.com>"]
66
readme = "README.md"

robusta_cli/cli/__init__.py

Whitespace-only changes.

robusta_cli/cli/demo_alert.py

Whitespace-only changes.

robusta_cli/demo_alert.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import json
2+
import random
3+
from typing import List
4+
5+
from hikaru.model.rel_1_26 import Container, Job, JobSpec, ObjectMeta, PodSpec, PodTemplateSpec, SecurityContext
6+
from kubernetes import client, config
7+
from robusta.integrations.prometheus.utils import AlertManagerDiscovery
8+
9+
10+
class AlertManagerException(Exception):
11+
def __init__(self, message: str):
12+
super().__init__(message)
13+
self.message = message
14+
15+
16+
def create_demo_alert(
17+
alertmanager_url: str,
18+
namespaces: List[str],
19+
alert: str,
20+
labels: str,
21+
kube_config: str,
22+
image: str
23+
):
24+
config.load_kube_config(kube_config)
25+
if not alertmanager_url:
26+
# search cluster alertmanager by known alertmanager labels
27+
alertmanager_url = AlertManagerDiscovery.find_alert_manager_url()
28+
if not alertmanager_url:
29+
raise AlertManagerException("Alertmanager service could not be auto-discovered. Please use the --alertmanager-url parameter")
30+
31+
pod = None
32+
for namespace in namespaces:
33+
pods = client.CoreV1Api().list_namespaced_pod(namespace)
34+
if pods.items:
35+
pod = pods.items[0]
36+
break
37+
38+
if not pod:
39+
raise AlertManagerException(f"Could not find any pod on namespace {namespaces}. Please use the --namespaces parameter to specify a namespace with pods")
40+
41+
alert_labels = {
42+
"alertname": alert,
43+
"severity": "critical",
44+
"pod": pod.metadata.name,
45+
"namespace": pod.metadata.namespace,
46+
}
47+
if labels:
48+
for label in labels.split(","):
49+
label_key = label.split("=")[0].strip()
50+
label_value = label.split("=")[1].strip()
51+
alert_labels[label_key] = label_value
52+
53+
demo_alerts = [
54+
{
55+
"status": "firing",
56+
"labels": alert_labels,
57+
"annotations": {
58+
"summary": "This is a demo alert manager alert created by Robusta",
59+
"description": "Nothing wrong here. This alert will be resolved soon",
60+
},
61+
}
62+
]
63+
64+
command = [
65+
"curl",
66+
"-X",
67+
"POST",
68+
f"{alertmanager_url}/api/v1/alerts",
69+
"-H",
70+
"Content-Type: application/json",
71+
"-d",
72+
f"{json.dumps(demo_alerts)}",
73+
]
74+
75+
job: Job = Job(
76+
metadata=ObjectMeta(
77+
name=f"alert-job-{random.randint(0, 10000)}",
78+
namespace=pod.metadata.namespace,
79+
),
80+
spec=JobSpec(
81+
template=PodTemplateSpec(
82+
spec=PodSpec(
83+
containers=[
84+
Container(
85+
name="alert-curl",
86+
image=image,
87+
command=command,
88+
securityContext=SecurityContext(runAsUser=2000),
89+
)
90+
],
91+
restartPolicy="Never",
92+
),
93+
),
94+
completions=1,
95+
ttlSecondsAfterFinished=0, # delete immediately when finished
96+
),
97+
)
98+
job.create()
99+
return pod.metadata.name, pod.metadata.namespace
Lines changed: 16 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import base64
22
import json
33
import os
4-
import random
54
import subprocess
65
import time
76
import traceback
@@ -11,11 +10,12 @@
1110
import certifi
1211
import typer
1312
import yaml
14-
# from hikaru.model.rel_1_26 import Container, Job, JobSpec, ObjectMeta, PodSpec, PodTemplateSpec, SecurityContext
15-
# from kubernetes import client, config
13+
1614
from pydantic import BaseModel, Extra
1715

18-
# from robusta._version import __version__
16+
import importlib.metadata
17+
__version__ = importlib.metadata.version("robusta-cli")
18+
1919
from auth import app as auth_commands
2020
from backend_profile import backend_profile
2121
from eula import handle_eula
@@ -26,13 +26,12 @@
2626
from slack_feedback_message import SlackFeedbackMessagesSender
2727
from slack_verification import verify_slack_channel
2828
from utils import get_runner_pod, log_title, namespace_to_kubectl
29-
from robusta.core.sinks.msteams.msteams_sink_params import MsTeamsSinkConfigWrapper, MsTeamsSinkParams
30-
from robusta.core.sinks.robusta.robusta_sink_params import RobustaSinkConfigWrapper, RobustaSinkParams
31-
from robusta.core.sinks.slack.slack_sink_params import SlackSinkConfigWrapper, SlackSinkParams
32-
from robusta.integrations.prometheus.utils import AlertManagerDiscovery
29+
from simple_sink_config import MsTeamsSinkConfigWrapper, MsTeamsSinkParams
30+
from simple_sink_config import RobustaSinkConfigWrapper, RobustaSinkParams
31+
from simple_sink_config import SlackSinkConfigWrapper, SlackSinkParams
32+
# from demo_alert import create_demo_alert, AlertManagerException
3333

3434
ADDITIONAL_CERTIFICATE: str = os.environ.get("CERTIFICATE", "")
35-
# TODO - separate shared classes to a separated shared repo, to remove dependencies between the cli and runner
3635

3736

3837
def cert_already_exists(new_cert: bytes) -> bool:
@@ -415,96 +414,16 @@ def demo_alert(
415414
Create a demo alert on AlertManager.
416415
The alert pod is selected randomly from the pods in the current namespace
417416
"""
418-
config.load_kube_config(kube_config)
419-
if not alertmanager_url:
420-
# search cluster alertmanager by known alertmanager labels
421-
alertmanager_url = AlertManagerDiscovery.find_alert_manager_url()
422-
if not alertmanager_url:
423-
typer.secho(
424-
"Alertmanager service could not be auto-discovered. " "Please use the --alertmanager-url parameter",
425-
fg="red",
426-
)
427-
return
428-
429-
pod = None
430-
for namespace in namespaces:
431-
pods = client.CoreV1Api().list_namespaced_pod(namespace)
432-
if pods.items:
433-
pod = pods.items[0]
434-
break
435-
436-
if not pod:
417+
try:
418+
pod_name, namespace = create_demo_alert(alertmanager_url, namespaces, alert, labels, kube_config, image)
437419
typer.secho(
438-
f"Could not find any pod on namespace {namespaces}"
439-
f"Please use the --namespaces parameter to specify a namespace with pods",
440-
fg="red",
420+
f"Created Alertmanager alert: alert-name: {alert} pod: {pod_name} "
421+
f"namespace: {namespace}",
422+
fg="green",
441423
)
442-
return
443-
444-
alert_labels = {
445-
"alertname": alert,
446-
"severity": "critical",
447-
"pod": pod.metadata.name,
448-
"namespace": pod.metadata.namespace,
449-
}
450-
if labels:
451-
for label in labels.split(","):
452-
label_key = label.split("=")[0].strip()
453-
label_value = label.split("=")[1].strip()
454-
alert_labels[label_key] = label_value
455-
456-
demo_alerts = [
457-
{
458-
"status": "firing",
459-
"labels": alert_labels,
460-
"annotations": {
461-
"summary": "This is a demo alert manager alert created by Robusta",
462-
"description": "Nothing wrong here. This alert will be resolved soon",
463-
},
464-
}
465-
]
466-
467-
command = [
468-
"curl",
469-
"-X",
470-
"POST",
471-
f"{alertmanager_url}/api/v1/alerts",
472-
"-H",
473-
"Content-Type: application/json",
474-
"-d",
475-
f"{json.dumps(demo_alerts)}",
476-
]
477-
478-
job: Job = Job(
479-
metadata=ObjectMeta(
480-
name=f"alert-job-{random.randint(0, 10000)}",
481-
namespace=pod.metadata.namespace,
482-
),
483-
spec=JobSpec(
484-
template=PodTemplateSpec(
485-
spec=PodSpec(
486-
containers=[
487-
Container(
488-
name="alert-curl",
489-
image=image,
490-
command=command,
491-
securityContext=SecurityContext(runAsUser=2000),
492-
)
493-
],
494-
restartPolicy="Never",
495-
),
496-
),
497-
completions=1,
498-
ttlSecondsAfterFinished=0, # delete immediately when finished
499-
),
500-
)
501-
job.create()
502-
typer.secho(
503-
f"Created Alertmanager alert: alert-name: {alert} pod: {pod.metadata.name} "
504-
f"namespace: {pod.metadata.namespace}",
505-
fg="green",
506-
)
507-
typer.echo("\n")
424+
typer.echo("\n")
425+
except AlertManagerException as e:
426+
typer.secho(e.message, fg="red")
508427

509428

510429
if __name__ == "__main__":

0 commit comments

Comments
 (0)