bugfix: right cols to get available pods
[bottlenecks.git] / utils / k8s_setup / k8s_utils.py
1 #!/usr/bin/env python
2 ##############################################################################
3 # Copyright (c) 2018 Huawei Technologies Co.,Ltd and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11 import os
12 import commands
13 import json
14 import utils.logger as log
15 from kubernetes import client, watch
16
17
18 LOG = log.Logger(__name__).getLogger()
19 INSTALLER_TYPE = os.getenv("INSTALLER_TYPE")
20 K8S_UTILS = "/home/opnfv/bottlenecks/utils/k8s_setup"
21
22
23 def get_config_path(INSTALLER_TYPE=None, K8S_CONFIG_PATH="/tmp/k8s_config"):
24     if INSTALLER_TYPE:
25         CMD = "bash " + K8S_UTILS + "/k8s_config_pre.sh -i " \
26               + INSTALLER_TYPE + \
27               " -c " + K8S_CONFIG_PATH
28         LOG.info("Executing command: " + CMD)
29         os.popen(CMD)
30     else:
31         if not os.path.exists(K8S_CONFIG_PATH):
32             raise Exception("Must at least specify the path \
33 of k8s config!")
34     return K8S_CONFIG_PATH
35
36
37 def get_core_api(version='v1'):
38     if version.lower() == 'v1':
39         API = client.CoreV1Api()
40         LOG.info(API)
41     else:
42         raise Exception("Must input a valid verison!")
43     return API
44
45
46 def get_apps_api(version='v1'):
47     if version.lower() == 'v1':
48         API = client.AppsV1Api()
49         LOG.info(API)
50     else:
51         raise Exception("Must input a valid verison!")
52     return API
53
54
55 def get_namespace_status(namespace):
56     CMD = ("kubectl get ns | grep %s" % namespace)
57     namespace_existed = commands.getstatusoutput(CMD)
58     return namespace_existed
59
60
61 def get_deployment_status(name, namespace):
62     CMD = ("kubectl get deployment --namespace={} | grep {}".format(
63         namespace, name))
64     deployment_existed = commands.getstatusoutput(CMD)
65     return deployment_existed
66
67
68 def get_available_pods(name, namespace):
69     CMD = ("kubectl get deployment --namespace={} | grep {}".format(
70         namespace, name) + " | awk '{print $4}'")
71     available_pods = commands.getstatusoutput(CMD)
72     return int(available_pods[1])
73
74
75 def watch_namespace(namespace, count=3, stop=None, request_timeout=0):
76     w = watch.Watch()
77     LOG.debug("Watch object generated: {}".format(w))
78     LOG.info("Watch stream generated: {}".format(
79              w.stream(namespace, _request_timeout=request_timeout)))
80     for event in w.stream(namespace, _request_timeout=request_timeout):
81         LOG.info("Event: %s %s" %
82                  (event['type'], event['object'].metadata.name))
83         if event['object'].metadata.name == stop:
84             LOG.info("Namesapce successfully added.\n")
85             w.stop()
86         count -= 1
87         if not count:
88             LOG.info("Ended.\n")
89             w.stop()
90
91
92 def write_json(data, file_name):
93     with open(file_name, "a") as f:
94         f.write(json.dumps(data, f))
95         f.write("\n")