Blame SOURCES/bz2000954-2-fence_kubevirt.patch

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