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