Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,10 @@ krr simple --help

<!-- Port-forwarding -->

## Prometheus auto-discovery
## Prometheus, Victoria Metrics and Thanos auto-discovery

By default, KRR will try to auto-discover the running Prometheus by scanning those labels:
By default, KRR will try to auto-discover the running Prometheus Victoria Metrics and Thanos.
For discovering prometheus it scan services for those labels:
```python
"app=kube-prometheus-stack-prometheus"
"app=prometheus,component=server"
Expand All @@ -286,8 +287,22 @@ By default, KRR will try to auto-discover the running Prometheus by scanning tho
"app=rancher-monitoring-prometheus"
"app=prometheus-prometheus"
```
For Thanos its these labels:
```python
"app.kubernetes.io/component=query,app.kubernetes.io/name=thanos",
"app.kubernetes.io/name=thanos-query",
"app=thanos-query",
"app=thanos-querier",
```
And for Victoria Metrics its the following labels:
```python
"app.kubernetes.io/name=vmsingle",
"app.kubernetes.io/name=victoria-metrics-single",
"app.kubernetes.io/name=vmselect",
"app=vmselect",
```

If none of those labels result in finding Prometheus, you will get an error and will have to pass the working url explicitly (using the `-p` flag).
If none of those labels result in finding Prometheus, Victoria Metrics or Thanos, you will get an error and will have to pass the working url explicitly (using the `-p` flag).

<p align="right">(<a href="#readme-top">back to top</a>)</p>

Expand All @@ -309,6 +324,18 @@ krr simple -p http://127.0.0.1:9090

<p align="right">(<a href="#readme-top">back to top</a>)</p>

## Scanning with a centralized Prometheus

If your Prometheus monitors multiple clusters you we require the label you defined for your cluster in Prometheus.

For example, if your cluster has the Prometheus label `cluster: "my-cluster-name"` and your prometheus is at url `http://my-centralized-prometheus:9090`, then run this command:

```sh
krr.py simple -p http://my-centralized-prometheus:9090 -l my-cluster-name
```

<p align="right">(<a href="#readme-top">back to top</a>)</p>

<!-- Formatters -->

## Available formatters
Expand Down
7 changes: 6 additions & 1 deletion robusta_krr/core/integrations/prometheus/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
from .loader import MetricsLoader
from .metrics_service.prometheus_metrics_service import CustomPrometheusConnect, PrometheusDiscovery, PrometheusNotFound
from .metrics_service.prometheus_metrics_service import (
ClusterNotSpecifiedException,
CustomPrometheusConnect,
PrometheusDiscovery,
PrometheusNotFound,
)
1 change: 1 addition & 0 deletions robusta_krr/core/integrations/prometheus/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def get_metrics_service(
loader = metric_service_class(config, api_client=api_client, cluster=cluster)
loader.check_connection()
self.echo(f"{service_name} found")
loader.validate_cluster_name()
return loader
except MetricsNotFound as e:
self.debug(f"{service_name} not found")
Expand Down
11 changes: 11 additions & 0 deletions robusta_krr/core/integrations/prometheus/metrics/base_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ def __init__(self, config: Config, prometheus: CustomPrometheusConnect) -> None:
super().__init__(config)
self.prometheus = prometheus

def get_prometheus_cluster_label(self) -> str:
"""
Generates the cluster label for querying a centralized Prometheus

Returns:
str: a promql safe label string for querying the cluster.
"""
if self.config.prometheus_cluster_label is None:
return ""
return f', cluster="{self.config.prometheus_cluster_label}"'

@abc.abstractmethod
def get_query(self, object: K8sObjectData) -> str:
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
class CPUMetricLoader(BaseFilteredMetricLoader):
def get_query(self, object: K8sObjectData) -> str:
pods_selector = "|".join(pod.name for pod in object.pods)
cluster_label = self.get_prometheus_cluster_label()
return (
"sum(irate(container_cpu_usage_seconds_total{"
f'namespace="{object.namespace}", '
f'pod=~"{pods_selector}", '
f'container="{object.container}"'
f"{cluster_label}"
"}[5m])) by (container, pod, job)"
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
class MemoryMetricLoader(BaseFilteredMetricLoader):
def get_query(self, object: K8sObjectData) -> str:
pods_selector = "|".join(pod.name for pod in object.pods)
cluster_label = self.get_prometheus_cluster_label()
return (
"sum(container_memory_working_set_bytes{"
f'namespace="{object.namespace}", '
f'pod=~"{pods_selector}", '
f'container="{object.container}"'
f"{cluster_label}"
"}) by (container, pod, job)"
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import abc
import datetime
from typing import Optional
from typing import List, Optional

from kubernetes.client.api_client import ApiClient

Expand Down Expand Up @@ -38,6 +38,10 @@ def name(self) -> str:
classname = self.__class__.__name__
return classname.replace("MetricsService", "") if classname != MetricsService.__name__ else classname

@abc.abstractmethod
async def get_cluster_names(self) -> Optional[List[str]]:
...

@abc.abstractmethod
async def gather_data(
self,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import asyncio
import datetime
from typing import Optional, no_type_check
from typing import List, Optional, no_type_check

import requests
from kubernetes.client import ApiClient
Expand Down Expand Up @@ -50,6 +50,14 @@ class PrometheusNotFound(MetricsNotFound):
pass


class ClusterNotSpecifiedException(Exception):
"""
An exception raised when a prometheus requires a cluster label but an invalid one is provided.
"""

pass


class CustomPrometheusConnect(PrometheusConnect):
"""
Custom PrometheusConnect class to handle retries.
Expand Down Expand Up @@ -134,6 +142,26 @@ def check_connection(self):
async def query(self, query: str) -> dict:
return await asyncio.to_thread(self.prometheus.custom_query, query=query)

def validate_cluster_name(self):
cluster_label = self.config.prometheus_cluster_label
cluster_names = self.get_cluster_names()

if len(cluster_names) <= 1:
# there is only one cluster of metrics in this prometheus
return

if not cluster_label:
raise ClusterNotSpecifiedException(
f"No label specified, Rerun krr with the flag `-l <cluster>` where <cluster> is one of {cluster_names}"
)
if cluster_label not in cluster_names:
raise ClusterNotSpecifiedException(
f"Label {cluster_label} does not exist, Rerun krr with the flag `-l <cluster>` where <cluster> is one of {cluster_names}"
)

def get_cluster_names(self) -> Optional[List[str]]:
return self.prometheus.get_label_values(label_name="cluster")

async def gather_data(
self,
object: K8sObjectData,
Expand Down
1 change: 1 addition & 0 deletions robusta_krr/core/models/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Config(pd.BaseSettings):
prometheus_url: Optional[str] = pd.Field(None)
prometheus_auth_header: Optional[str] = pd.Field(None)
prometheus_ssl_enabled: bool = pd.Field(False)
prometheus_cluster_label: Optional[str] = pd.Field(None)

# Logging Settings
format: str
Expand Down
3 changes: 2 additions & 1 deletion robusta_krr/core/models/objects.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Optional

import pydantic as pd

from typing import Optional
from robusta_krr.core.models.allocations import ResourceAllocations


Expand Down
13 changes: 11 additions & 2 deletions robusta_krr/core/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from robusta_krr.core.abstract.strategies import ResourceRecommendation, RunResult
from robusta_krr.core.integrations.kubernetes import KubernetesLoader
from robusta_krr.core.integrations.prometheus import MetricsLoader, PrometheusNotFound
from robusta_krr.core.integrations.prometheus import ClusterNotSpecifiedException, MetricsLoader, PrometheusNotFound
from robusta_krr.core.models.config import Config
from robusta_krr.core.models.objects import K8sObjectData
from robusta_krr.core.models.result import MetricsData, ResourceAllocations, ResourceScan, ResourceType, Result
Expand Down Expand Up @@ -139,6 +139,13 @@ async def _gather_objects_recommendations(

async def _collect_result(self) -> Result:
clusters = await self._k8s_loader.list_clusters()
if len(clusters) > 1 and self.config.prometheus_url:
# this can only happen for multi-cluster querying a single centeralized prometheus
# In this scenario we dont yet support determining which metrics belong to which cluster so the reccomendation can be incorrect
raise ClusterNotSpecifiedException(
f"Cannot scan multiple clusters for this prometheus, Rerun with the flag `-c <cluster>` where <cluster> is one of {clusters}"
)

self.info(f'Using clusters: {clusters if clusters is not None else "inner cluster"}')
objects = await self._k8s_loader.list_scannable_objects(clusters)

Expand Down Expand Up @@ -173,5 +180,7 @@ async def run(self) -> None:
try:
result = await self._collect_result()
self._process_result(result)
except Exception:
except ClusterNotSpecifiedException as e:
self.error(e)
except Exception as e:
self.console.print_exception(extra_lines=1, max_frames=10)
8 changes: 8 additions & 0 deletions robusta_krr/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ def {func_name}(
help="Enable SSL for Prometheus requests.",
rich_help_panel="Prometheus Settings",
),
prometheus_cluster_label: Optional[str] = typer.Option(
None,
"--prometheus-cluster-label",
"-l",
help="The label in prometheus for your cluster.(Only relevant for centralized prometheus)",
rich_help_panel="Prometheus Settings",
),
format: str = typer.Option("table", "--formatter", "-f", help="Output formatter ({formatters})", rich_help_panel="Logging Settings"),
verbose: bool = typer.Option(False, "--verbose", "-v", help="Enable verbose mode", rich_help_panel="Logging Settings"),
quiet: bool = typer.Option(False, "--quiet", "-q", help="Enable quiet mode", rich_help_panel="Logging Settings"),
Expand All @@ -106,6 +113,7 @@ def {func_name}(
prometheus_url=prometheus_url,
prometheus_auth_header=prometheus_auth_header,
prometheus_ssl_enabled=prometheus_ssl_enabled,
prometheus_cluster_label=prometheus_cluster_label,
format=format,
verbose=verbose,
quiet=quiet,
Expand Down