blob: c5b55eef187e02d57536180f0f14e459215ae896 [file] [log] [blame]
Mandeep Khindad6ea9872017-06-24 11:49:37 -04001#!/usr/bin/python
2from kubernetes import client, config
3import time, argparse, logging, sys, os
4
5#extract env variables.
6namespace = os.environ['NAMESPACE']
7cert = os.environ['CERT']
8host = os.environ['KUBERNETES_SERVICE_HOST']
9token_path = os.environ['TOKEN']
10
11with open(token_path, 'r') as token_file:
12 token = token_file.read().replace('\n', '')
13
14client.configuration.host = "https://" + host
15client.configuration.ssl_ca_cert = cert
16client.configuration.api_key['authorization'] = token
17client.configuration.api_key_prefix['authorization'] = 'Bearer'
18
19#setup logging
20log = logging.getLogger(__name__)
21handler = logging.StreamHandler(sys.stdout)
22handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
23handler.setLevel(logging.INFO)
24log.addHandler(handler)
25log.setLevel(logging.INFO)
26
27
28def is_ready(container_name):
29 log.info( "Checking if " + container_name + " is ready")
30 # config.load_kube_config() # for local testing
31 # namespace='onap-sdc' # for local testing
32 v1 = client.CoreV1Api()
33
34 ready = False
35
36 try:
37 response = v1.list_namespaced_pod(namespace=namespace, watch=False)
38 for i in response.items:
39 for s in i.status.container_statuses:
40 if s.name == container_name:
41 ready = s.ready
42 if not ready:
43 log.info( container_name + " is not ready.")
44 else:
45 log.info( container_name + " is ready!")
46 else:
47 continue
48 return ready
49 except Exception as e:
50 log.error("Exception when calling list_namespaced_pod: %s\n" % e)
51
52
53def main(args):
54 # args are a list of container names
55 for container_name in args:
56 # 5 min, TODO: make configurable
57 timeout = time.time() + 60 * 10
58 while True:
59 ready = is_ready(container_name)
60 if ready is True:
61 break
62 elif time.time() > timeout:
63 log.warning( "timed out waiting for '" + container_name + "' to be ready")
64 exit(1)
65 else:
66 time.sleep(5)
67
68
69if __name__ == "__main__":
70 parser = argparse.ArgumentParser(description='Process some names.')
71 parser.add_argument('--container-name', action='append', required=True, help='A container name')
72 args = parser.parse_args()
73 arg_dict = vars(args)
74
75 for arg in arg_dict.itervalues():
76 main(arg)