From: Yang (Gabriel) Yu Date: Sat, 11 Aug 2018 03:14:43 +0000 (+0800) Subject: add k8s util functions for compatibility of upstream changes X-Git-Tag: opnfv-7.1.0~14 X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?p=bottlenecks.git;a=commitdiff_plain;h=480c3c51a0a2bcc67ac5ca1d96cb63153bf9fff8 add k8s util functions for compatibility of upstream changes JIRA: BOTTLENECK-243 Change-Id: Idcbe75a4bcd8fedde4e4b95ae999eebbaaa01645 Signed-off-by: Yang (Gabriel) Yu --- 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()