From 175ee8390985ae12b1b67d602338a4d1b1c81b52 Mon Sep 17 00:00:00 2001 From: Sebastian Wagner Date: Tue, 23 Feb 2021 15:42:01 +0100 Subject: [PATCH] mgr/orch: replace def add_{type}(...) with generic add_daemon() Signed-off-by: Sebastian Wagner --- doc/mgr/orchestrator_modules.rst | 13 +--- src/pybind/mgr/cephadm/module.py | 75 +++----------------- src/pybind/mgr/cephadm/tests/test_cephadm.py | 52 +++++++------- src/pybind/mgr/orchestrator/_interface.py | 52 +------------- src/pybind/mgr/orchestrator/module.py | 33 +-------- src/pybind/mgr/test_orchestrator/module.py | 17 +---- 6 files changed, 41 insertions(+), 201 deletions(-) diff --git a/doc/mgr/orchestrator_modules.rst b/doc/mgr/orchestrator_modules.rst index 4e021fddff376..65e2e4981d133 100644 --- a/doc/mgr/orchestrator_modules.rst +++ b/doc/mgr/orchestrator_modules.rst @@ -279,20 +279,13 @@ Phase two is a call to :meth:`Orchestrator.create_osds` with a Drive Group with .. py:currentmodule:: orchestrator -Monitors +Services -------- -.. automethod:: Orchestrator.add_mon +.. automethod:: Orchestrator.add_daemon .. automethod:: Orchestrator.apply_mon - -Stateless Services ------------------- - -.. automethod:: Orchestrator.add_mgr .. automethod:: Orchestrator.apply_mgr -.. automethod:: Orchestrator.add_mds .. automethod:: Orchestrator.apply_mds -.. automethod:: Orchestrator.add_rbd_mirror .. automethod:: Orchestrator.apply_rbd_mirror .. py:currentmodule:: ceph.deployment.service_spec @@ -301,7 +294,6 @@ Stateless Services .. py:currentmodule:: orchestrator -.. automethod:: Orchestrator.add_rgw .. automethod:: Orchestrator.apply_rgw .. py:currentmodule:: ceph.deployment.service_spec @@ -310,7 +302,6 @@ Stateless Services .. py:currentmodule:: orchestrator -.. automethod:: Orchestrator.add_nfs .. automethod:: Orchestrator.apply_nfs Upgrades diff --git a/src/pybind/mgr/cephadm/module.py b/src/pybind/mgr/cephadm/module.py index a04f5e38bef91..6b07d4a97f68d 100644 --- a/src/pybind/mgr/cephadm/module.py +++ b/src/pybind/mgr/cephadm/module.py @@ -37,7 +37,8 @@ import orchestrator from orchestrator.module import to_format, Format from orchestrator import OrchestratorError, OrchestratorValidationError, HostSpec, \ - CLICommandMeta, DaemonDescription, DaemonDescriptionStatus, handle_orch_error + CLICommandMeta, DaemonDescription, DaemonDescriptionStatus, handle_orch_error, \ + service_to_daemon_types from orchestrator._interface import GenericSpec from orchestrator._interface import daemon_type_to_service @@ -2058,18 +2059,15 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule, return create_func_map(args) @handle_orch_error - def apply_mon(self, spec: ServiceSpec) -> str: - return self._apply(spec) - - @handle_orch_error - def add_mon(self, spec): - # type: (ServiceSpec) -> List[str] - return self._add_daemon('mon', spec) + def add_daemon(self, spec: ServiceSpec) -> List[str]: + ret: List[str] = [] + for d_type in service_to_daemon_types(spec.service_type): + ret.extend(self._add_daemon(d_type, spec)) + return ret @handle_orch_error - def add_mgr(self, spec): - # type: (ServiceSpec) -> List[str] - return self._add_daemon('mgr', spec) + def apply_mon(self, spec: ServiceSpec) -> str: + return self._apply(spec) def _apply(self, spec: GenericSpec) -> str: if spec.service_type == 'host': @@ -2168,18 +2166,10 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule, def apply_mgr(self, spec: ServiceSpec) -> str: return self._apply(spec) - @handle_orch_error - def add_mds(self, spec: ServiceSpec) -> List[str]: - return self._add_daemon('mds', spec) - @handle_orch_error def apply_mds(self, spec: ServiceSpec) -> str: return self._apply(spec) - @handle_orch_error - def add_rgw(self, spec: ServiceSpec) -> List[str]: - return self._add_daemon('rgw', spec) - @handle_orch_error def apply_rgw(self, spec: ServiceSpec) -> str: return self._apply(spec) @@ -2188,27 +2178,14 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule, def apply_ha_rgw(self, spec: ServiceSpec) -> str: return self._apply(spec) - @handle_orch_error - def add_iscsi(self, spec): - # type: (ServiceSpec) -> List[str] - return self._add_daemon('iscsi', spec) - @handle_orch_error def apply_iscsi(self, spec: ServiceSpec) -> str: return self._apply(spec) - @handle_orch_error - def add_rbd_mirror(self, spec: ServiceSpec) -> List[str]: - return self._add_daemon('rbd-mirror', spec) - @handle_orch_error def apply_rbd_mirror(self, spec: ServiceSpec) -> str: return self._apply(spec) - @handle_orch_error - def add_nfs(self, spec: ServiceSpec) -> List[str]: - return self._add_daemon('nfs', spec) - @handle_orch_error def apply_nfs(self, spec: ServiceSpec) -> str: return self._apply(spec) @@ -2217,64 +2194,30 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule, # type: () -> str return self.get('mgr_map').get('services', {}).get('dashboard', '') - @handle_orch_error - def add_prometheus(self, spec: ServiceSpec) -> List[str]: - return self._add_daemon('prometheus', spec) - @handle_orch_error def apply_prometheus(self, spec: ServiceSpec) -> str: return self._apply(spec) - @handle_orch_error - def add_node_exporter(self, spec): - # type: (ServiceSpec) -> List[str] - return self._add_daemon('node-exporter', spec) - @handle_orch_error def apply_node_exporter(self, spec: ServiceSpec) -> str: return self._apply(spec) - @handle_orch_error - def add_crash(self, spec): - # type: (ServiceSpec) -> List[str] - return self._add_daemon('crash', spec) - @handle_orch_error def apply_crash(self, spec: ServiceSpec) -> str: return self._apply(spec) - @handle_orch_error - def add_grafana(self, spec): - # type: (ServiceSpec) -> List[str] - return self._add_daemon('grafana', spec) - @handle_orch_error def apply_grafana(self, spec: ServiceSpec) -> str: return self._apply(spec) - @handle_orch_error - def add_alertmanager(self, spec): - # type: (ServiceSpec) -> List[str] - return self._add_daemon('alertmanager', spec) - @handle_orch_error def apply_alertmanager(self, spec: ServiceSpec) -> str: return self._apply(spec) - @handle_orch_error - def add_container(self, spec: ServiceSpec) -> List[str]: - return self._add_daemon('container', spec) - @handle_orch_error def apply_container(self, spec: ServiceSpec) -> str: return self._apply(spec) - @handle_orch_error - def add_cephadm_exporter(self, spec): - # type: (ServiceSpec) -> List[str] - return self._add_daemon('cephadm-exporter', - spec) - @handle_orch_error def apply_cephadm_exporter(self, spec: ServiceSpec) -> str: return self._apply(spec) diff --git a/src/pybind/mgr/cephadm/tests/test_cephadm.py b/src/pybind/mgr/cephadm/tests/test_cephadm.py index 9c1a7b7db07c3..7b3985b35ef42 100644 --- a/src/pybind/mgr/cephadm/tests/test_cephadm.py +++ b/src/pybind/mgr/cephadm/tests/test_cephadm.py @@ -52,10 +52,10 @@ def assert_rm_daemon(cephadm: CephadmOrchestrator, prefix, host): @contextmanager -def with_daemon(cephadm_module: CephadmOrchestrator, spec: ServiceSpec, meth, host: str): +def with_daemon(cephadm_module: CephadmOrchestrator, spec: ServiceSpec, host: str): spec.placement = PlacementSpec(hosts=[host], count=1) - c = meth(cephadm_module, spec) + c = cephadm_module.add_daemon(spec) [out] = wait(cephadm_module, c) match_glob(out, f"Deployed {spec.service_name()}.* on host '{host}'") @@ -106,7 +106,7 @@ class TestCephadm(object): c = cephadm_module.list_daemons(refresh=True) assert wait(cephadm_module, c) == [] with with_service(cephadm_module, ServiceSpec('mds', 'name', unmanaged=True)) as _, \ - with_daemon(cephadm_module, ServiceSpec('mds', 'name'), CephadmOrchestrator.add_mds, 'test') as _: + with_daemon(cephadm_module, ServiceSpec('mds', 'name'), 'test') as _: c = cephadm_module.list_daemons() @@ -184,7 +184,7 @@ class TestCephadm(object): cephadm_module.service_cache_timeout = 10 with with_host(cephadm_module, 'test'): with with_service(cephadm_module, RGWSpec(service_id='myrgw.foobar', unmanaged=True)) as _, \ - with_daemon(cephadm_module, RGWSpec(service_id='myrgw.foobar'), CephadmOrchestrator.add_rgw, 'test') as daemon_id: + with_daemon(cephadm_module, RGWSpec(service_id='myrgw.foobar'), 'test') as daemon_id: c = cephadm_module.daemon_action('redeploy', 'rgw.' + daemon_id) assert wait(cephadm_module, @@ -209,7 +209,7 @@ class TestCephadm(object): cephadm_module.service_cache_timeout = 10 with with_host(cephadm_module, 'test'): with with_service(cephadm_module, RGWSpec(service_id='myrgw.foobar', unmanaged=True)) as _, \ - with_daemon(cephadm_module, RGWSpec(service_id='myrgw.foobar'), CephadmOrchestrator.add_rgw, 'test') as daemon_id: + with_daemon(cephadm_module, RGWSpec(service_id='myrgw.foobar'), 'test') as daemon_id: with mock.patch('ceph_module.BaseMgrModule._ceph_send_command') as _ceph_send_command: _ceph_send_command.side_effect = Exception("myerror") @@ -321,12 +321,12 @@ class TestCephadm(object): with with_host(cephadm_module, 'test'): with with_service(cephadm_module, ServiceSpec(service_type='mon', unmanaged=True)): ps = PlacementSpec(hosts=['test:0.0.0.0=a'], count=1) - c = cephadm_module.add_mon(ServiceSpec('mon', placement=ps)) + c = cephadm_module.add_daemon(ServiceSpec('mon', placement=ps)) assert wait(cephadm_module, c) == ["Deployed mon.a on host 'test'"] with pytest.raises(OrchestratorError, match="Must set public_network config option or specify a CIDR network,"): ps = PlacementSpec(hosts=['test'], count=1) - c = cephadm_module.add_mon(ServiceSpec('mon', placement=ps)) + c = cephadm_module.add_daemon(ServiceSpec('mon', placement=ps)) wait(cephadm_module, c) @mock.patch("cephadm.serve.CephadmServe._run_cephadm", _run_cephadm('[]')) @@ -658,7 +658,7 @@ class TestCephadm(object): with with_host(cephadm_module, 'host2'): with with_service(cephadm_module, RGWSpec(service_id="foo", unmanaged=True)): ps = PlacementSpec(hosts=['host1'], count=1) - c = cephadm_module.add_rgw( + c = cephadm_module.add_daemon( RGWSpec(service_id="foo", placement=ps)) [out] = wait(cephadm_module, c) match_glob(out, "Deployed rgw.foo.* on host 'host1'") @@ -693,28 +693,28 @@ class TestCephadm(object): assert out == ["Removed rgw.myrgw.myhost.myid from host 'test'"] @pytest.mark.parametrize( - "spec, meth", + "spec", [ - (ServiceSpec('crash'), CephadmOrchestrator.add_crash), - (ServiceSpec('prometheus'), CephadmOrchestrator.add_prometheus), - (ServiceSpec('grafana'), CephadmOrchestrator.add_grafana), - (ServiceSpec('node-exporter'), CephadmOrchestrator.add_node_exporter), - (ServiceSpec('alertmanager'), CephadmOrchestrator.add_alertmanager), - (ServiceSpec('rbd-mirror'), CephadmOrchestrator.add_rbd_mirror), - (ServiceSpec('mds', service_id='fsname'), CephadmOrchestrator.add_mds), - (RGWSpec(rgw_realm='realm', rgw_zone='zone'), CephadmOrchestrator.add_rgw), - (RGWSpec(service_id="foo"), CephadmOrchestrator.add_rgw), - (ServiceSpec('cephadm-exporter'), CephadmOrchestrator.add_cephadm_exporter), + ServiceSpec('crash'), + ServiceSpec('prometheus'), + ServiceSpec('grafana'), + ServiceSpec('node-exporter'), + ServiceSpec('alertmanager'), + ServiceSpec('rbd-mirror'), + ServiceSpec('mds', service_id='fsname'), + RGWSpec(rgw_realm='realm', rgw_zone='zone'), + RGWSpec(service_id="foo"), + ServiceSpec('cephadm-exporter'), ] ) @mock.patch("cephadm.serve.CephadmServe._deploy_cephadm_binary", _deploy_cephadm_binary('test')) @mock.patch("cephadm.serve.CephadmServe._run_cephadm", _run_cephadm('{}')) - def test_daemon_add(self, spec: ServiceSpec, meth, cephadm_module): + def test_daemon_add(self, spec: ServiceSpec, cephadm_module): unmanaged_spec = ServiceSpec.from_json(spec.to_json()) unmanaged_spec.unmanaged = True with with_host(cephadm_module, 'test'): with with_service(cephadm_module, unmanaged_spec): - with with_daemon(cephadm_module, spec, meth, 'test'): + with with_daemon(cephadm_module, spec, 'test'): pass @mock.patch("cephadm.serve.CephadmServe._run_cephadm") @@ -729,7 +729,7 @@ class TestCephadm(object): with with_service(cephadm_module, spec): _run_cephadm.side_effect = OrchestratorError('fail') with pytest.raises(OrchestratorError): - wait(cephadm_module, cephadm_module.add_mgr(spec)) + wait(cephadm_module, cephadm_module.add_daemon(spec)) cephadm_module.assert_issued_mon_command({ 'prefix': 'auth rm', 'entity': 'mgr.x', @@ -748,7 +748,7 @@ class TestCephadm(object): unmanaged_spec = ServiceSpec.from_json(spec.to_json()) unmanaged_spec.unmanaged = True with with_service(cephadm_module, unmanaged_spec): - c = cephadm_module.add_nfs(spec) + c = cephadm_module.add_daemon(spec) [out] = wait(cephadm_module, c) match_glob(out, "Deployed nfs.name.* on host 'test'") @@ -769,7 +769,7 @@ class TestCephadm(object): unmanaged_spec.unmanaged = True with with_service(cephadm_module, unmanaged_spec): - c = cephadm_module.add_iscsi(spec) + c = cephadm_module.add_daemon(spec) [out] = wait(cephadm_module, c) match_glob(out, "Deployed iscsi.name.* on host 'test'") @@ -979,14 +979,14 @@ class TestCephadm(object): """ fuse = False - @staticmethod + @ staticmethod def has_connection(): return False def import_module(self, *args, **kargs): return mock.Mock() - @staticmethod + @ staticmethod def exit(): pass diff --git a/src/pybind/mgr/orchestrator/_interface.py b/src/pybind/mgr/orchestrator/_interface.py index a7c60b1d2045e..e62db08298bbf 100644 --- a/src/pybind/mgr/orchestrator/_interface.py +++ b/src/pybind/mgr/orchestrator/_interface.py @@ -576,34 +576,22 @@ class Orchestrator(object): """Zap/Erase a device (DESTROYS DATA)""" raise NotImplementedError() - def add_mon(self, spec: ServiceSpec) -> OrchResult[List[str]]: - """Create mon daemon(s)""" + def add_daemon(self, spec: ServiceSpec) -> OrchResult[List[str]]: + """Create daemons daemon(s) for unmanaged services""" raise NotImplementedError() def apply_mon(self, spec: ServiceSpec) -> OrchResult[str]: """Update mon cluster""" raise NotImplementedError() - def add_mgr(self, spec: ServiceSpec) -> OrchResult[List[str]]: - """Create mgr daemon(s)""" - raise NotImplementedError() - def apply_mgr(self, spec: ServiceSpec) -> OrchResult[str]: """Update mgr cluster""" raise NotImplementedError() - def add_mds(self, spec: ServiceSpec) -> OrchResult[List[str]]: - """Create MDS daemon(s)""" - raise NotImplementedError() - def apply_mds(self, spec: ServiceSpec) -> OrchResult[str]: """Update MDS cluster""" raise NotImplementedError() - def add_rgw(self, spec: RGWSpec) -> OrchResult[List[str]]: - """Create RGW daemon(s)""" - raise NotImplementedError() - def apply_rgw(self, spec: RGWSpec) -> OrchResult[str]: """Update RGW cluster""" raise NotImplementedError() @@ -612,74 +600,38 @@ class Orchestrator(object): """Update ha-rgw daemons""" raise NotImplementedError() - def add_rbd_mirror(self, spec: ServiceSpec) -> OrchResult[List[str]]: - """Create rbd-mirror daemon(s)""" - raise NotImplementedError() - def apply_rbd_mirror(self, spec: ServiceSpec) -> OrchResult[str]: """Update rbd-mirror cluster""" raise NotImplementedError() - def add_nfs(self, spec: NFSServiceSpec) -> OrchResult[List[str]]: - """Create NFS daemon(s)""" - raise NotImplementedError() - def apply_nfs(self, spec: NFSServiceSpec) -> OrchResult[str]: """Update NFS cluster""" raise NotImplementedError() - def add_iscsi(self, spec: IscsiServiceSpec) -> OrchResult[List[str]]: - """Create iscsi daemon(s)""" - raise NotImplementedError() - def apply_iscsi(self, spec: IscsiServiceSpec) -> OrchResult[str]: """Update iscsi cluster""" raise NotImplementedError() - def add_prometheus(self, spec: ServiceSpec) -> OrchResult[List[str]]: - """Create new prometheus daemon""" - raise NotImplementedError() - def apply_prometheus(self, spec: ServiceSpec) -> OrchResult[str]: """Update prometheus cluster""" raise NotImplementedError() - def add_node_exporter(self, spec: ServiceSpec) -> OrchResult[List[str]]: - """Create a new Node-Exporter service""" - raise NotImplementedError() - def apply_node_exporter(self, spec: ServiceSpec) -> OrchResult[str]: """Update existing a Node-Exporter daemon(s)""" raise NotImplementedError() - def add_crash(self, spec: ServiceSpec) -> OrchResult[List[str]]: - """Create a new crash service""" - raise NotImplementedError() - def apply_crash(self, spec: ServiceSpec) -> OrchResult[str]: """Update existing a crash daemon(s)""" raise NotImplementedError() - def add_grafana(self, spec: ServiceSpec) -> OrchResult[List[str]]: - """Create a new grafana service""" - raise NotImplementedError() - def apply_grafana(self, spec: ServiceSpec) -> OrchResult[str]: """Update existing a grafana service""" raise NotImplementedError() - def add_alertmanager(self, spec: ServiceSpec) -> OrchResult[List[str]]: - """Create a new AlertManager service""" - raise NotImplementedError() - def apply_alertmanager(self, spec: ServiceSpec) -> OrchResult[str]: """Update an existing AlertManager daemon(s)""" raise NotImplementedError() - def add_cephadm_exporter(self, spec: ServiceSpec) -> OrchResult[List[str]]: - """Create a new cephadm exporter daemon""" - raise NotImplementedError() - def apply_cephadm_exporter(self, spec: ServiceSpec) -> OrchResult[str]: """Update an existing cephadm exporter daemon""" raise NotImplementedError() diff --git a/src/pybind/mgr/orchestrator/module.py b/src/pybind/mgr/orchestrator/module.py index 9980c87cd4fe6..ae1c4c4e3635a 100644 --- a/src/pybind/mgr/orchestrator/module.py +++ b/src/pybind/mgr/orchestrator/module.py @@ -853,38 +853,7 @@ Usage: return self._daemon_add_misc(spec) def _daemon_add_misc(self, spec: ServiceSpec) -> HandleCommandResult: - daemon_type = ServiceType(spec.service_type) - - if daemon_type == ServiceType.mon: - completion = self.add_mon(spec) - elif daemon_type == ServiceType.mgr: - completion = self.add_mgr(spec) - elif daemon_type == ServiceType.rbd_mirror: - completion = self.add_rbd_mirror(spec) - elif daemon_type == ServiceType.crash: - completion = self.add_crash(spec) - elif daemon_type == ServiceType.alertmanager: - completion = self.add_alertmanager(spec) - elif daemon_type == ServiceType.grafana: - completion = self.add_grafana(spec) - elif daemon_type == ServiceType.node_exporter: - completion = self.add_node_exporter(spec) - elif daemon_type == ServiceType.prometheus: - completion = self.add_prometheus(spec) - elif daemon_type == ServiceType.mds: - completion = self.add_mds(spec) - elif daemon_type == ServiceType.rgw: - completion = self.add_rgw(cast(RGWSpec, spec)) - elif daemon_type == ServiceType.nfs: - completion = self.add_nfs(cast(NFSServiceSpec, spec)) - elif daemon_type == ServiceType.iscsi: - completion = self.add_iscsi(cast(IscsiServiceSpec, spec)) - elif daemon_type == ServiceType.cephadm_exporter: - completion = self.add_cephadm_exporter(spec) - else: - tp = type(daemon_type) - raise OrchestratorValidationError(f'unknown daemon type `{tp}`') - + completion = self.add_daemon(spec) raise_if_exception(completion) return HandleCommandResult(stdout=completion.result_str()) diff --git a/src/pybind/mgr/test_orchestrator/module.py b/src/pybind/mgr/test_orchestrator/module.py index 75fec48d37ef3..f5729e12242f6 100644 --- a/src/pybind/mgr/test_orchestrator/module.py +++ b/src/pybind/mgr/test_orchestrator/module.py @@ -246,33 +246,18 @@ class TestOrchestrator(MgrModule, orchestrator.Orchestrator): return 'done' @handle_orch_error - def add_nfs(self, spec): - # type: (NFSServiceSpec) -> List[str] - assert isinstance(spec.pool, str) + def add_daemon(self, spec: ServiceSpec): return [spec.one_line_str()] @handle_orch_error def apply_nfs(self, spec): return spec.one_line_str() - @handle_orch_error - def add_iscsi(self, spec): - # type: (IscsiServiceSpec) -> List[str] - return [spec.one_line_str()] - @handle_orch_error def apply_iscsi(self, spec): # type: (IscsiServiceSpec) -> str return spec.one_line_str() - @handle_orch_error - def add_mds(self, spec): - return 'done' - - @handle_orch_error - def add_rgw(self, spec): - return 'done' - @handle_orch_error def get_hosts(self): if self._inventory: -- 2.39.5