|
|
021bc5 |
diff --color -uNr a/agents/kubevirt/fence_kubevirt.py b/agents/kubevirt/fence_kubevirt.py
|
|
|
021bc5 |
--- a/agents/kubevirt/fence_kubevirt.py 1970-01-01 01:00:00.000000000 +0100
|
|
|
021bc5 |
+++ b/agents/kubevirt/fence_kubevirt.py 2021-10-25 13:25:59.904501348 +0200
|
|
|
021bc5 |
@@ -0,0 +1,140 @@
|
|
|
021bc5 |
+#!@PYTHON@ -tt
|
|
|
021bc5 |
+
|
|
|
021bc5 |
+import sys
|
|
|
021bc5 |
+import logging
|
|
|
021bc5 |
+sys.path.append("@FENCEAGENTSLIBDIR@")
|
|
|
021bc5 |
+from fencing import *
|
|
|
021bc5 |
+from fencing import fail, fail_usage, run_delay, EC_STATUS
|
|
|
021bc5 |
+
|
|
|
021bc5 |
+try:
|
|
|
021bc5 |
+ sys.path.insert(0, '/usr/lib/fence-agents/bundled/kubevirt')
|
|
|
021bc5 |
+ from kubernetes.client.exceptions import ApiException
|
|
|
021bc5 |
+except ImportError:
|
|
|
021bc5 |
+ logging.error("Couldn\'t import kubernetes.client.exceptions.ApiException - not found or not accessible")
|
|
|
021bc5 |
+
|
|
|
021bc5 |
+def get_nodes_list(conn, options):
|
|
|
021bc5 |
+ logging.debug("Starting list/monitor operation")
|
|
|
021bc5 |
+ result = {}
|
|
|
021bc5 |
+ try:
|
|
|
021bc5 |
+ apiversion = options.get("--apiversion")
|
|
|
021bc5 |
+ namespace = options.get("--namespace")
|
|
|
021bc5 |
+ include_uninitialized = True
|
|
|
021bc5 |
+ vm_api = conn.resources.get(api_version=apiversion, kind='VirtualMachine')
|
|
|
021bc5 |
+ vm_list = vm_api.get(namespace=namespace)
|
|
|
021bc5 |
+ for vm in vm_list.items:
|
|
|
021bc5 |
+ result[vm.metadata.name] = ("", None)
|
|
|
021bc5 |
+ except Exception as e:
|
|
|
021bc5 |
+ logging.error("Exception when calling VirtualMachine list: %s", e)
|
|
|
021bc5 |
+ return result
|
|
|
021bc5 |
+
|
|
|
021bc5 |
+def get_power_status(conn, options):
|
|
|
021bc5 |
+ logging.debug("Starting get status operation")
|
|
|
021bc5 |
+ try:
|
|
|
021bc5 |
+ apiversion = options.get("--apiversion")
|
|
|
021bc5 |
+ namespace = options.get("--namespace")
|
|
|
021bc5 |
+ name = options.get("--plug")
|
|
|
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 |
+ except ApiException as e:
|
|
|
021bc5 |
+ if e.status == 404:
|
|
|
021bc5 |
+ return "off"
|
|
|
021bc5 |
+ logging.error("Failed to get power status, with API Exception: %s", e)
|
|
|
021bc5 |
+ fail(EC_STATUS)
|
|
|
021bc5 |
+ except Exception as e:
|
|
|
021bc5 |
+ logging.error("Failed to get power status, with Exception: %s", e)
|
|
|
021bc5 |
+ fail(EC_STATUS)
|
|
|
021bc5 |
+
|
|
|
021bc5 |
+def set_power_status(conn, options):
|
|
|
021bc5 |
+ logging.debug("Starting set status operation")
|
|
|
021bc5 |
+ try:
|
|
|
021bc5 |
+ apiversion= options.get("--apiversion")
|
|
|
021bc5 |
+ namespace = options.get("--namespace")
|
|
|
021bc5 |
+ name = options.get("--plug")
|
|
|
021bc5 |
+ action = 'start' if options["--action"] == "on" else 'stop'
|
|
|
021bc5 |
+ virtctl_vm_action(conn, action, namespace, name, apiversion)
|
|
|
021bc5 |
+ except Exception as e:
|
|
|
021bc5 |
+ logging.error("Failed to set power status, with Exception: %s", e)
|
|
|
021bc5 |
+ fail(EC_STATUS)
|
|
|
021bc5 |
+
|
|
|
021bc5 |
+def define_new_opts():
|
|
|
021bc5 |
+ all_opt["namespace"] = {
|
|
|
021bc5 |
+ "getopt" : ":",
|
|
|
021bc5 |
+ "longopt" : "namespace",
|
|
|
021bc5 |
+ "help" : "--namespace=[namespace] Namespace of the KubeVirt machine",
|
|
|
021bc5 |
+ "shortdesc" : "Namespace of the KubeVirt machine.",
|
|
|
021bc5 |
+ "required" : "1",
|
|
|
021bc5 |
+ "order" : 2
|
|
|
021bc5 |
+ }
|
|
|
021bc5 |
+ all_opt["kubeconfig"] = {
|
|
|
021bc5 |
+ "getopt" : ":",
|
|
|
021bc5 |
+ "longopt" : "kubeconfig",
|
|
|
021bc5 |
+ "help" : "--kubeconfig=[kubeconfig] Kubeconfig file path",
|
|
|
021bc5 |
+ "shortdesc": "Kubeconfig file path",
|
|
|
021bc5 |
+ "required": "0",
|
|
|
021bc5 |
+ "order": 4
|
|
|
021bc5 |
+ }
|
|
|
021bc5 |
+ all_opt["apiversion"] = {
|
|
|
021bc5 |
+ "getopt" : ":",
|
|
|
021bc5 |
+ "longopt" : "apiversion",
|
|
|
021bc5 |
+ "help" : "--apiversion=[apiversion] Version of the KubeVirt API",
|
|
|
021bc5 |
+ "shortdesc" : "Version of the KubeVirt API.",
|
|
|
021bc5 |
+ "required" : "0",
|
|
|
021bc5 |
+ "default" : "kubevirt.io/v1",
|
|
|
021bc5 |
+ "order" : 5
|
|
|
021bc5 |
+ }
|
|
|
021bc5 |
+
|
|
|
021bc5 |
+def virtctl_vm_action(conn, action, namespace, name, apiversion):
|
|
|
021bc5 |
+ path = '/apis/subresources.{api_version}/namespaces/{namespace}/virtualmachines/{name}/{action}'
|
|
|
021bc5 |
+ path = path.format(api_version=apiversion, namespace=namespace, name=name, action=action)
|
|
|
021bc5 |
+ return conn.request('put', path, header_params={'accept': '*/*'})
|
|
|
021bc5 |
+
|
|
|
021bc5 |
+def validate_options(required_options_list, options):
|
|
|
021bc5 |
+ for required_option in required_options_list:
|
|
|
021bc5 |
+ if required_option not in options:
|
|
|
021bc5 |
+ fail_usage("Failed: %s option must be provided" % required_option)
|
|
|
021bc5 |
+
|
|
|
021bc5 |
+# Main agent method
|
|
|
021bc5 |
+def main():
|
|
|
021bc5 |
+ conn = None
|
|
|
021bc5 |
+
|
|
|
021bc5 |
+ device_opt = ["port", "namespace", "kubeconfig", "ssl_insecure", "no_password", "apiversion"]
|
|
|
021bc5 |
+ define_new_opts()
|
|
|
021bc5 |
+ options = check_input(device_opt, process_input(device_opt))
|
|
|
021bc5 |
+
|
|
|
021bc5 |
+ docs = {}
|
|
|
021bc5 |
+ docs["shortdesc"] = "Fence agent for KubeVirt"
|
|
|
021bc5 |
+ docs["longdesc"] = "fence_kubevirt is an I/O Fencing agent for KubeVirt."
|
|
|
021bc5 |
+ docs["vendorurl"] = "https://kubevirt.io/"
|
|
|
021bc5 |
+ show_docs(options, docs)
|
|
|
021bc5 |
+
|
|
|
021bc5 |
+ run_delay(options)
|
|
|
021bc5 |
+
|
|
|
021bc5 |
+ validate_options(['--namespace'], options)
|
|
|
021bc5 |
+
|
|
|
021bc5 |
+ # Disable insecure-certificate-warning message
|
|
|
021bc5 |
+ if "--ssl-insecure" in options:
|
|
|
021bc5 |
+ import urllib3
|
|
|
021bc5 |
+ urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
|
|
021bc5 |
+
|
|
|
021bc5 |
+ try:
|
|
|
021bc5 |
+ from kubernetes import config
|
|
|
021bc5 |
+ from openshift.dynamic import DynamicClient
|
|
|
021bc5 |
+ kubeconfig = options.get('--kubeconfig')
|
|
|
021bc5 |
+ k8s_client = config.new_client_from_config(config_file=kubeconfig)
|
|
|
021bc5 |
+ conn = DynamicClient(k8s_client)
|
|
|
021bc5 |
+ except ImportError:
|
|
|
021bc5 |
+ logging.error("Couldn\'t import kubernetes.config or "
|
|
|
021bc5 |
+ "openshift.dynamic.DynamicClient - not found or not accessible")
|
|
|
021bc5 |
+
|
|
|
021bc5 |
+ # Operate the fencing device
|
|
|
021bc5 |
+ result = fence_action(conn, options, set_power_status, get_power_status, get_nodes_list)
|
|
|
021bc5 |
+ sys.exit(result)
|
|
|
021bc5 |
+
|
|
|
021bc5 |
+if __name__ == "__main__":
|
|
|
021bc5 |
+ main()
|