-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jakub Smolar
committed
Jul 25, 2024
1 parent
aff3df9
commit 84aa4c7
Showing
11 changed files
with
191 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"""Kubernetes monitoring common objects""" | ||
|
||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class MetricsEndpoint: | ||
"""Dataclass for endpoint definition in ServiceMonitor Kubernetes object. | ||
It contains endpoint path and port to the exported metrics.""" | ||
|
||
path: str = "/metrics" | ||
port: str = "http" | ||
interval: str = "30s" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
"""Module implements Pod Monitor CR""" | ||
|
||
from testsuite.kubernetes import KubernetesObject | ||
from testsuite.kubernetes.client import KubernetesClient | ||
from testsuite.kubernetes.monitoring import MetricsEndpoint | ||
from testsuite.utils import asdict | ||
|
||
|
||
class PodMonitor(KubernetesObject): | ||
"""Represents Service Monitor object for OpenShift""" | ||
|
||
@classmethod | ||
def create_instance( | ||
cls, | ||
cluster: KubernetesClient, | ||
name: str, | ||
endpoints: list[MetricsEndpoint], | ||
match_labels: dict[str, str], | ||
labels: dict[str, str] = None, | ||
): | ||
"""Creates new instance of ServiceMonitor""" | ||
model = { | ||
"apiVersion": "monitoring.coreos.com/v1", | ||
"kind": "PodMonitor", | ||
"metadata": { | ||
"name": name, | ||
"labels": labels, | ||
}, | ||
"spec": { | ||
"podMetricsEndpoints": [asdict(e) for e in endpoints], | ||
"selector": { | ||
"matchLabels": match_labels, | ||
}, | ||
}, | ||
} | ||
|
||
return cls(model, context=cluster.context) |
15 changes: 2 additions & 13 deletions
15
testsuite/kubernetes/service_monitor.py → .../kubernetes/monitoring/service_monitor.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 2 additions & 34 deletions
36
testsuite/tests/singlecluster/authorino/metrics/conftest.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
24 changes: 24 additions & 0 deletions
24
testsuite/tests/singlecluster/limitador/metrics/conftest.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
"""Conftest for limitador metrics tests""" | ||
|
||
import pytest | ||
|
||
from testsuite.kubernetes.monitoring import MetricsEndpoint | ||
from testsuite.kubernetes.monitoring.pod_monitor import PodMonitor | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def pod_monitor(cluster, testconfig, request, blame, limitador): | ||
"""Creates Pod Monitor object to watch over '/metrics' endpoint of limitador pod""" | ||
project = cluster.change_project(testconfig["service_protection"]["system_project"]) | ||
|
||
endpoints = [MetricsEndpoint("/metrics", "http")] | ||
monitor = PodMonitor.create_instance(project, blame("pd"), endpoints, match_labels={"app": limitador.name()}) | ||
request.addfinalizer(monitor.delete) | ||
monitor.commit() | ||
return monitor | ||
|
||
|
||
@pytest.fixture(scope="module", autouse=True) | ||
def wait_for_active_targets(prometheus, pod_monitor): | ||
"""Waits for all endpoints in Pod Monitor to become active targets""" | ||
assert prometheus.is_reconciled(pod_monitor) |
46 changes: 46 additions & 0 deletions
46
testsuite/tests/singlecluster/limitador/metrics/test_metrics.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
"""Tests for Limitador metrics""" | ||
|
||
import pytest | ||
|
||
from testsuite.kuadrant.policy.rate_limit import Limit | ||
|
||
|
||
@pytest.fixture(scope="module", autouse=True) | ||
def rate_limit(rate_limit): | ||
"""Add limit to the policy""" | ||
rate_limit.add_limit("multiple", [Limit(3, 10)]) | ||
return rate_limit | ||
|
||
|
||
@pytest.fixture(scope="module", autouse=True) | ||
def wait_for_scrape(prometheus, pod_monitor, client): | ||
""" | ||
Creates 5 requests, from which 3 are authorized and 2 are rate limited. | ||
Waits until Prometheus scrapes '/metrics' endpoint. | ||
""" | ||
for _ in range(5): | ||
client.get("/get") | ||
prometheus.wait_for_scrape(pod_monitor, "/metrics") | ||
|
||
|
||
@pytest.mark.parametrize("metric, expected_value", [("authorized_calls", 3), ("limited_calls", 2)]) | ||
def test_calls_metric(prometheus, limitador, rate_limit, metric, expected_value): | ||
"""Tests that `authorized_calls` and `limited_calls` are emitted a correctly incremented""" | ||
metrics = prometheus.get_metrics( | ||
labels={"pod": limitador.pod.name(), "limitador_namespace": f"kuadrant/{rate_limit.name()}"} | ||
) | ||
|
||
authorized = metrics.filter(lambda x: x["metric"]["__name__"] == metric) | ||
assert len(authorized.metrics) == 1 | ||
assert authorized.values[0] == expected_value | ||
|
||
|
||
def test_limitador_status_metric(prometheus, limitador, pod_monitor): | ||
"""Tests that `limitador_up` metric is emitted""" | ||
metrics = prometheus.get_metrics( | ||
labels={"pod": limitador.pod.name(), "job": f"{pod_monitor.namespace()}/{pod_monitor.name()}"} | ||
) | ||
|
||
limitador_up = metrics.filter(lambda x: x["metric"]["__name__"] == "limitador_up") | ||
assert len(limitador_up.metrics) == 1 | ||
assert limitador_up.values[0] == 1 |