Skip to content

Commit 395b8d3

Browse files
cephadm: add parsed_container_mem_usage to container_engines
Add a new function that combines the call and parse operations that exist in cephadm.py (currently _parse_mem_usage and related command calls). This will be used in a future commit to replace that code and reduce the size of cephadm.py. Signed-off-by: John Mulligan <[email protected]>
1 parent c20073f commit 395b8d3

File tree

1 file changed

+60
-1
lines changed

1 file changed

+60
-1
lines changed

src/cephadm/cephadmlib/container_engines.py

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# container_engines.py - container engine types and selection funcs
22

33
import os
4+
import logging
45

56
from typing import Tuple, List, Optional, Dict
67

7-
from .call_wrappers import call_throws, CallVerbosity
8+
from .call_wrappers import call_throws, call, CallVerbosity
89
from .context import CephadmContext
910
from .container_engine_base import ContainerEngine
1011
from .constants import (
@@ -13,9 +14,13 @@
1314
MIN_PODMAN_VERSION,
1415
PIDS_LIMIT_UNLIMITED_PODMAN_VERSION,
1516
)
17+
from .data_utils import with_units_to_int
1618
from .exceptions import Error
1719

1820

21+
logger = logging.getLogger()
22+
23+
1924
class Podman(ContainerEngine):
2025
EXE = 'podman'
2126

@@ -189,3 +194,57 @@ def pull_command(
189194
if os.path.exists('/etc/ceph/podman-auth.json'):
190195
cmd.append('--authfile=/etc/ceph/podman-auth.json')
191196
return cmd
197+
198+
199+
def _container_mem_usage(
200+
ctx: CephadmContext,
201+
*,
202+
container_path: str = '',
203+
verbosity: CallVerbosity = CallVerbosity.QUIET,
204+
) -> Tuple[str, str, int]:
205+
container_path = container_path or ctx.container_engine.path
206+
out, err, code = call(
207+
ctx,
208+
[
209+
container_path,
210+
'stats',
211+
'--format',
212+
'{{.ID}},{{.MemUsage}}',
213+
'--no-stream',
214+
],
215+
verbosity=verbosity,
216+
)
217+
return out, err, code
218+
219+
220+
def _parse_mem_usage(code: int, out: str) -> Tuple[int, Dict[str, int]]:
221+
# keep track of memory usage we've seen
222+
seen_memusage = {} # type: Dict[str, int]
223+
seen_memusage_cid_len = 0
224+
if not code:
225+
for line in out.splitlines():
226+
(cid, usage) = line.split(',')
227+
(used, limit) = usage.split(' / ')
228+
try:
229+
seen_memusage[cid] = with_units_to_int(used)
230+
if not seen_memusage_cid_len:
231+
seen_memusage_cid_len = len(cid)
232+
except ValueError:
233+
logger.info(
234+
'unable to parse memory usage line\n>{}'.format(line)
235+
)
236+
pass
237+
return seen_memusage_cid_len, seen_memusage
238+
239+
240+
def parsed_container_mem_usage(
241+
ctx: CephadmContext,
242+
*,
243+
container_path: str = '',
244+
verbosity: CallVerbosity = CallVerbosity.QUIET,
245+
) -> Tuple[int, Dict[str, int]]:
246+
"""Return memory useage values parsed from the container engine's container status."""
247+
out, _, code = _container_mem_usage(
248+
ctx, container_path=container_path, verbosity=verbosity
249+
)
250+
return _parse_mem_usage(code, out)

0 commit comments

Comments
 (0)