Skip to content

Commit

Permalink
Merge pull request #36 from robusta-dev/prometheus_resolution
Browse files Browse the repository at this point in the history
Prometheus resolution
  • Loading branch information
arikalon1 authored Aug 16, 2021
2 parents 540ca6b + 0f8e1a5 commit c76ee4b
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 25 deletions.
2 changes: 1 addition & 1 deletion docs/getting-started/uninstallation.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Uninstalling Robusta
#####################

Everything Robusta installs is placed in the ``robusta`` namespace. To delete Robusta, just delete that namespace.
Everything Robusta installs is placed in the ``robusta`` namespace. To delete Robusta, just delete that namespace.
4 changes: 2 additions & 2 deletions playbooks/alerts_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ def enrich(self, alert: PrometheusKubernetesAlert):
class GraphEnricher(Enricher):
def enrich(self, alert: PrometheusKubernetesAlert):
url = urlparse(alert.alert.generatorURL)
prometheus_base_url = f"{url.scheme}://{url.netloc}"
if alert.prometheus_url:
prometheus_base_url = alert.prometheus_url

else:
prometheus_base_url = PrometheusDiscovery.find_prometheus_url()
prom = PrometheusConnect(url=prometheus_base_url, disable_ssl=True)

promql_query = re.match(
Expand Down
1 change: 1 addition & 0 deletions src/robusta/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from ..integrations.scheduled.triggers import *
from ..integrations.git.git_repo import *
from ..integrations.resource_analysis.kubernetes_node_analyzer import NodeAnalyzer
from ..integrations.prometheus.utils import PrometheusDiscovery
from ..core.persistency.in_memory import get_persistent_data
from ..utils.rate_limiter import RateLimiter
from ..runner.object_updater import *
13 changes: 3 additions & 10 deletions src/robusta/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ def examples_download(
cluster_name: str = None,
use_robusta_ui: bool = False,
skip_robusta_sink: bool = False,
skip_new: bool = True,
robusta_ui_token: str = None,
url: str = None,
skip_integrations: bool = False,
Expand Down Expand Up @@ -195,14 +194,12 @@ def examples_download(
"Please specify a unique name for your cluster or press ENTER to use the default",
default=default_name,
)
# skip_new is used here, temporary, since we don't have the new fields in the released active_playbooks.yaml yet
# TODO remove on next release
if not skip_new and cluster_name is not None:
if cluster_name is not None:
replace_in_file(
"playbooks/active_playbooks.yaml", "<CLUSTER_NAME>", cluster_name.strip()
)

if not skip_new and (
if not skip_robusta_sink and (
use_robusta_ui or typer.confirm("Would you like to use Robusta UI?")
):
if robusta_ui_token is None:
Expand Down Expand Up @@ -249,10 +246,6 @@ def examples(
False,
help="Enable Robusta sink?",
),
skip_new: bool = typer.Option(
True,
help="Skip new config replacements?",
),
robusta_ui_token: str = typer.Option(
None,
help="Robusta UI account token",
Expand All @@ -266,13 +259,13 @@ def examples(
help="Skip integrations configuration",
),
):
"""Download playbooks code and configuration defaults"""
examples_download(
slack_api_key,
slack_channel,
cluster_name,
use_robusta_ui,
skip_robusta_sink,
skip_new,
robusta_ui_token,
url,
skip_integrations,
Expand Down
24 changes: 19 additions & 5 deletions src/robusta/integrations/prometheus/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
import logging

from ...utils.service_discovery import find_service_url


def find_prometheus_url():
"""
Try to autodiscover the url of an in-cluster grafana service
"""
return find_service_url("app=kube-prometheus-stack-prometheus")
class PrometheusDiscovery:
prometheus_url = None
@classmethod
def find_prometheus_url(cls):
"""
Try to autodiscover the url of an in-cluster grafana service
"""
if cls.prometheus_url:
return cls.prometheus_url
prometheus_selectors = ["app=kube-prometheus-stack-prometheus", "app.kubernetes.io/name=prometheus"]
for label_selector in prometheus_selectors:
service_url = find_service_url(label_selector)
if service_url:
cls.prometheus_url = service_url
return service_url
logging.error("Prometheus url could not be found. Add 'prometheus_url' under global_config")
return None
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from collections import OrderedDict
from hikaru.model import Node
from prometheus_api_client import PrometheusConnect
from ..prometheus.utils import find_prometheus_url
from ..prometheus.utils import PrometheusDiscovery


class NodeAnalyzer:
Expand All @@ -17,7 +17,8 @@ def __init__(self, node: Node, prometheus_url: str, range_size="5m"):
if addr.type == "InternalIP"
)
if prometheus_url is None:
prometheus_url = find_prometheus_url()
prometheus_url = PrometheusDiscovery.find_prometheus_url()

self.prom = PrometheusConnect(url=prometheus_url, disable_ssl=True)

def get_total_cpu_usage(self, other_method=False):
Expand Down
12 changes: 8 additions & 4 deletions src/robusta/utils/service_discovery.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
from kubernetes import client
from kubernetes.client import V1ServiceList
from kubernetes.client.models.v1_service import V1Service


Expand All @@ -9,14 +10,17 @@ def find_service_url(label_selector):
"""
# we do it this way because there is a weird issue with hikaru's ServiceList.listServiceForAllNamespaces()
v1 = client.CoreV1Api()
svc: V1Service = v1.list_service_for_all_namespaces(
svc_list: V1ServiceList = v1.list_service_for_all_namespaces(
label_selector=label_selector
).items[0]
)
if not svc_list.items:
return None
svc: V1Service = svc_list.items[0]
name = svc.metadata.name
namespace = svc.metadata.namespace
port = svc.spec.ports[0].port
url = f"http://{name}.{namespace}.svc:{port}"
logging.debug(
url = f"http://{name}.{namespace}.svc.cluster.local:{port}"
logging.info(
f"discovered service with label-selector: `{label_selector}` at url: `{url}`"
)
return url
1 change: 0 additions & 1 deletion tests/utils/robusta_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def cli_examples(self, playbooks_url: str, slack_channel: str, slack_api_key: st
"--robusta-ui-token",
"test-token",
"--skip-robusta-sink",
"--skip-new",
],
)
assert b"examples downloaded into the playbooks/ directory" in logs
Expand Down

0 comments on commit c76ee4b

Please sign in to comment.