From: John Mulligan Date: Thu, 15 Jun 2023 19:34:15 +0000 (-0400) Subject: cephadm: update code to use fetch_custom_config_files X-Git-Tag: v19.0.0~982^2~16 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=0ac4fe0624d1b7a03f0c06ad2af2999ebadfc8fb;p=ceph.git cephadm: update code to use fetch_custom_config_files Update the code to use fetch_custom_config_files rather than get_custom_config_files. Like other fetch_ functions recently added to cephadm we don't LBYL for checking values on ctx, but rather call the fetch function on ctx and then check if the result contains actionable data. get_custom_config_files gets removed as no remaining callers exist. Signed-off-by: John Mulligan --- diff --git a/src/cephadm/cephadm.py b/src/cephadm/cephadm.py index d188189cfd26..1dd12885c67a 100755 --- a/src/cephadm/cephadm.py +++ b/src/cephadm/cephadm.py @@ -2971,14 +2971,14 @@ def create_daemon_dirs(ctx, fsid, daemon_type, daemon_id, uid, gid, def _write_custom_conf_files(ctx: CephadmContext, daemon_type: str, daemon_id: str, fsid: str, uid: int, gid: int) -> None: # mostly making this its own function to make unit testing easier - if 'config_json' not in ctx or not ctx.config_json: + ccfiles = fetch_custom_config_files(ctx) + if not ccfiles: return - config_json = get_custom_config_files(ctx.config_json) custom_config_dir = os.path.join(ctx.data_dir, fsid, 'custom_config_files', f'{daemon_type}.{daemon_id}') if not os.path.exists(custom_config_dir): makedirs(custom_config_dir, uid, gid, 0o755) mandatory_keys = ['mount_path', 'content'] - for ccf in config_json['custom_config_files']: + for ccf in ccfiles: if all(k in ccf for k in mandatory_keys): file_path = os.path.join(custom_config_dir, os.path.basename(ccf['mount_path'])) with write_new(file_path, owner=(uid, gid), encoding='utf-8') as f: @@ -2989,20 +2989,12 @@ def get_parm(option: str) -> Dict[str, str]: js = _get_config_json(option) # custom_config_files is a special field that may be in the config # dict. It is used for mounting custom config files into daemon's containers - # and should be accessed through the "get_custom_config_files" function. + # and should be accessed through the "fetch_custom_config_files" function. # For get_parm we need to discard it. js.pop('custom_config_files', None) return js -def get_custom_config_files(option: str) -> Dict[str, List[Dict[str, str]]]: - js = _get_config_json(option) - res: Dict[str, List[Dict[str, str]]] = {'custom_config_files': []} - if 'custom_config_files' in js: - res['custom_config_files'] = js['custom_config_files'] - return res - - def _get_config_json(option: str) -> Dict[str, Any]: if not option: return dict() @@ -6198,10 +6190,10 @@ def get_deployment_container(ctx: CephadmContext, c.container_args.extend(ctx.extra_container_args) if 'extra_entrypoint_args' in ctx and ctx.extra_entrypoint_args: c.args.extend(ctx.extra_entrypoint_args) - if 'config_json' in ctx and ctx.config_json: - conf_files = get_custom_config_files(ctx.config_json) + ccfiles = fetch_custom_config_files(ctx) + if ccfiles: mandatory_keys = ['mount_path', 'content'] - for conf in conf_files['custom_config_files']: + for conf in ccfiles: if all(k in conf for k in mandatory_keys): mount_path = conf['mount_path'] file_path = os.path.join( diff --git a/src/cephadm/tests/test_cephadm.py b/src/cephadm/tests/test_cephadm.py index a3de66c982e3..d51210949b09 100644 --- a/src/cephadm/tests/test_cephadm.py +++ b/src/cephadm/tests/test_cephadm.py @@ -258,7 +258,7 @@ class TestCephAdm(object): _cephadm.prepare_dashboard(ctx, 0, 0, lambda _, extra_mounts=None, ___=None : '5', lambda : None) @mock.patch('cephadm.logger') - @mock.patch('cephadm.get_custom_config_files') + @mock.patch('cephadm.fetch_custom_config_files') @mock.patch('cephadm.get_container') def test_get_deployment_container(self, _get_container, _get_config, _logger): """ @@ -272,12 +272,12 @@ class TestCephAdm(object): '--something', ] ctx.data_dir = 'data' - _get_config.return_value = {'custom_config_files': [ + _get_config.return_value = [ { 'mount_path': '/etc/testing.str', 'content': 'this\nis\na\nstring', } - ]} + ] _get_container.return_value = _cephadm.CephContainer.for_daemon( ctx, fsid='9b9d7609-f4d5-4aba-94c8-effa764d96c9', @@ -357,7 +357,7 @@ class TestCephAdm(object): _cephadm.command_deploy(ctx) @mock.patch('cephadm.logger') - @mock.patch('cephadm.get_custom_config_files') + @mock.patch('cephadm.fetch_custom_config_files') def test_write_custom_conf_files(self, _get_config, _logger, cephadm_fs): """ test _write_custom_conf_files writes the conf files correctly @@ -366,7 +366,7 @@ class TestCephAdm(object): ctx = _cephadm.CephadmContext() ctx.config_json = '-' ctx.data_dir = _cephadm.DATA_DIR - _get_config.return_value = {'custom_config_files': [ + _get_config.return_value = [ { 'mount_path': '/etc/testing.str', 'content': 'this\nis\na\nstring', @@ -378,7 +378,7 @@ class TestCephAdm(object): { 'mount_path': '/etc/no-content.conf', }, - ]} + ] _cephadm._write_custom_conf_files(ctx, 'mon', 'host1', 'fsid', 0, 0) with open(os.path.join(_cephadm.DATA_DIR, 'fsid', 'custom_config_files', 'mon.host1', 'testing.str'), 'r') as f: assert 'this\nis\na\nstring' == f.read()