From 0c7e8289193988be29e289d034c7cb2a2b5ca48b Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Thu, 13 Feb 2020 09:11:25 -0600 Subject: [PATCH] mgr/cephadm: implement prometheus add/update Signed-off-by: Sage Weil --- src/pybind/mgr/cephadm/module.py | 34 ++++++++++++++++++++ src/pybind/mgr/cephadm/tests/test_cephadm.py | 19 +++++++++++ 2 files changed, 53 insertions(+) diff --git a/src/pybind/mgr/cephadm/module.py b/src/pybind/mgr/cephadm/module.py index bb0d420e8118a..66d9ccad1988a 100644 --- a/src/pybind/mgr/cephadm/module.py +++ b/src/pybind/mgr/cephadm/module.py @@ -2167,6 +2167,40 @@ scrape_configs: }) return j + def add_prometheus(self, spec): + spec = NodeAssignment(spec=spec, get_hosts_func=self._get_hosts, service_type='prometheus').load() + self.log.debug('nodes %s' % spec.placement.hosts) + + def _add(daemons): + args = [] + num_added = 0 + for host, _, name in spec.placement.hosts: + if num_added >= spec.count: + break + daemon_id = self.get_unique_name(host, daemons, None, name) + self.log.debug('placing prometheus.%s on host %s' % (daemon_id, + host)) + args.append((daemon_id, host)) + + # add to daemon list so next name(s) will also be unique + sd = orchestrator.ServiceDescription() + sd.service_instance = daemon_id + sd.service_type = 'prometheus' + sd.nodename = host + daemons.append(sd) + num_added += 1 + return self._create_prometheus(args) + + return self._get_daemons('prometheus').then(_add) + + @async_map_completion + def _create_prometheus(self, daemon_id, host): + return self._create_daemon('prometheus', daemon_id, host) + + def apply_prometheus(self, spec): + spec = NodeAssignment(spec=spec, get_hosts_func=self._get_hosts, service_type='prometheus').load() + return self._update_service('prometheus', self.add_prometheus, spec) + def _get_container_image_id(self, image_name): # pick a random host... host = None diff --git a/src/pybind/mgr/cephadm/tests/test_cephadm.py b/src/pybind/mgr/cephadm/tests/test_cephadm.py index 929edf3c59904..cf9b1bfe3c23d 100644 --- a/src/pybind/mgr/cephadm/tests/test_cephadm.py +++ b/src/pybind/mgr/cephadm/tests/test_cephadm.py @@ -273,6 +273,25 @@ class TestCephadm(object): match_glob(out, "Deployed rbd-mirror.* on host 'test'") + @mock.patch("cephadm.module.CephadmOrchestrator._run_cephadm", _run_cephadm('{}')) + @mock.patch("cephadm.module.CephadmOrchestrator.send_command") + @mock.patch("cephadm.module.CephadmOrchestrator.mon_command", mon_command) + @mock.patch("cephadm.module.CephadmOrchestrator._get_connection") + def test_prometheus(self, _send_command, _get_connection, cephadm_module): + with self._with_host(cephadm_module, 'test'): + ps = PlacementSpec(hosts=['test'], count=1) + + c = cephadm_module.add_prometheus(ServiceSpec(placement=ps)) + [out] = wait(cephadm_module, c) + assert "Deployed prometheus." in out + assert " on host 'test'" in out + + ps = PlacementSpec(hosts=['test'], count=2) + c = cephadm_module.apply_prometheus(ServiceSpec(placement=ps)) + [out] = wait(cephadm_module, c) + assert "Deployed prometheus." in out + assert " on host 'test'" in out + @mock.patch("cephadm.module.CephadmOrchestrator._run_cephadm", _run_cephadm('{}')) @mock.patch("cephadm.module.CephadmOrchestrator.send_command") @mock.patch("cephadm.module.CephadmOrchestrator.mon_command", mon_command) -- 2.39.5