|
| 1 | +import logging |
| 2 | +from kubernetes import client |
| 3 | +from kubernetes.client import V1ServiceList |
| 4 | +from kubernetes.client.models.v1_service import V1Service |
| 5 | +import os |
| 6 | +from typing import List, Optional |
| 7 | +from kubernetes import config |
| 8 | + |
| 9 | +CLUSTER_DOMAIN = os.environ.get("CLUSTER_DOMAIN", "cluster.local") |
| 10 | + |
| 11 | +try: |
| 12 | + if os.getenv("KUBERNETES_SERVICE_HOST"): |
| 13 | + config.load_incluster_config() |
| 14 | + else: |
| 15 | + config.load_kube_config() |
| 16 | +except config.config_exception.ConfigException as e: |
| 17 | + logging.warning(f"Running without kube-config! e={e}") |
| 18 | + |
| 19 | + |
| 20 | +def find_service_url(label_selector): |
| 21 | + """ |
| 22 | + Get the url of an in-cluster service with a specific label |
| 23 | + """ |
| 24 | + # we do it this way because there is a weird issue with hikaru's ServiceList.listServiceForAllNamespaces() |
| 25 | + try: |
| 26 | + v1 = client.CoreV1Api() |
| 27 | + svc_list: V1ServiceList = v1.list_service_for_all_namespaces( |
| 28 | + label_selector=label_selector |
| 29 | + ) |
| 30 | + if not svc_list.items: |
| 31 | + return None |
| 32 | + svc: V1Service = svc_list.items[0] |
| 33 | + name = svc.metadata.name |
| 34 | + namespace = svc.metadata.namespace |
| 35 | + port = svc.spec.ports[0].port |
| 36 | + url = f"http://{name}.{namespace}.svc.{CLUSTER_DOMAIN}:{port}" |
| 37 | + logging.info( |
| 38 | + f"discovered service with label-selector: `{label_selector}` at url: `{url}`" |
| 39 | + ) |
| 40 | + return url |
| 41 | + except Exception: |
| 42 | + logging.warning("Error finding url") |
| 43 | + return None |
| 44 | + |
| 45 | + |
| 46 | +class ServiceDiscovery: |
| 47 | + @classmethod |
| 48 | + def find_url(cls, selectors: List[str], error_msg: str) -> Optional[str]: |
| 49 | + """ |
| 50 | + Try to autodiscover the url of an in-cluster service |
| 51 | + """ |
| 52 | + |
| 53 | + for label_selector in selectors: |
| 54 | + service_url = find_service_url(label_selector) |
| 55 | + if service_url: |
| 56 | + return service_url |
| 57 | + |
| 58 | + logging.debug(error_msg) |
| 59 | + return None |
| 60 | + |
| 61 | + |
| 62 | +class PrometheusDiscovery(ServiceDiscovery): |
| 63 | + @classmethod |
| 64 | + def find_prometheus_url(cls) -> Optional[str]: |
| 65 | + return super().find_url( |
| 66 | + selectors=[ |
| 67 | + "app=kube-prometheus-stack-prometheus", |
| 68 | + "app=prometheus,component=server,release!=kubecost", |
| 69 | + "app=prometheus-server", |
| 70 | + "app=prometheus-operator-prometheus", |
| 71 | + "app=rancher-monitoring-prometheus", |
| 72 | + "app=prometheus-prometheus", |
| 73 | + "app.kubernetes.io/component=query,app.kubernetes.io/name=thanos", |
| 74 | + "app.kubernetes.io/name=thanos-query", |
| 75 | + "app=thanos-query", |
| 76 | + "app=thanos-querier", |
| 77 | + ], |
| 78 | + error_msg="Prometheus url could not be found. Add 'prometheus_url' under your prometheus tools config", |
| 79 | + ) |
| 80 | + |
| 81 | + @classmethod |
| 82 | + def find_vm_url(cls) -> Optional[str]: |
| 83 | + return super().find_url( |
| 84 | + selectors=[ |
| 85 | + "app.kubernetes.io/name=vmsingle", |
| 86 | + "app.kubernetes.io/name=victoria-metrics-single", |
| 87 | + "app.kubernetes.io/name=vmselect", |
| 88 | + "app=vmselect", |
| 89 | + ], |
| 90 | + error_msg="Victoria Metrics url could not be found. Add 'prometheus_url' under your prometheus tools config", |
| 91 | + ) |
0 commit comments