]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
src/pybind/mgr/cephadm/service_discovery: enhanced service to allow discovery of... 62302/head
authorBernard Landon <bernard@lndn.ch>
Fri, 14 Mar 2025 14:25:00 +0000 (14:25 +0000)
committerBernard Landon <bernard@lndn.ch>
Wed, 23 Apr 2025 20:19:55 +0000 (20:19 +0000)
Fixes: https://tracker.ceph.com/issues/70482
Signed-off-by: Bernard Landon <bernard@lndn.ch>
src/pybind/mgr/cephadm/service_discovery.py
src/pybind/mgr/cephadm/tests/test_service_discovery.py

index 1efd2dc1281aaae0f454d2f5608ab4d6285f4305..867a5ff39f12bd7bcf9323637c95164f45c4c4ed 100644 (file)
@@ -138,7 +138,7 @@ class Root(Server):
 
     @cherrypy.expose
     def index(self) -> str:
-        return '''<!DOCTYPE html>
+        return self.mgr.get_store('service_discovery/index') or '''<!DOCTYPE html>
 <html>
 <head><title>Cephadm HTTP Endpoint</title></head>
 <body>
@@ -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 <http_sd_config> 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."""
index 6f73cad91def05f5770b3818af80ada6b904cc83..d1203c54c04eb4ffd8609ea807cd07d2a751f6bb 100644 (file)
@@ -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')