Cloudify support for OOM
[oom.git] / cloudify-onap / docker-custom-readiness / ready.py
1 #!/usr/bin/python
2 #from kubernetes import client, config
3 import kubernetes
4 import time, argparse, logging, sys, os, base64
5 import yaml
6
7 #setup logging
8 log = logging.getLogger(__name__)
9 handler = logging.StreamHandler(sys.stdout)
10 handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
11 handler.setLevel(logging.DEBUG)
12 log.addHandler(handler)
13 log.setLevel(logging.DEBUG)
14
15
16 def is_ready(container_name):
17     log.info( "Checking if " + container_name + "  is ready")
18
19     kubernetes.config.kube_config.KubeConfigLoader(config_dict=get_k8s_config_env()).load_and_set()
20     client = kubernetes.client
21     namespace = get_namespace_env()
22     v1 = client.CoreV1Api()
23
24     ready = False
25
26     try:
27         response = v1.list_namespaced_pod(namespace=namespace, watch=False)
28         for i in response.items:
29             for s in i.status.container_statuses:
30                 if s.name == container_name:
31                     log.debug ( "response %s" % response )
32                     ready = s.ready
33                     if not ready:
34                         log.info( container_name + " is not ready.")
35                     else:
36                         log.info( container_name + " is ready!")
37                 else:
38                     continue
39         return ready
40     except Exception as e:
41         log.error("Exception when calling list_namespaced_pod: %s\n" % e)
42
43
44 def get_k8s_config_env():
45     try:
46         k8s_config_env = os.environ.get("K8S_CONFIG_B64")
47         decoded = base64.b64decode(k8s_config_env)
48         return yaml.load(decoded)
49     except KeyError as ke:
50         raise Exception("K8S_CONFIG_B64 variable is not set.")
51
52
53 def get_namespace_env():
54     try:
55         namespace_env = os.environ.get("NAMESPACE")
56         return namespace_env
57     except KeyError as ke:
58         raise Exception("NAMESPACE variable is not set.")
59
60
61 def main(args):#from kubernetes import client, config
62
63     # args are a list of container names
64     for container_name in args:
65         # 5 min, TODO: make configurable
66         timeout = time.time() + 60 * 10
67         while True:
68             ready = is_ready(container_name)
69             if ready is True:
70                 break
71             elif time.time() > timeout:
72                 log.warning( "timed out waiting for '" + container_name + "' to be ready")
73                 exit(1)
74             else:
75                 time.sleep(5)
76
77
78 if __name__ == "__main__":
79     parser = argparse.ArgumentParser(description='Process some names.')
80     parser.add_argument('--container-name', action='append', required=True, help='A container name')
81     args = parser.parse_args()
82     arg_dict = vars(args)
83
84     for arg in arg_dict.itervalues():
85         main(arg)