]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
cephadm: add parsed_container_mem_usage to container_engines
authorJohn Mulligan <jmulligan@redhat.com>
Thu, 30 Jan 2025 00:12:41 +0000 (19:12 -0500)
committerJohn Mulligan <jmulligan@redhat.com>
Thu, 30 Jan 2025 00:43:11 +0000 (19:43 -0500)
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 <jmulligan@redhat.com>
src/cephadm/cephadmlib/container_engines.py

index 64ce7ae821abf30504ec95863e3dec7cc6cdf541..77e3714c18acc7ff52fe52f8b6cb141c19e1a271 100644 (file)
@@ -1,10 +1,11 @@
 # container_engines.py - container engine types and selection funcs
 
 import os
+import logging
 
 from typing import Tuple, List, Optional, Dict
 
-from .call_wrappers import call_throws, CallVerbosity
+from .call_wrappers import call_throws, call, CallVerbosity
 from .context import CephadmContext
 from .container_engine_base import ContainerEngine
 from .constants import (
@@ -13,9 +14,13 @@ from .constants import (
     MIN_PODMAN_VERSION,
     PIDS_LIMIT_UNLIMITED_PODMAN_VERSION,
 )
+from .data_utils import with_units_to_int
 from .exceptions import Error
 
 
+logger = logging.getLogger()
+
+
 class Podman(ContainerEngine):
     EXE = 'podman'
 
@@ -189,3 +194,57 @@ def pull_command(
         if os.path.exists('/etc/ceph/podman-auth.json'):
             cmd.append('--authfile=/etc/ceph/podman-auth.json')
     return cmd
+
+
+def _container_mem_usage(
+    ctx: CephadmContext,
+    *,
+    container_path: str = '',
+    verbosity: CallVerbosity = CallVerbosity.QUIET,
+) -> Tuple[str, str, int]:
+    container_path = container_path or ctx.container_engine.path
+    out, err, code = call(
+        ctx,
+        [
+            container_path,
+            'stats',
+            '--format',
+            '{{.ID}},{{.MemUsage}}',
+            '--no-stream',
+        ],
+        verbosity=verbosity,
+    )
+    return out, err, code
+
+
+def _parse_mem_usage(code: int, out: str) -> Tuple[int, Dict[str, int]]:
+    # keep track of memory usage we've seen
+    seen_memusage = {}  # type: Dict[str, int]
+    seen_memusage_cid_len = 0
+    if not code:
+        for line in out.splitlines():
+            (cid, usage) = line.split(',')
+            (used, limit) = usage.split(' / ')
+            try:
+                seen_memusage[cid] = with_units_to_int(used)
+                if not seen_memusage_cid_len:
+                    seen_memusage_cid_len = len(cid)
+            except ValueError:
+                logger.info(
+                    'unable to parse memory usage line\n>{}'.format(line)
+                )
+                pass
+    return seen_memusage_cid_len, seen_memusage
+
+
+def parsed_container_mem_usage(
+    ctx: CephadmContext,
+    *,
+    container_path: str = '',
+    verbosity: CallVerbosity = CallVerbosity.QUIET,
+) -> Tuple[int, Dict[str, int]]:
+    """Return memory useage values parsed from the container engine's container status."""
+    out, _, code = _container_mem_usage(
+        ctx, container_path=container_path, verbosity=verbosity
+    )
+    return _parse_mem_usage(code, out)