diff options
author | Yang (Gabriel) Yu <Gabriel.yuyang@huawei.com> | 2018-08-11 11:14:43 +0800 |
---|---|---|
committer | Yang (Gabriel) Yu <Gabriel.yuyang@huawei.com> | 2018-08-13 16:26:57 +0800 |
commit | 480c3c51a0a2bcc67ac5ca1d96cb63153bf9fff8 (patch) | |
tree | 2108cd907003b1bd2229be6959b3ed4572d8027d /utils | |
parent | 976ebf4916c7c6487bf98d8c14f985ff9c2dabac (diff) |
add k8s util functions for compatibility of upstream changes
JIRA: BOTTLENECK-243
Change-Id: Idcbe75a4bcd8fedde4e4b95ae999eebbaaa01645
Signed-off-by: Yang (Gabriel) Yu <Gabriel.yuyang@huawei.com>
Diffstat (limited to 'utils')
-rw-r--r-- | utils/k8s_setup/k8s_utils.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/utils/k8s_setup/k8s_utils.py b/utils/k8s_setup/k8s_utils.py index afcdb307..7195bf23 100644 --- a/utils/k8s_setup/k8s_utils.py +++ b/utils/k8s_setup/k8s_utils.py @@ -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() |