Blame SOURCES/bz1977588-3-fence_kubevirt-fix-status.patch

021bc5
From c23bfc3b9fc6e1dc7b1350e340171f3827fb6fb7 Mon Sep 17 00:00:00 2001
021bc5
From: Nir Argaman <nargaman@redhat.com>
021bc5
Date: Mon, 1 Nov 2021 08:34:00 +0200
021bc5
Subject: [PATCH] fence_kubevirt: Fix kubevirt VM status
021bc5
021bc5
---
021bc5
 agents/kubevirt/fence_kubevirt.py | 19 +++++++++++++------
021bc5
 1 file changed, 13 insertions(+), 6 deletions(-)
021bc5
021bc5
diff --git a/agents/kubevirt/fence_kubevirt.py b/agents/kubevirt/fence_kubevirt.py
021bc5
index 70c14d89a..61ffcaa09 100755
021bc5
--- a/agents/kubevirt/fence_kubevirt.py
021bc5
+++ b/agents/kubevirt/fence_kubevirt.py
021bc5
@@ -4,7 +4,7 @@
021bc5
 import logging
021bc5
 sys.path.append("@FENCEAGENTSLIBDIR@")
021bc5
 from fencing import *
021bc5
-from fencing import fail, fail_usage, run_delay, EC_STATUS
021bc5
+from fencing import fail, fail_usage, run_delay, EC_STATUS, EC_FETCH_VM_UUID
021bc5
 
021bc5
 try:
021bc5
     from kubernetes.client.exceptions import ApiException
021bc5
@@ -35,13 +35,15 @@ def get_power_status(conn, options):
021bc5
         vmi_api = conn.resources.get(api_version=apiversion,
021bc5
                                               kind='VirtualMachineInstance')
021bc5
         vmi = vmi_api.get(name=name, namespace=namespace)
021bc5
-        if vmi is not None:
021bc5
-            phase = vmi.status.phase
021bc5
-            if phase == "Running":
021bc5
-                return "on"
021bc5
-        return "off"
021bc5
+        return translate_status(vmi.status.phase)
021bc5
     except ApiException as e:
021bc5
         if e.status == 404:
021bc5
+            try:
021bc5
+                vm_api = conn.resources.get(api_version=apiversion, kind='VirtualMachine')
021bc5
+                vm = vm_api.get(name=name, namespace=namespace)
021bc5
+            except ApiException as e:
021bc5
+                logging.error("VM %s doesn't exist", name)
021bc5
+                fail(EC_FETCH_VM_UUID)
021bc5
             return "off"
021bc5
         logging.error("Failed to get power status, with API Exception: %s", e)
021bc5
         fail(EC_STATUS)
021bc5
@@ -49,6 +51,11 @@ def get_power_status(conn, options):
021bc5
         logging.error("Failed to get power status, with Exception: %s", e)
021bc5
         fail(EC_STATUS)
021bc5
 
021bc5
+def translate_status(instance_status):
021bc5
+    if instance_status == "Running":
021bc5
+        return "on"
021bc5
+    return "unknown"
021bc5
+
021bc5
 def set_power_status(conn, options):
021bc5
     logging.debug("Starting set status operation")
021bc5
     try: