Blame SOURCES/bz2000954-2-fence_kubevirt.patch

d4ebfc
--- fence-agents-4.10.0/agents/kubevirt/fence_kubevirt.py	2021-07-08 13:09:05.000000000 +0200
d4ebfc
+++ /home/oalbrigt/rhpkg/fence-agents-8.6/fence-agents-4.2.1/agents/kubevirt/fence_kubevirt.py	2021-11-02 15:35:46.217440426 +0100
d4ebfc
@@ -2,24 +2,25 @@
d4ebfc
 
d4ebfc
 import sys
d4ebfc
 import logging
d4ebfc
+import atexit
d4ebfc
 sys.path.append("@FENCEAGENTSLIBDIR@")
d4ebfc
 from fencing import *
d4ebfc
-from fencing import fail, fail_usage, run_delay, EC_STATUS
d4ebfc
+from fencing import fail, fail_usage, run_delay, EC_STATUS, EC_FETCH_VM_UUID
d4ebfc
 
d4ebfc
 try:
d4ebfc
+    sys.path.insert(0, '/usr/lib/fence-agents/bundled/kubevirt')
d4ebfc
     from kubernetes.client.exceptions import ApiException
d4ebfc
 except ImportError:
d4ebfc
     logging.error("Couldn\'t import kubernetes.client.exceptions.ApiException - not found or not accessible")
d4ebfc
 
d4ebfc
-API_VERSION='kubevirt.io/v1'
d4ebfc
-
d4ebfc
 def get_nodes_list(conn, options):
d4ebfc
     logging.debug("Starting list/monitor operation")
d4ebfc
     result = {}
d4ebfc
     try:
d4ebfc
+        apiversion = options.get("--apiversion")
d4ebfc
         namespace = options.get("--namespace")
d4ebfc
         include_uninitialized = True
d4ebfc
-        vm_api = conn.resources.get(api_version=API_VERSION, kind='VirtualMachine')
d4ebfc
+        vm_api = conn.resources.get(api_version=apiversion, kind='VirtualMachine')
d4ebfc
         vm_list = vm_api.get(namespace=namespace)
d4ebfc
         for vm in vm_list.items:
d4ebfc
             result[vm.metadata.name] = ("", None)
d4ebfc
@@ -30,18 +31,21 @@
d4ebfc
 def get_power_status(conn, options):
d4ebfc
     logging.debug("Starting get status operation")
d4ebfc
     try:
d4ebfc
+        apiversion = options.get("--apiversion")
d4ebfc
         namespace = options.get("--namespace")
d4ebfc
         name = options.get("--plug")
d4ebfc
-        vmi_api = conn.resources.get(api_version=API_VERSION,
d4ebfc
+        vmi_api = conn.resources.get(api_version=apiversion,
d4ebfc
                                               kind='VirtualMachineInstance')
d4ebfc
         vmi = vmi_api.get(name=name, namespace=namespace)
d4ebfc
-        if vmi is not None:
d4ebfc
-            phase = vmi.status.phase
d4ebfc
-            if phase == "Running":
d4ebfc
-                return "on"
d4ebfc
-        return "off"
d4ebfc
+        return translate_status(vmi.status.phase)
d4ebfc
     except ApiException as e:
d4ebfc
         if e.status == 404:
d4ebfc
+            try:
d4ebfc
+                vm_api = conn.resources.get(api_version=apiversion, kind='VirtualMachine')
d4ebfc
+                vm = vm_api.get(name=name, namespace=namespace)
d4ebfc
+            except ApiException as e:
d4ebfc
+                logging.error("VM %s doesn't exist", name)
d4ebfc
+                fail(EC_FETCH_VM_UUID)
d4ebfc
             return "off"
d4ebfc
         logging.error("Failed to get power status, with API Exception: %s", e)
d4ebfc
         fail(EC_STATUS)
d4ebfc
@@ -49,38 +53,53 @@
d4ebfc
         logging.error("Failed to get power status, with Exception: %s", e)
d4ebfc
         fail(EC_STATUS)
d4ebfc
 
d4ebfc
+def translate_status(instance_status):
d4ebfc
+    if instance_status == "Running":
d4ebfc
+        return "on"
d4ebfc
+    return "unknown"
d4ebfc
+
d4ebfc
 def set_power_status(conn, options):
d4ebfc
     logging.debug("Starting set status operation")
d4ebfc
     try:
d4ebfc
+        apiversion= options.get("--apiversion")
d4ebfc
         namespace = options.get("--namespace")
d4ebfc
         name = options.get("--plug")
d4ebfc
         action = 'start' if options["--action"] == "on" else 'stop'
d4ebfc
-        virtctl_vm_action(conn, action, namespace, name)
d4ebfc
+        virtctl_vm_action(conn, action, namespace, name, apiversion)
d4ebfc
     except Exception as e:
d4ebfc
         logging.error("Failed to set power status, with Exception: %s", e)
d4ebfc
         fail(EC_STATUS)
d4ebfc
 
d4ebfc
 def define_new_opts():
d4ebfc
-	all_opt["namespace"] = {
d4ebfc
-		"getopt" : ":",
d4ebfc
-		"longopt" : "namespace",
d4ebfc
-		"help" : "--namespace=[namespace]        Namespace of the KubeVirt machine",
d4ebfc
-		"shortdesc" : "Namespace of the KubeVirt machine.",
d4ebfc
-		"required" : "1",
d4ebfc
-		"order" : 2
d4ebfc
-	}
d4ebfc
-	all_opt["kubeconfig"] = {
d4ebfc
-		"getopt" : ":",
d4ebfc
-		"longopt" : "kubeconfig",
d4ebfc
-		"help" : "--kubeconfig=[kubeconfig]      Kubeconfig file path",
d4ebfc
-		"shortdesc": "Kubeconfig file path",
d4ebfc
-		"required": "0",
d4ebfc
-		"order": 4
d4ebfc
-	}
d4ebfc
+    all_opt["namespace"] = {
d4ebfc
+        "getopt" : ":",
d4ebfc
+        "longopt" : "namespace",
d4ebfc
+        "help" : "--namespace=[namespace]        Namespace of the KubeVirt machine",
d4ebfc
+        "shortdesc" : "Namespace of the KubeVirt machine.",
d4ebfc
+        "required" : "1",
d4ebfc
+        "order" : 2
d4ebfc
+    }
d4ebfc
+    all_opt["kubeconfig"] = {
d4ebfc
+        "getopt" : ":",
d4ebfc
+        "longopt" : "kubeconfig",
d4ebfc
+        "help" : "--kubeconfig=[kubeconfig]      Kubeconfig file path",
d4ebfc
+        "shortdesc": "Kubeconfig file path",
d4ebfc
+        "required": "0",
d4ebfc
+        "order": 4
d4ebfc
+    }
d4ebfc
+    all_opt["apiversion"] = {
d4ebfc
+        "getopt" : ":",
d4ebfc
+        "longopt" : "apiversion",
d4ebfc
+        "help" : "--apiversion=[apiversion]      Version of the KubeVirt API",
d4ebfc
+        "shortdesc" : "Version of the KubeVirt API.",
d4ebfc
+        "required" : "0",
d4ebfc
+        "default" : "kubevirt.io/v1",
d4ebfc
+        "order" : 5
d4ebfc
+    }
d4ebfc
 
d4ebfc
-def virtctl_vm_action(conn, action, namespace, name):
d4ebfc
+def virtctl_vm_action(conn, action, namespace, name, apiversion):
d4ebfc
     path = '/apis/subresources.{api_version}/namespaces/{namespace}/virtualmachines/{name}/{action}'
d4ebfc
-    path = path.format(api_version=API_VERSION, namespace=namespace, name=name, action=action)
d4ebfc
+    path = path.format(api_version=apiversion, namespace=namespace, name=name, action=action)
d4ebfc
     return conn.request('put', path, header_params={'accept': '*/*'})
d4ebfc
 
d4ebfc
 def validate_options(required_options_list, options):
d4ebfc
@@ -92,8 +111,13 @@
d4ebfc
 def main():
d4ebfc
     conn = None
d4ebfc
 
d4ebfc
-    device_opt = ["port", "namespace", "kubeconfig", "separator", "no_password"]
d4ebfc
+    device_opt = ["port", "namespace", "kubeconfig", "ssl_insecure", "no_password", "apiversion"]
d4ebfc
+
d4ebfc
+    atexit.register(atexit_handler)
d4ebfc
     define_new_opts()
d4ebfc
+
d4ebfc
+    all_opt["power_timeout"]["default"] = "40"
d4ebfc
+
d4ebfc
     options = check_input(device_opt, process_input(device_opt))
d4ebfc
 
d4ebfc
     docs = {}
d4ebfc
@@ -106,6 +130,11 @@
d4ebfc
 
d4ebfc
     validate_options(['--namespace'], options)
d4ebfc
 
d4ebfc
+    # Disable insecure-certificate-warning message
d4ebfc
+    if "--ssl-insecure" in options:
d4ebfc
+        import urllib3
d4ebfc
+        urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
d4ebfc
+
d4ebfc
     try:
d4ebfc
         from kubernetes import config
d4ebfc
         from openshift.dynamic import DynamicClient