From: John Mulligan Date: Thu, 30 Jan 2025 23:49:01 +0000 (-0500) Subject: cephadm: replace get_container_stats in cephadm.py X-Git-Tag: v20.0.0~205^2~2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=32fe8aacc983cfcf36f0e9088b145437b09be9bc;p=ceph.git cephadm: replace get_container_stats in cephadm.py Replace the existing get_container_stats with a version from container_types.py that returns the parsed results of the container status command (as a ContainerInfo, None on error). Fix up a bunch of tests. Signed-off-by: John Mulligan --- diff --git a/src/cephadm/cephadm.py b/src/cephadm/cephadm.py index cf7531f76389..2a8f5e042bdc 100755 --- a/src/cephadm/cephadm.py +++ b/src/cephadm/cephadm.py @@ -147,8 +147,9 @@ from cephadmlib.container_types import ( InitContainer, SidecarContainer, extract_uid_gid, - is_container_running, + get_container_stats, get_mgr_images, + is_container_running, ) from cephadmlib.decorators import ( deprecated_command, @@ -511,10 +512,11 @@ def get_container_info(ctx: CephadmContext, daemon_filter: str, by_name: bool) - version=version) else: d_type, d_id = matching_daemons[0]['name'].split('.', 1) - out, _, code = get_container_stats(ctx, ctx.container_engine.path, ctx.fsid, d_type, d_id) - if not code: - (container_id, image_name, image_id, start, version) = out.strip().split(',') - return ContainerInfo(container_id, image_name, image_id, start, version) + cinfo = get_container_stats( + ctx, DaemonIdentity(ctx.fsid, d_type, d_id) + ) + if cinfo: + return cinfo return None @@ -3490,10 +3492,17 @@ def list_daemons( version = None start_stamp = None - out, err, code = get_container_stats(ctx, container_path, fsid, daemon_type, daemon_id) - if not code: - (container_id, image_name, image_id, start, - version) = out.strip().split(',') + cinfo = get_container_stats( + ctx, + DaemonIdentity(fsid, daemon_type, daemon_id), + container_path=container_path + ) + if cinfo: + container_id = cinfo.container_id + image_name = cinfo.image_name + image_id = cinfo.image_id + start = cinfo.start + version = cinfo.version image_id = normalize_container_id(image_id) daemon_type = name.split('.', 1)[0] start_stamp = try_convert_datetime(start) @@ -3638,24 +3647,6 @@ def get_daemon_description(ctx, fsid, name, detail=False, legacy_dir=None): raise Error('Daemon not found: {}. See `cephadm ls`'.format(name)) -def get_container_stats(ctx: CephadmContext, container_path: str, fsid: str, daemon_type: str, daemon_id: str) -> Tuple[str, str, int]: - """returns container id, image name, image id, created time, and ceph version if available""" - c = CephContainer.for_daemon( - ctx, DaemonIdentity(fsid, daemon_type, daemon_id), 'bash' - ) - out, err, code = '', '', -1 - for name in (c.cname, c.old_cname): - cmd = [ - container_path, 'inspect', - '--format', '{{.Id}},{{.Config.Image}},{{.Image}},{{.Created}},{{index .Config.Labels "io.ceph.version"}}', - name - ] - out, err, code = call(ctx, cmd, verbosity=CallVerbosity.QUIET) - if not code: - break - return out, err, code - - 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 diff --git a/src/cephadm/tests/test_cephadm.py b/src/cephadm/tests/test_cephadm.py index 003a1f59b1e2..de4eddc2f726 100644 --- a/src/cephadm/tests/test_cephadm.py +++ b/src/cephadm/tests/test_cephadm.py @@ -650,9 +650,8 @@ class TestCephAdm(object): {'name': 'mon.ceph-node-0', 'fsid': '00000000-0000-0000-0000-0000deadbeef'}, {'name': 'mgr.ceph-node-0', 'fsid': '00000000-0000-0000-0000-0000deadbeef'}, ], - ("935b549714b8f007c6a4e29c758689cf9e8e69f2e0f51180506492974b90a972,registry.hub.docker.com/rkachach/ceph:custom-v0.5,666bbfa87e8df05702d6172cae11dd7bc48efb1d94f1b9e492952f19647199a4,2022-04-19 13:45:20.97146228 +0000 UTC,", - "", - 0), + ("935b549714b8f007c6a4e29c758689cf9e8e69f2e0f51180506492974b90a972", "registry.hub.docker.com/rkachach/ceph:custom-v0.5", "666bbfa87e8df05702d6172cae11dd7bc48efb1d94f1b9e492952f19647199a4", "2022-04-19 13:45:20.97146228 +0000 UTC", "" + ), _cephadm.ContainerInfo('935b549714b8f007c6a4e29c758689cf9e8e69f2e0f51180506492974b90a972', 'registry.hub.docker.com/rkachach/ceph:custom-v0.5', '666bbfa87e8df05702d6172cae11dd7bc48efb1d94f1b9e492952f19647199a4', @@ -667,9 +666,8 @@ class TestCephAdm(object): {'name': 'mgr.ceph-node-0', 'fsid': '00000000-0000-0000-0000-0000deadbeef'}, {'name': 'mon.ceph-node-0', 'fsid': '00000000-0000-0000-0000-0000deadbeef'}, ], - ("935b549714b8f007c6a4e29c758689cf9e8e69f2e0f51180506492974b90a972,registry.hub.docker.com/rkachach/ceph:custom-v0.5,666bbfa87e8df05702d6172cae11dd7bc48efb1d94f1b9e492952f19647199a4,2022-04-19 13:45:20.97146228 +0000 UTC,", - "", - 0), + ("935b549714b8f007c6a4e29c758689cf9e8e69f2e0f51180506492974b90a972", "registry.hub.docker.com/rkachach/ceph:custom-v0.5", "666bbfa87e8df05702d6172cae11dd7bc48efb1d94f1b9e492952f19647199a4", "2022-04-19 13:45:20.97146228 +0000 UTC", "" + ), _cephadm.ContainerInfo('935b549714b8f007c6a4e29c758689cf9e8e69f2e0f51180506492974b90a972', 'registry.hub.docker.com/rkachach/ceph:custom-v0.5', '666bbfa87e8df05702d6172cae11dd7bc48efb1d94f1b9e492952f19647199a4', @@ -684,9 +682,8 @@ class TestCephAdm(object): {'name': 'mon.ceph-node-0', 'fsid': '10000000-0000-0000-0000-0000deadbeef'}, {'name': 'mon.ceph-node-0', 'fsid': '00000000-0000-0000-0000-0000deadbeef'}, ], - ("935b549714b8f007c6a4e29c758689cf9e8e69f2e0f51180506492974b90a972,registry.hub.docker.com/rkachach/ceph:custom-v0.5,666bbfa87e8df05702d6172cae11dd7bc48efb1d94f1b9e492952f19647199a4,2022-04-19 13:45:20.97146228 +0000 UTC,", - "", - 0), + ("935b549714b8f007c6a4e29c758689cf9e8e69f2e0f51180506492974b90a972", "registry.hub.docker.com/rkachach/ceph:custom-v0.5", "666bbfa87e8df05702d6172cae11dd7bc48efb1d94f1b9e492952f19647199a4", "2022-04-19 13:45:20.97146228 +0000 UTC", "" + ), _cephadm.ContainerInfo('935b549714b8f007c6a4e29c758689cf9e8e69f2e0f51180506492974b90a972', 'registry.hub.docker.com/rkachach/ceph:custom-v0.5', '666bbfa87e8df05702d6172cae11dd7bc48efb1d94f1b9e492952f19647199a4', @@ -701,9 +698,7 @@ class TestCephAdm(object): {'name': 'mon.ceph-node-0', 'fsid': '00000000-FFFF-0000-0000-0000deadbeef'}, {'name': 'mon.ceph-node-0', 'fsid': '00000000-0000-0000-0000-0000deadbeef'}, ], - ("", - "", - 127), + None, None ), # get container info by name (bad container stats: 127 code) @@ -714,9 +709,7 @@ class TestCephAdm(object): {'name': 'mgr.ceph-node-0', 'fsid': '00000000-0000-0000-0000-0000deadbeef'}, {'name': 'mon.ceph-node-0', 'fsid': '00000000-0000-0000-0000-0000deadbeef'}, ], - ("", - "", - 127), + None, None ), # get container info by invalid name (doens't contain '.') @@ -727,9 +720,8 @@ class TestCephAdm(object): {'name': 'mon.ceph-node-0', 'fsid': '00000000-0000-0000-0000-0000deadbeef'}, {'name': 'mon.ceph-node-0', 'fsid': '00000000-0000-0000-0000-0000deadbeef'}, ], - ("935b549714b8f007c6a4e29c758689cf9e8e69f2e0f51180506492974b90a972,registry.hub.docker.com/rkachach/ceph:custom-v0.5,666bbfa87e8df05702d6172cae11dd7bc48efb1d94f1b9e492952f19647199a4,2022-04-19 13:45:20.97146228 +0000 UTC,", - "", - 0), + ("935b549714b8f007c6a4e29c758689cf9e8e69f2e0f51180506492974b90a972", "registry.hub.docker.com/rkachach/ceph:custom-v0.5", "666bbfa87e8df05702d6172cae11dd7bc48efb1d94f1b9e492952f19647199a4", "2022-04-19 13:45:20.97146228 +0000 UTC", "" + ), None ), # get container info by invalid name (empty) @@ -740,9 +732,8 @@ class TestCephAdm(object): {'name': 'mon.ceph-node-0', 'fsid': '00000000-0000-0000-0000-0000deadbeef'}, {'name': 'mon.ceph-node-0', 'fsid': '00000000-0000-0000-0000-0000deadbeef'}, ], - ("935b549714b8f007c6a4e29c758689cf9e8e69f2e0f51180506492974b90a972,registry.hub.docker.com/rkachach/ceph:custom-v0.5,666bbfa87e8df05702d6172cae11dd7bc48efb1d94f1b9e492952f19647199a4,2022-04-19 13:45:20.97146228 +0000 UTC,", - "", - 0), + ("935b549714b8f007c6a4e29c758689cf9e8e69f2e0f51180506492974b90a972", "registry.hub.docker.com/rkachach/ceph:custom-v0.5", "666bbfa87e8df05702d6172cae11dd7bc48efb1d94f1b9e492952f19647199a4", "2022-04-19 13:45:20.97146228 +0000 UTC", "" + ), None ), # get container info by invalid type (empty) @@ -753,9 +744,8 @@ class TestCephAdm(object): {'name': 'mon.ceph-node-0', 'fsid': '00000000-0000-0000-0000-0000deadbeef'}, {'name': 'mon.ceph-node-0', 'fsid': '00000000-0000-0000-0000-0000deadbeef'}, ], - ("935b549714b8f007c6a4e29c758689cf9e8e69f2e0f51180506492974b90a972,registry.hub.docker.com/rkachach/ceph:custom-v0.5,666bbfa87e8df05702d6172cae11dd7bc48efb1d94f1b9e492952f19647199a4,2022-04-19 13:45:20.97146228 +0000 UTC,", - "", - 0), + ("935b549714b8f007c6a4e29c758689cf9e8e69f2e0f51180506492974b90a972", "registry.hub.docker.com/rkachach/ceph:custom-v0.5", "666bbfa87e8df05702d6172cae11dd7bc48efb1d94f1b9e492952f19647199a4", "2022-04-19 13:45:20.97146228 +0000 UTC", "" + ), None ), # get container info by name: no match (invalid fsid) @@ -766,9 +756,8 @@ class TestCephAdm(object): {'name': 'mon.ceph-node-0', 'fsid': '00000000-1111-0000-0000-0000deadbeef'}, {'name': 'mon.ceph-node-0', 'fsid': '00000000-2222-0000-0000-0000deadbeef'}, ], - ("935b549714b8f007c6a4e29c758689cf9e8e69f2e0f51180506492974b90a972,registry.hub.docker.com/rkachach/ceph:custom-v0.5,666bbfa87e8df05702d6172cae11dd7bc48efb1d94f1b9e492952f19647199a4,2022-04-19 13:45:20.97146228 +0000 UTC,", - "", - 0), + ("935b549714b8f007c6a4e29c758689cf9e8e69f2e0f51180506492974b90a972", "registry.hub.docker.com/rkachach/ceph:custom-v0.5", "666bbfa87e8df05702d6172cae11dd7bc48efb1d94f1b9e492952f19647199a4", "2022-04-19 13:45:20.97146228 +0000 UTC", "" + ), None ), # get container info by name: no match @@ -788,19 +777,36 @@ class TestCephAdm(object): None ), ]) - @mock.patch('cephadm.logger') - def test_get_container_info(self, _logger, daemon_filter, by_name, daemon_list, container_stats, output): + def test_get_container_info( + self, + daemon_filter, + by_name, + daemon_list, + container_stats, + output, + funkypatch, + ): ctx = _cephadm.CephadmContext() ctx.fsid = '00000000-0000-0000-0000-0000deadbeef' ctx.container_engine = mock_podman() - with mock.patch('cephadm.list_daemons', return_value=daemon_list): - with mock.patch('cephadm.get_container_stats', return_value=container_stats): - assert _cephadm.get_container_info(ctx, daemon_filter, by_name) == output - - @mock.patch('cephadm.list_daemons') - @mock.patch('cephadm.get_container_stats') - @mock.patch('cephadm.get_container_stats_by_image_name') - def test_get_container_info_daemon_down(self, _get_stats_by_name, _get_stats, _list_daemons): + funkypatch.patch('cephadm.list_daemons').return_value = daemon_list + cinfo = ( + _cephadm.ContainerInfo(*container_stats) + if container_stats + else None + ) + funkypatch.patch( + 'cephadmlib.container_types.get_container_stats' + ).return_value = cinfo + assert ( + _cephadm.get_container_info(ctx, daemon_filter, by_name) == output + ) + + def test_get_container_info_daemon_down(self, funkypatch): + _get_stats_by_name = funkypatch.patch('cephadm.get_container_stats_by_image_name') + _get_stats = funkypatch.patch('cephadmlib.container_types.get_container_stats') + _list_daemons = funkypatch.patch('cephadm.list_daemons') + ctx = _cephadm.CephadmContext() ctx.fsid = '5e39c134-dfc5-11ee-a344-5254000ee071' ctx.container_engine = mock_podman() @@ -859,7 +865,7 @@ class TestCephAdm(object): # than it partially being taken from the list_daemons output up_osd_json = copy.deepcopy(down_osd_json) up_osd_json['state'] = 'running' - _get_stats.return_value = (('container_id,image_name,image_id,the_past,'), '', 0) + _get_stats.return_value = _cephadm.ContainerInfo('container_id', 'image_name','image_id','the_past','') _list_daemons.return_value = [down_osd_json, up_osd_json] expected_container_info = _cephadm.ContainerInfo(