From edc278468c2eaa9921cee340de0f0592d38682d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20M=C3=A1gr?= Date: Tue, 29 Aug 2023 14:57:42 +0200 Subject: [PATCH] Support podman 4.0.0- This patch makes container healthcheck work also with versions of podman(-remote) lower than 4.0.0. For more infor please check [1]. [1] https://github.com/containers/podman/issues/11645 Resolves: rhbz#2223294 Change-Id: I4acdb425f19802424800876c11838364b623bc07 --- .../monitoring/collectd_check_health.py | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/container_config_scripts/monitoring/collectd_check_health.py b/container_config_scripts/monitoring/collectd_check_health.py index 15fedaa2f4..1dd5175c7d 100755 --- a/container_config_scripts/monitoring/collectd_check_health.py +++ b/container_config_scripts/monitoring/collectd_check_health.py @@ -20,10 +20,11 @@ import subprocess import sys +from pkg_resources import packaging + SOCKET = "unix:/run/podman/podman.sock" -FORMAT = ("{service: .Name, container: .Id, status: .State.Running, " - "healthy: .State.Health.Status}") +BASE_FORMAT = "{service: .Name, container: .Id, status: .State.Running, " SKIP_LIST = ['_bootstrap', 'container-puppet-', '_db_sync', '_ensure_', '_fix_', '_init_', '_map_', '_wait_', 'mysql_data_ownership', 'configure_cms_options'] @@ -55,6 +56,19 @@ def execute(cmd, workdir: str = None, def fetch_container_health(containers): + proc = execute([shutil.which('podman-remote'), + '--url', SOCKET, 'version', + '--format', r'{{.Server.Version}}']) + o, e = proc.communicate() + try: + if packaging.version.parse(o.decode().strip()) >= packaging.version.parse("4.0.0"): + fmt = BASE_FORMAT + "healthy: .State.Health.Status}" + else: + fmt = BASE_FORMAT + "healthy: .State.Healthcheck.Status}" + except Exception: + # keep podman-4.0.0+ format in case of version decoding error + fmt = BASE_FORMAT + "healthy: .State.Health.Status}" + out = [] for cont in set(containers.split('\n')) - set(SKIP_LIST): if not cont: @@ -62,7 +76,7 @@ def fetch_container_health(containers): proc = execute([ [shutil.which('podman-remote'), '--url', SOCKET, 'inspect', cont], - [shutil.which('jq'), '.[] | %s' % FORMAT] + [shutil.which('jq'), '.[] | %s' % fmt] ]) o, e = proc.communicate() if proc.returncode != 0: