Blame SOURCES/bz2000954-3-fence_kubevirt-get-namespace-from-context.patch

d4ebfc
From 647841dea9d93922779a4aa7d0b5f52f5bc2b4e9 Mon Sep 17 00:00:00 2001
d4ebfc
From: Dan Kenigsberg <danken@redhat.com>
d4ebfc
Date: Thu, 13 Jan 2022 14:57:26 +0200
d4ebfc
Subject: [PATCH] fence_kubevirt: take default namespace from context
d4ebfc
d4ebfc
If --namespace is not provided to kubectl, a default one is taken from
d4ebfc
kubeconfig context. Let fence_kubevirt behave similarly.
d4ebfc
d4ebfc
Signed-off-by: Dan Kenigsberg <danken@redhat.com>
d4ebfc
---
d4ebfc
 agents/kubevirt/fence_kubevirt.py      | 24 +++++++++++++-----------
d4ebfc
 tests/data/metadata/fence_kubevirt.xml |  2 +-
d4ebfc
 2 files changed, 14 insertions(+), 12 deletions(-)
d4ebfc
d4ebfc
diff --git a/agents/kubevirt/fence_kubevirt.py b/agents/kubevirt/fence_kubevirt.py
d4ebfc
index 8392b75a0..8c27a0334 100755
d4ebfc
--- a/agents/kubevirt/fence_kubevirt.py
d4ebfc
+++ b/agents/kubevirt/fence_kubevirt.py
d4ebfc
@@ -12,12 +12,21 @@
d4ebfc
 except ImportError:
d4ebfc
     logging.error("Couldn\'t import kubernetes.client.exceptions.ApiException - not found or not accessible")
d4ebfc
 
d4ebfc
+def _get_namespace(options):
d4ebfc
+    from kubernetes import config
d4ebfc
+
d4ebfc
+    ns = options.get("--namespace")
d4ebfc
+    if ns is None:
d4ebfc
+        ns = config.kube_config.list_kube_config_contexts()[1]['context']['namespace']
d4ebfc
+
d4ebfc
+    return ns
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
+        namespace = _get_namespace(options)
d4ebfc
         include_uninitialized = True
d4ebfc
         vm_api = conn.resources.get(api_version=apiversion, kind='VirtualMachine')
d4ebfc
         vm_list = vm_api.get(namespace=namespace)
d4ebfc
@@ -31,7 +40,7 @@ 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
+        namespace = _get_namespace(options)
d4ebfc
         name = options.get("--plug")
d4ebfc
         vmi_api = conn.resources.get(api_version=apiversion,
d4ebfc
                                               kind='VirtualMachineInstance')
d4ebfc
@@ -61,7 +70,7 @@ 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
+        namespace = _get_namespace(options)
d4ebfc
         name = options.get("--plug")
d4ebfc
         action = 'start' if options["--action"] == "on" else 'stop'
d4ebfc
         virtctl_vm_action(conn, action, namespace, name, apiversion)
d4ebfc
@@ -75,7 +84,7 @@ def define_new_opts():
d4ebfc
         "longopt" : "namespace",
d4ebfc
         "help" : "--namespace=[namespace]        Namespace of the KubeVirt machine",
d4ebfc
         "shortdesc" : "Namespace of the KubeVirt machine.",
d4ebfc
-        "required" : "1",
d4ebfc
+        "required" : "0",
d4ebfc
         "order" : 2
d4ebfc
     }
d4ebfc
     all_opt["kubeconfig"] = {
d4ebfc
@@ -101,11 +110,6 @@ def virtctl_vm_action(conn, action, namespace, name, apiversion):
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
-    for required_option in required_options_list:
d4ebfc
-        if required_option not in options:
d4ebfc
-            fail_usage("Failed: %s option must be provided" % required_option)
d4ebfc
-
d4ebfc
 # Main agent method
d4ebfc
 def main():
d4ebfc
     conn = None
d4ebfc
@@ -127,8 +131,6 @@ def main():
d4ebfc
 
d4ebfc
     run_delay(options)
d4ebfc
 
d4ebfc
-    validate_options(['--namespace'], options)
d4ebfc
-
d4ebfc
     # Disable insecure-certificate-warning message
d4ebfc
     if "--ssl-insecure" in options:
d4ebfc
         import urllib3
d4ebfc
diff --git a/tests/data/metadata/fence_kubevirt.xml b/tests/data/metadata/fence_kubevirt.xml
d4ebfc
index 24e975587..ccb20c224 100644
d4ebfc
--- a/tests/data/metadata/fence_kubevirt.xml
d4ebfc
+++ b/tests/data/metadata/fence_kubevirt.xml
d4ebfc
@@ -23,7 +23,7 @@
d4ebfc
 		<content type="boolean"  />
d4ebfc
 		<shortdesc lang="en">Use SSL connection without verifying certificate</shortdesc>
d4ebfc
 	</parameter>
d4ebfc
-	<parameter name="namespace" unique="0" required="1">
d4ebfc
+	<parameter name="namespace" unique="0" required="0">
d4ebfc
 		<getopt mixed="--namespace=[namespace]" />
d4ebfc
 		<content type="string"  />
d4ebfc
 		<shortdesc lang="en">Namespace of the KubeVirt machine.</shortdesc>