summaryrefslogtreecommitdiffstats
path: root/kubernetes/readiness/docker/init/ready.py
blob: 7cfb6cdcdeb7cb720959789e8f0501734bdc2c0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/python
from kubernetes import client, config
import time, argparse, logging, sys, os

#extract env variables.
namespace = os.environ['NAMESPACE']
cert = os.environ['CERT']
host = os.environ['KUBERNETES_SERVICE_HOST']
token_path = os.environ['TOKEN']

with open(token_path, 'r') as token_file:
    token = token_file.read().replace('\n', '')

client.configuration.host = "https://" + host
client.configuration.ssl_ca_cert = cert
client.configuration.api_key['authorization'] = token
client.configuration.api_key_prefix['authorization'] = 'Bearer'

#setup logging
log = logging.getLogger(__name__)
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
handler.setLevel(logging.INFO)
log.addHandler(handler)
log.setLevel(logging.INFO)


def is_ready(container_name):
    log.info( "Checking if " + container_name + "  is ready")
    # config.load_kube_config() # for local testing
    # namespace='onap-sdc' # for local testing
    v1 = client.CoreV1Api()

    ready = False

    try:
        response = v1.list_namespaced_pod(namespace=namespace, watch=False)
        for i in response.items:
            # container_statuses can be None, which is non-iterable.
            if i.status.container_statuses is None:
               continue
            for s in i.status.container_statuses:
                if s.name == container_name:
                    ready = s.ready
                    if not ready:
                        log.info( container_name + " is not ready.")
                    else:
                        log.info( container_name + " is ready!")
                else:
                    continue
        return ready
    except Exception as e:
        log.error("Exception when calling list_namespaced_pod: %s\n" % e)


def main(args):
    # args are a list of container names
    for container_name in args:
        # 5 min, TODO: make configurable
        timeout = time.time() + 60 * 10
        while True:
            ready = is_ready(container_name)
            if ready is True:
                break
            elif time.time() > timeout:
                log.warning( "timed out waiting for '" + container_name + "' to be ready")
                exit(1)
            else:
                time.sleep(5)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Process some names.')
    parser.add_argument('--container-name', action='append', required=True, help='A container name')
    args = parser.parse_args()
    arg_dict = vars(args)

    for arg in arg_dict.itervalues():
        main(arg)