blob: 6d2edef81c32e7b4d96957e6021d79e27a370aba [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:
44 if s.name == container_name:
45 ready = s.ready
46 if not ready:
BorislavG06518262018-02-14 17:40:49 +020047 log.info(container_name + " is not ready.")
Mandeep Khindad6ea9872017-06-24 11:49:37 -040048 else:
BorislavG06518262018-02-14 17:40:49 +020049 log.info(container_name + " is ready!")
Mandeep Khindad6ea9872017-06-24 11:49:37 -040050 else:
51 continue
52 return ready
53 except Exception as e:
54 log.error("Exception when calling list_namespaced_pod: %s\n" % e)
55
56
BorislavG06518262018-02-14 17:40:49 +020057DEF_TIMEOUT = 10
58DESCRIPTION = "Kubernetes container readiness check utility"
59USAGE = "Usage: ready.py [-t <timeout>] -c <container_name> [-c <container_name> ...]\n" \
60 "where\n" \
61 "<timeout> - wait for container readiness timeout in min, default is " + str(DEF_TIMEOUT) + "\n" \
62 "<container_name> - name of the container to wait for\n"
63
64def main(argv):
Mandeep Khindad6ea9872017-06-24 11:49:37 -040065 # args are a list of container names
BorislavG06518262018-02-14 17:40:49 +020066 container_names = []
67 timeout = DEF_TIMEOUT
68 try:
69 opts, args = getopt.getopt(argv, "hc:t:", ["container-name=", "timeout=", "help"])
70 for opt, arg in opts:
71 if opt in ("-h", "--help"):
72 print("%s\n\n%s" % (DESCRIPTION, USAGE))
73 sys.exit()
74 elif opt in ("-c", "--container-name"):
75 container_names.append(arg)
76 elif opt in ("-t", "--timeout"):
77 timeout = float(arg)
78 except (getopt.GetoptError, ValueError) as e:
79 print("Error parsing input parameters: %s\n" % e)
80 print(USAGE)
81 sys.exit(2)
82 if container_names.__len__() == 0:
83 print("Missing required input parameter(s)\n")
84 print(USAGE)
85 sys.exit(2)
86
87 for container_name in container_names:
88 timeout = time.time() + timeout * 60
Mandeep Khindad6ea9872017-06-24 11:49:37 -040089 while True:
90 ready = is_ready(container_name)
91 if ready is True:
92 break
93 elif time.time() > timeout:
BorislavG06518262018-02-14 17:40:49 +020094 log.warning("timed out waiting for '" + container_name + "' to be ready")
Mandeep Khindad6ea9872017-06-24 11:49:37 -040095 exit(1)
96 else:
97 time.sleep(5)
98
99
100if __name__ == "__main__":
BorislavG06518262018-02-14 17:40:49 +0200101 main(sys.argv[1:])
Mandeep Khindad6ea9872017-06-24 11:49:37 -0400102