Blame SOURCES/bz1977588-5-fence_kubevirt-get-namespace-from-context.patch

a63592
From 647841dea9d93922779a4aa7d0b5f52f5bc2b4e9 Mon Sep 17 00:00:00 2001
a63592
From: Dan Kenigsberg <danken@redhat.com>
a63592
Date: Thu, 13 Jan 2022 14:57:26 +0200
a63592
Subject: [PATCH] fence_kubevirt: take default namespace from context
a63592
a63592
If --namespace is not provided to kubectl, a default one is taken from
a63592
kubeconfig context. Let fence_kubevirt behave similarly.
a63592
a63592
Signed-off-by: Dan Kenigsberg <danken@redhat.com>
a63592
---
a63592
 agents/kubevirt/fence_kubevirt.py      | 24 +++++++++++++-----------
a63592
 2 files changed, 14 insertions(+), 12 deletions(-)
a63592
a63592
diff --git a/agents/kubevirt/fence_kubevirt.py b/agents/kubevirt/fence_kubevirt.py
a63592
index 8392b75a0..8c27a0334 100755
a63592
--- a/agents/kubevirt/fence_kubevirt.py
a63592
+++ b/agents/kubevirt/fence_kubevirt.py
a63592
@@ -12,12 +12,21 @@
a63592
 except ImportError:
a63592
     logging.error("Couldn\'t import kubernetes.client.exceptions.ApiException - not found or not accessible")
a63592
 
a63592
+def _get_namespace(options):
a63592
+    from kubernetes import config
a63592
+
a63592
+    ns = options.get("--namespace")
a63592
+    if ns is None:
a63592
+        ns = config.kube_config.list_kube_config_contexts()[1]['context']['namespace']
a63592
+
a63592
+    return ns
a63592
+
a63592
 def get_nodes_list(conn, options):
a63592
     logging.debug("Starting list/monitor operation")
a63592
     result = {}
a63592
     try:
a63592
         apiversion = options.get("--apiversion")
a63592
-        namespace = options.get("--namespace")
a63592
+        namespace = _get_namespace(options)
a63592
         include_uninitialized = True
a63592
         vm_api = conn.resources.get(api_version=apiversion, kind='VirtualMachine')
a63592
         vm_list = vm_api.get(namespace=namespace)
a63592
@@ -31,7 +40,7 @@ def get_power_status(conn, options):
a63592
     logging.debug("Starting get status operation")
a63592
     try:
a63592
         apiversion = options.get("--apiversion")
a63592
-        namespace = options.get("--namespace")
a63592
+        namespace = _get_namespace(options)
a63592
         name = options.get("--plug")
a63592
         vmi_api = conn.resources.get(api_version=apiversion,
a63592
                                               kind='VirtualMachineInstance')
a63592
@@ -61,7 +70,7 @@ def set_power_status(conn, options):
a63592
     logging.debug("Starting set status operation")
a63592
     try:
a63592
         apiversion= options.get("--apiversion")
a63592
-        namespace = options.get("--namespace")
a63592
+        namespace = _get_namespace(options)
a63592
         name = options.get("--plug")
a63592
         action = 'start' if options["--action"] == "on" else 'stop'
a63592
         virtctl_vm_action(conn, action, namespace, name, apiversion)
a63592
@@ -75,7 +84,7 @@ def define_new_opts():
a63592
         "longopt" : "namespace",
a63592
         "help" : "--namespace=[namespace]        Namespace of the KubeVirt machine",
a63592
         "shortdesc" : "Namespace of the KubeVirt machine.",
a63592
-        "required" : "1",
a63592
+        "required" : "0",
a63592
         "order" : 2
a63592
     }
a63592
     all_opt["kubeconfig"] = {
a63592
@@ -101,11 +110,6 @@ def virtctl_vm_action(conn, action, namespace, name, apiversion):
a63592
     path = path.format(api_version=apiversion, namespace=namespace, name=name, action=action)
a63592
     return conn.request('put', path, header_params={'accept': '*/*'})
a63592
 
a63592
-def validate_options(required_options_list, options):
a63592
-    for required_option in required_options_list:
a63592
-        if required_option not in options:
a63592
-            fail_usage("Failed: %s option must be provided" % required_option)
a63592
-
a63592
 # Main agent method
a63592
 def main():
a63592
     conn = None
a63592
@@ -127,8 +131,6 @@ def main():
a63592
 
a63592
     run_delay(options)
a63592
 
a63592
-    validate_options(['--namespace'], options)
a63592
-
a63592
     # Disable insecure-certificate-warning message
a63592
     if "--ssl-insecure" in options:
a63592
         import urllib3