]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
cephadm: update code to use fetch_custom_config_files
authorJohn Mulligan <jmulligan@redhat.com>
Thu, 15 Jun 2023 19:34:15 +0000 (15:34 -0400)
committerAdam King <adking@redhat.com>
Thu, 31 Aug 2023 17:35:14 +0000 (13:35 -0400)
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 <jmulligan@redhat.com>
src/cephadm/cephadm.py
src/cephadm/tests/test_cephadm.py

index 309108b8dff29bca294aabb16fc8c3b150f11157..e10621b16f41fea6add6bf0186fc0c23989f6542 100755 (executable)
@@ -2970,14 +2970,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:
@@ -2988,20 +2988,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()
@@ -6233,10 +6225,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(
index 4f174df51d48e0496a5ab0438091af7a8003541d..a66bac9630a9627a958b81dec779c86fcc5063a8 100644 (file)
@@ -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',
@@ -356,7 +356,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
@@ -365,7 +365,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',
@@ -377,7 +377,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()