|
1 | 1 | import base64 |
2 | 2 | import json |
3 | 3 | import os |
4 | | -import random |
5 | 4 | import subprocess |
6 | 5 | import time |
7 | 6 | import traceback |
|
11 | 10 | import certifi |
12 | 11 | import typer |
13 | 12 | import yaml |
14 | | -# from hikaru.model.rel_1_26 import Container, Job, JobSpec, ObjectMeta, PodSpec, PodTemplateSpec, SecurityContext |
15 | | -# from kubernetes import client, config |
| 13 | + |
16 | 14 | from pydantic import BaseModel, Extra |
17 | 15 |
|
18 | | -# from robusta._version import __version__ |
| 16 | +import importlib.metadata |
| 17 | +__version__ = importlib.metadata.version("robusta-cli") |
| 18 | + |
19 | 19 | from auth import app as auth_commands |
20 | 20 | from backend_profile import backend_profile |
21 | 21 | from eula import handle_eula |
|
26 | 26 | from slack_feedback_message import SlackFeedbackMessagesSender |
27 | 27 | from slack_verification import verify_slack_channel |
28 | 28 | 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 |
33 | 33 |
|
34 | 34 | 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 |
36 | 35 |
|
37 | 36 |
|
38 | 37 | def cert_already_exists(new_cert: bytes) -> bool: |
@@ -415,96 +414,16 @@ def demo_alert( |
415 | 414 | Create a demo alert on AlertManager. |
416 | 415 | The alert pod is selected randomly from the pods in the current namespace |
417 | 416 | """ |
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) |
437 | 419 | 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", |
441 | 423 | ) |
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") |
508 | 427 |
|
509 | 428 |
|
510 | 429 | if __name__ == "__main__": |
|
0 commit comments