From 6745ec3fae700322b158dc9473409e1bb377abd3 Mon Sep 17 00:00:00 2001 From: Bernard Landon Date: Fri, 14 Mar 2025 14:25:00 +0000 Subject: [PATCH] src/pybind/mgr/cephadm/service_discovery: enhanced service to allow discovery of custom containers Fixes: https://tracker.ceph.com/issues/70482 Signed-off-by: Bernard Landon --- src/pybind/mgr/cephadm/service_discovery.py | 19 ++++++++++++++++++- .../cephadm/tests/test_service_discovery.py | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/pybind/mgr/cephadm/service_discovery.py b/src/pybind/mgr/cephadm/service_discovery.py index 1efd2dc1281aa..867a5ff39f12b 100644 --- a/src/pybind/mgr/cephadm/service_discovery.py +++ b/src/pybind/mgr/cephadm/service_discovery.py @@ -138,7 +138,7 @@ class Root(Server): @cherrypy.expose def index(self) -> str: - return ''' + return self.mgr.get_store('service_discovery/index') or ''' Cephadm HTTP Endpoint @@ -175,6 +175,8 @@ class Root(Server): return self.nfs_sd_config() elif service == 'smb': return self.smb_sd_config() + elif service.startswith("container"): + return self.container_sd_config(service) else: return [] @@ -282,6 +284,21 @@ class Root(Server): }) return srv_entries + def container_sd_config(self, service: str) -> List[Dict[str, Collection[str]]]: + """Return compatible prometheus config for a container service.""" + srv_entries = [] + for dd in self.mgr.cache.get_daemons_by_service(service): + assert dd.hostname is not None + addr = dd.ip if dd.ip else self.mgr.inventory.get_addr(dd.hostname) + if not dd.ports: + continue + port = dd.ports[0] + srv_entries.append({ + 'targets': [build_url(host=addr, port=port).lstrip('/')], + 'labels': {'instance': dd.hostname} + }) + return srv_entries + @cherrypy.expose(alias='prometheus/rules') def get_prometheus_rules(self) -> str: """Return currently configured prometheus rules as Yaml.""" diff --git a/src/pybind/mgr/cephadm/tests/test_service_discovery.py b/src/pybind/mgr/cephadm/tests/test_service_discovery.py index 6f73cad91def0..d1203c54c04eb 100644 --- a/src/pybind/mgr/cephadm/tests/test_service_discovery.py +++ b/src/pybind/mgr/cephadm/tests/test_service_discovery.py @@ -31,6 +31,10 @@ class FakeCache: return [FakeDaemonDescription('1.2.3.4', [9922], 'node0'), FakeDaemonDescription('1.2.3.5', [9922], 'node1')] + if service_type == 'container.custom-container': + return [FakeDaemonDescription('1.2.3.4', [9123], 'node0'), + FakeDaemonDescription('1.2.3.5', [9123], 'node1')] + return [FakeDaemonDescription('1.2.3.4', [9100], 'node0'), FakeDaemonDescription('1.2.3.5', [9200], 'node1')] @@ -224,6 +228,20 @@ class TestServiceDiscovery: # check content assert cfg[0]['targets'] == ['1.2.3.4:9922'] + def test_get_sd_config_custom_container(self): + mgr = FakeMgr() + root = Root(mgr, 5000, '0.0.0.0') + cfg = root.get_sd_config('container.custom-container') + + # check response structure + assert cfg + for entry in cfg: + assert 'labels' in entry + assert 'targets' in entry + + # check content + assert cfg[0]['targets'] == ['1.2.3.4:9123'] + def test_get_sd_config_invalid_service(self): mgr = FakeMgr() root = Root(mgr, 5000, '0.0.0.0') -- 2.39.5