summaryrefslogtreecommitdiff
path: root/robusta_krr/utils
diff options
context:
space:
mode:
authorПавел Жуков <33721692+LeaveMyYard@users.noreply.github.com>2023-05-29 13:40:57 +0300
committerПавел Жуков <33721692+LeaveMyYard@users.noreply.github.com>2023-05-29 13:40:57 +0300
commit29de96b3ccea0f10ed86d7f9015d1bc3b7b90d5a (patch)
treef82def135c74924729ab5ae14e9716df12b3684c /robusta_krr/utils
parent110da668bee9f87e0f98a0e41ef32dee4984dddf (diff)
Fix multi-cluster issues
Diffstat (limited to 'robusta_krr/utils')
-rw-r--r--robusta_krr/utils/configurable.py2
-rw-r--r--robusta_krr/utils/service_discovery.py34
2 files changed, 20 insertions, 16 deletions
diff --git a/robusta_krr/utils/configurable.py b/robusta_krr/utils/configurable.py
index 4777a9c..a6cb92f 100644
--- a/robusta_krr/utils/configurable.py
+++ b/robusta_krr/utils/configurable.py
@@ -15,7 +15,7 @@ class Configurable(abc.ABC):
def __init__(self, config: Config) -> None:
self.config = config
- self.console = Console(stderr=self.config.log_to_stderr)
+ self.console: Console = self.config.console # type: ignore
@property
def debug_active(self) -> bool:
diff --git a/robusta_krr/utils/service_discovery.py b/robusta_krr/utils/service_discovery.py
index b718700..8ba58ca 100644
--- a/robusta_krr/utils/service_discovery.py
+++ b/robusta_krr/utils/service_discovery.py
@@ -1,4 +1,3 @@
-import logging
from typing import Optional
from cachetools import TTLCache
@@ -7,6 +6,7 @@ from kubernetes.client import V1IngressList, V1ServiceList
from kubernetes.client.api_client import ApiClient
from kubernetes.client.models.v1_ingress import V1Ingress
from kubernetes.client.models.v1_service import V1Service
+from robusta_krr.core.models.config import Config
from robusta_krr.utils.configurable import Configurable
@@ -15,12 +15,16 @@ class ServiceDiscovery(Configurable):
SERVICE_CACHE_TTL_SEC = 900
cache: TTLCache = TTLCache(maxsize=1, ttl=SERVICE_CACHE_TTL_SEC)
- def find_service_url(self, label_selector: str, *, api_client: Optional[ApiClient] = None) -> Optional[str]:
+ def __init__(self, config: Config, api_client: Optional[ApiClient] = None) -> None:
+ super().__init__(config)
+ self.api_client = api_client
+
+ def find_service_url(self, label_selector: str) -> Optional[str]:
"""
Get the url of an in-cluster service with a specific label
"""
# we do it this way because there is a weird issue with hikaru's ServiceList.listServiceForAllNamespaces()
- v1 = client.CoreV1Api(api_client=api_client)
+ v1 = client.CoreV1Api(api_client=self.api_client)
svc_list: V1ServiceList = v1.list_service_for_all_namespaces(label_selector=label_selector)
if not svc_list.items:
return None
@@ -33,19 +37,19 @@ class ServiceDiscovery(Configurable):
if self.config.inside_cluster:
return f"http://{name}.{namespace}.svc.cluster.local:{port}"
- elif api_client is not None:
- return f"{api_client.configuration.host}/api/v1/namespaces/{namespace}/services/{name}:{port}/proxy"
+ elif self.api_client is not None:
+ return f"{self.api_client.configuration.host}/api/v1/namespaces/{namespace}/services/{name}:{port}/proxy"
return None
- def find_ingress_host(self, label_selector: str, *, api_client: Optional[ApiClient] = None) -> Optional[str]:
+ def find_ingress_host(self, label_selector: str) -> Optional[str]:
"""
Discover the ingress host of the Prometheus if krr is not running in cluster
"""
if self.config.inside_cluster:
return None
- v1 = client.NetworkingV1Api(api_client=api_client)
+ v1 = client.NetworkingV1Api(api_client=self.api_client)
ingress_list: V1IngressList = v1.list_ingress_for_all_namespaces(label_selector=label_selector)
if not ingress_list.items:
return None
@@ -54,26 +58,26 @@ class ServiceDiscovery(Configurable):
prometheus_host = ingress.spec.rules[0].host
return f"http://{prometheus_host}"
- def find_url(self, selectors: list[str], *, api_client: Optional[ApiClient] = None) -> Optional[str]:
+ def find_url(self, selectors: list[str]) -> Optional[str]:
"""
Try to autodiscover the url of an in-cluster service
"""
- cache_key = ",".join(selectors)
+ cache_key = ",".join(selectors + [self.api_client.configuration.host if self.api_client else ""])
cached_value = self.cache.get(cache_key)
if cached_value:
return cached_value
for label_selector in selectors:
- logging.debug(f"Trying to find service with label selector {label_selector}")
- service_url = self.find_service_url(label_selector, api_client=api_client)
+ self.debug(f"Trying to find service with label selector {label_selector}")
+ service_url = self.find_service_url(label_selector)
if service_url:
- logging.debug(f"Found service with label selector {label_selector}")
+ self.debug(f"Found service with label selector {label_selector}")
self.cache[cache_key] = service_url
return service_url
- logging.debug(f"Trying to find ingress with label selector {label_selector}")
- self.find_ingress_host(label_selector, api_client=api_client)
- ingress_url = self.find_ingress_host(label_selector, api_client=api_client)
+ self.debug(f"Trying to find ingress with label selector {label_selector}")
+ self.find_ingress_host(label_selector)
+ ingress_url = self.find_ingress_host(label_selector)
if ingress_url:
return ingress_url