blob: f4a5e5da8fc3b852b7a5ab7f836c6c7c07a44e70 [file] [log] [blame]
Mandeep Khindad6ea9872017-06-24 11:49:37 -04001#!/usr/bin/python
BorislavG06518262018-02-14 17:40:49 +02002import getopt
3import logging
4import os
5import sys
6import time
Mandeep Khindad6ea9872017-06-24 11:49:37 -04007
BorislavG06518262018-02-14 17:40:49 +02008from kubernetes import client
9
10# extract env variables.
Mandeep Khindad6ea9872017-06-24 11:49:37 -040011namespace = os.environ['NAMESPACE']
12cert = os.environ['CERT']
13host = os.environ['KUBERNETES_SERVICE_HOST']
14token_path = os.environ['TOKEN']
15
16with open(token_path, 'r') as token_file:
17 token = token_file.read().replace('\n', '')
18
BorislavG06518262018-02-14 17:40:49 +020019# setup logging
Mandeep Khindad6ea9872017-06-24 11:49:37 -040020log = 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
BorislavG06518262018-02-14 17:40:49 +020027configuration = client.Configuration()
28configuration.host = "https://" + host
29configuration.ssl_ca_cert = cert
30configuration.api_key['authorization'] = token
31configuration.api_key_prefix['authorization'] = 'Bearer'
32coreV1Api = client.CoreV1Api(client.ApiClient(configuration))
Mandeep Khindad6ea9872017-06-24 11:49:37 -040033
34def is_ready(container_name):
Mandeep Khindad6ea9872017-06-24 11:49:37 -040035 ready = False
BorislavG06518262018-02-14 17:40:49 +020036 log.info("Checking if " + container_name + " is ready")
Mandeep Khindad6ea9872017-06-24 11:49:37 -040037 try:
BorislavG06518262018-02-14 17:40:49 +020038 response = coreV1Api.list_namespaced_pod(namespace=namespace, watch=False)
Mandeep Khindad6ea9872017-06-24 11:49:37 -040039 for i in response.items:
BorislavG07dc7db2018-02-14 15:23:51 +020040 # container_statuses can be None, which is non-iterable.
41 if i.status.container_statuses is None:
BorislavG06518262018-02-14 17:40:49 +020042 continue
Mandeep Khindad6ea9872017-06-24 11:49:37 -040043 for s in i.status.container_statuses:
David, Dana (dd775k)2988e542018-07-29 14:28:52 +030044 if i.metadata.owner_references[0].kind == "StatefulSet":
45 if i.metadata.name == container_name:
46 ready = s.ready
47 if not ready:
48 log.info(container_name + " is not ready.")
49 else:
50 log.info(container_name + " is ready!")
51 else:
52 continue
53 elif s.name == container_name:
Mandeep Khindad6ea9872017-06-24 11:49:37 -040054 ready = s.ready
55 if not ready:
BorislavG06518262018-02-14 17:40:49 +020056 log.info(container_name + " is not ready.")
Mandeep Khindad6ea9872017-06-24 11:49:37 -040057 else:
BorislavG06518262018-02-14 17:40:49 +020058 log.info(container_name + " is ready!")
Mandeep Khindad6ea9872017-06-24 11:49:37 -040059 else:
60 continue
61 return ready
62 except Exception as e:
63 log.error("Exception when calling list_namespaced_pod: %s\n" % e)
64
65
BorislavG06518262018-02-14 17:40:49 +020066DEF_TIMEOUT = 10
67DESCRIPTION = "Kubernetes container readiness check utility"
68USAGE = "Usage: ready.py [-t <timeout>] -c <container_name> [-c <container_name> ...]\n" \
69 "where\n" \
70 "<timeout> - wait for container readiness timeout in min, default is " + str(DEF_TIMEOUT) + "\n" \
71 "<container_name> - name of the container to wait for\n"
72
73def main(argv):
Mandeep Khindad6ea9872017-06-24 11:49:37 -040074 # args are a list of container names
BorislavG06518262018-02-14 17:40:49 +020075 container_names = []
76 timeout = DEF_TIMEOUT
77 try:
78 opts, args = getopt.getopt(argv, "hc:t:", ["container-name=", "timeout=", "help"])
79 for opt, arg in opts:
80 if opt in ("-h", "--help"):
81 print("%s\n\n%s" % (DESCRIPTION, USAGE))
82 sys.exit()
83 elif opt in ("-c", "--container-name"):
84 container_names.append(arg)
85 elif opt in ("-t", "--timeout"):
86 timeout = float(arg)
87 except (getopt.GetoptError, ValueError) as e:
88 print("Error parsing input parameters: %s\n" % e)
89 print(USAGE)
90 sys.exit(2)
91 if container_names.__len__() == 0:
92 print("Missing required input parameter(s)\n")
93 print(USAGE)
94 sys.exit(2)
95
96 for container_name in container_names:
97 timeout = time.time() + timeout * 60
Mandeep Khindad6ea9872017-06-24 11:49:37 -040098 while True:
99 ready = is_ready(container_name)
100 if ready is True:
101 break
102 elif time.time() > timeout:
BorislavG06518262018-02-14 17:40:49 +0200103 log.warning("timed out waiting for '" + container_name + "' to be ready")
Mandeep Khindad6ea9872017-06-24 11:49:37 -0400104 exit(1)
105 else:
106 time.sleep(5)
107
108
109if __name__ == "__main__":
BorislavG06518262018-02-14 17:40:49 +0200110 main(sys.argv[1:])
Mandeep Khindad6ea9872017-06-24 11:49:37 -0400111