add k8s util functions for compatibility of upstream changes 65/60865/3
authorYang (Gabriel) Yu <Gabriel.yuyang@huawei.com>
Sat, 11 Aug 2018 03:14:43 +0000 (11:14 +0800)
committerYang (Gabriel) Yu <Gabriel.yuyang@huawei.com>
Mon, 13 Aug 2018 08:26:57 +0000 (16:26 +0800)
JIRA: BOTTLENECK-243

Change-Id: Idcbe75a4bcd8fedde4e4b95ae999eebbaaa01645
Signed-off-by: Yang (Gabriel) Yu <Gabriel.yuyang@huawei.com>
utils/k8s_setup/k8s_utils.py

index afcdb30..7195bf2 100644 (file)
@@ -10,6 +10,8 @@
 
 import os
 import utils.logger as log
+from kubernetes import client, watch
+
 
 LOG = log.Logger(__name__).getLogger()
 INSTALLER_TYPE = os.getenv("INSTALLER_TYPE")
@@ -26,3 +28,29 @@ def get_config_path(INSTALLER_TYPE=None, CONFIG_PATH="/tmp/k8s_config"):
             raise Exception("Must at least specify the path \
 of k8s config!")
     return CONFIG_PATH
+
+
+def get_core_api(version='v1'):
+    if version.lower() == 'v1':
+        API = client.CoreV1Api()
+        LOG.info(API)
+    else:
+        raise Exception("Must input a validate verison!")
+    return API
+
+
+def watch_namespace(namespace, count=3, stop=None, request_timeout=0):
+    w = watch.Watch()
+    LOG.debug("Watch object generated: {}".format(w))
+    LOG.info("Watch stream generated: {}".format(
+             w.stream(namespace, _request_timeout=request_timeout)))
+    for event in w.stream(namespace, _request_timeout=request_timeout):
+        LOG.info("Event: %s %s" %
+                 (event['type'], event['object'].metadata.name))
+        if event['object'].metadata.name == stop:
+            LOG.info("Namesapce successfully added.\n")
+            w.stop()
+        count -= 1
+        if not count:
+            LOG.info("Ended.\n")
+            w.stop()