Blame SOURCES/sos-bz2098643-crio-output-to-json.patch

092273
From c2e66fa4dae51f03c7310ba5278897ddecac1aad Mon Sep 17 00:00:00 2001
092273
From: Nadia Pinaeva <npinaeva@redhat.com>
092273
Date: Thu, 2 Jun 2022 15:43:09 +0200
092273
Subject: [PATCH] crio: switch from parsing output in table format to json
092273
092273
Signed-off-by: Nadia Pinaeva <npinaeva@redhat.com>
092273
---
092273
 sos/policies/runtimes/crio.py | 30 ++++++++++++++++++++----------
092273
 1 file changed, 20 insertions(+), 10 deletions(-)
092273
092273
diff --git a/sos/policies/runtimes/crio.py b/sos/policies/runtimes/crio.py
092273
index 55082d07..4cae1ecc 100644
092273
--- a/sos/policies/runtimes/crio.py
092273
+++ b/sos/policies/runtimes/crio.py
092273
@@ -7,6 +7,7 @@
092273
 # version 2 of the GNU General Public License.
092273
 #
092273
 # See the LICENSE file in the source distribution for further information.
092273
+import json
092273
 
092273
 from sos.policies.runtimes import ContainerRuntime
092273
 from sos.utilities import sos_get_command_output
092273
@@ -29,14 +30,15 @@ class CrioContainerRuntime(ContainerRuntime):
092273
         :type get_all: ``bool``
092273
         """
092273
         containers = []
092273
-        _cmd = "%s ps %s" % (self.binary, '-a' if get_all else '')
092273
+        _cmd = "%s ps %s -o json" % (self.binary, '-a' if get_all else '')
092273
         if self.active:
092273
             out = sos_get_command_output(_cmd, chroot=self.policy.sysroot)
092273
-            if out['status'] == 0:
092273
-                for ent in out['output'].splitlines()[1:]:
092273
-                    ent = ent.split()
092273
+            if out["status"] == 0:
092273
+                out_json = json.loads(out["output"])
092273
+                for container in out_json["containers"]:
092273
                     # takes the form (container_id, container_name)
092273
-                    containers.append((ent[0], ent[-3]))
092273
+                    containers.append(
092273
+                        (container["id"], container["metadata"]["name"]))
092273
         return containers
092273
 
092273
     def get_images(self):
092273
@@ -47,13 +49,21 @@ class CrioContainerRuntime(ContainerRuntime):
092273
         """
092273
         images = []
092273
         if self.active:
092273
-            out = sos_get_command_output("%s images" % self.binary,
092273
+            out = sos_get_command_output("%s images -o json" % self.binary,
092273
                                          chroot=self.policy.sysroot)
092273
             if out['status'] == 0:
092273
-                for ent in out['output'].splitlines():
092273
-                    ent = ent.split()
092273
-                    # takes the form (image_name, image_id)
092273
-                    images.append((ent[0] + ':' + ent[1], ent[2]))
092273
+                out_json = json.loads(out["output"])
092273
+                for image in out_json["images"]:
092273
+                    # takes the form (repository:tag, image_id)
092273
+                    if len(image["repoTags"]) > 0:
092273
+                        for repo_tag in image["repoTags"]:
092273
+                            images.append((repo_tag, image["id"]))
092273
+                    else:
092273
+                        if len(image["repoDigests"]) == 0:
092273
+                            image_name = "<none>"
092273
+                        else:
092273
+                            image_name = image["repoDigests"][0].split("@")[0]
092273
+                        images.append((image_name + ":<none>", image["id"]))
092273
         return images
092273
 
092273
     def fmt_container_cmd(self, container, cmd, quotecmd):
092273
-- 
092273
2.27.0
092273