From: John Mulligan Date: Fri, 31 Jan 2025 00:04:56 +0000 (-0500) Subject: cephadm: use get_container_image_stats in cephadm.py X-Git-Tag: v20.0.0~205^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=926011ad9d3b6091b83d26df62d2c53238e8135f;p=ceph.git cephadm: use get_container_image_stats in cephadm.py Replace the existing get_container_stats_by_image_name with a version from container_engines.py that returns the parsed results of the container status command (as a ContainerInfo, None on error). Fix up a few tests. Part of the test is somewhat pointless now as the input and the output are both the same ContainerInfo, but this was never a great test to test parsing anyway. Signed-off-by: John Mulligan --- diff --git a/src/cephadm/cephadm.py b/src/cephadm/cephadm.py index 2a8f5e042bdc..6dc1d5cdfa08 100755 --- a/src/cephadm/cephadm.py +++ b/src/cephadm/cephadm.py @@ -89,8 +89,9 @@ from cephadmlib.container_engines import ( Podman, check_container_engine, find_container_engine, - parsed_container_mem_usage, parsed_container_cpu_perc, + parsed_container_image_stats, + parsed_container_mem_usage, pull_command, registry_login, ) @@ -500,16 +501,9 @@ def get_container_info(ctx: CephadmContext, daemon_filter: str, by_name: bool) - # container will not help us. If we have the image name from the list_daemons output # we can try that. image_name = matching_daemons[0]['container_image_name'] - out, _, code = get_container_stats_by_image_name(ctx, ctx.container_engine.path, image_name) - if not code: - # keep in mind, the daemon container is not running, so no container id here - (image_id, start, version) = out.strip().split(',') - return ContainerInfo( - container_id='', - image_name=image_name, - image_id=image_id, - start=start, - version=version) + cinfo = parsed_container_image_stats(ctx, image_name) + if cinfo: + return cinfo else: d_type, d_id = matching_daemons[0]['name'].split('.', 1) cinfo = get_container_stats( @@ -3646,18 +3640,6 @@ def get_daemon_description(ctx, fsid, name, detail=False, legacy_dir=None): return d raise Error('Daemon not found: {}. See `cephadm ls`'.format(name)) - -def get_container_stats_by_image_name(ctx: CephadmContext, container_path: str, image_name: str) -> Tuple[str, str, int]: - """returns image id, created time, and ceph version if available""" - out, err, code = '', '', -1 - cmd = [ - container_path, 'image', 'inspect', - '--format', '{{.Id}},{{.Created}},{{index .Config.Labels "io.ceph.version"}}', - image_name - ] - out, err, code = call(ctx, cmd, verbosity=CallVerbosity.QUIET) - return out, err, code - ################################## diff --git a/src/cephadm/tests/test_cephadm.py b/src/cephadm/tests/test_cephadm.py index de4eddc2f726..f33f269c78e3 100644 --- a/src/cephadm/tests/test_cephadm.py +++ b/src/cephadm/tests/test_cephadm.py @@ -803,7 +803,7 @@ class TestCephAdm(object): ) def test_get_container_info_daemon_down(self, funkypatch): - _get_stats_by_name = funkypatch.patch('cephadm.get_container_stats_by_image_name') + _get_stats_by_name = funkypatch.patch('cephadmlib.container_engines.parsed_container_image_stats') _get_stats = funkypatch.patch('cephadmlib.container_types.get_container_stats') _list_daemons = funkypatch.patch('cephadm.list_daemons') @@ -845,9 +845,6 @@ class TestCephAdm(object): "configured": "2024-03-11T17:37:28.494075Z" } _list_daemons.return_value = [down_osd_json] - _get_stats_by_name.return_value = (('a03c201ff4080204949932f367545cd381c4acee0d48dbc15f2eac1e35f22318,' - '2023-11-28 21:34:38.045413692 +0000 UTC,'), - '', 0) expected_container_info = _cephadm.ContainerInfo( container_id='', @@ -855,6 +852,10 @@ class TestCephAdm(object): image_id='a03c201ff4080204949932f367545cd381c4acee0d48dbc15f2eac1e35f22318', start='2023-11-28 21:34:38.045413692 +0000 UTC', version='') + # refactoring get_container_stats_by_image_name into + # parsed_container_image_stats has made this part of the test somewhat + # redundant + _get_stats_by_name.return_value = expected_container_info assert _cephadm.get_container_info(ctx, 'osd.2', by_name=True) == expected_container_info assert not _get_stats.called, 'only get_container_stats_by_image_name should have been called'