From 29de96b3ccea0f10ed86d7f9015d1bc3b7b90d5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=B0=D0=B2=D0=B5=D0=BB=20=D0=96=D1=83=D0=BA=D0=BE?= =?UTF-8?q?=D0=B2?= <33721692+LeaveMyYard@users.noreply.github.com> Date: Mon, 29 May 2023 13:40:57 +0300 Subject: Fix multi-cluster issues --- robusta_krr/utils/configurable.py | 2 +- robusta_krr/utils/service_discovery.py | 34 +++++++++++++++++++--------------- 2 files changed, 20 insertions(+), 16 deletions(-) (limited to 'robusta_krr/utils') 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 -- cgit v1.2.3