blob: 87c09a444cf19ad4028926e437b5232924abba6d [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))
Mahendra Raghuwanshi6a8304c2019-01-28 09:49:30 +000033api_instance=client.ExtensionsV1beta1Api(client.ApiClient(configuration))
34api = client.AppsV1beta1Api(client.ApiClient(configuration))
35batchV1Api = client.BatchV1Api(client.ApiClient(configuration))
36
37def is_job_complete(job_name):
38 complete = False
39 log.info("Checking if " + job_name + " is complete")
40 response = ""
41 try:
42 response = batchV1Api.read_namespaced_job_status(job_name, namespace)
43 if response.status.succeeded == 1:
44 job_status_type = response.status.conditions[0].type
45 if job_status_type == "Complete":
46 complete = True
47 log.info(job_name + " is complete")
48 else:
49 log.info(job_name + " is not complete")
50 else:
51 log.info(job_name + " has not succeeded yet")
52 return complete
53 except Exception as e:
54 log.error("Exception when calling read_namespaced_job_status: %s\n" % e)
55
56def wait_for_statefulset_complete(statefulset_name):
57 try:
58 response = api.read_namespaced_stateful_set(statefulset_name, namespace)
59 s = response.status
60 if ( s.updated_replicas == response.spec.replicas and
61 s.replicas == response.spec.replicas and
62 s.ready_replicas == response.spec.replicas and
63 s.current_replicas == response.spec.replicas and
64 s.observed_generation == response.metadata.generation):
65 log.info("Statefulset " + statefulset_name + " is ready")
66 return True
67 else:
68 log.info("Statefulset " + statefulset_name + " is not ready")
69 return False
70 except Exception as e:
71 log.error("Exception when waiting for Statefulset status: %s\n" % e)
72
73def wait_for_deployment_complete(deployment_name):
74 try:
75 response = api.read_namespaced_deployment(deployment_name, namespace)
76 s = response.status
77 if ( s.unavailable_replicas == None and
78 s.updated_replicas == response.spec.replicas and
79 s.replicas == response.spec.replicas and
80 s.ready_replicas == response.spec.replicas and
81 s.observed_generation == response.metadata.generation):
82 log.info("Deployment " + deployment_name + " is ready")
83 return True
84 else:
85 log.info("Deployment " + deployment_name + " is not ready")
86 return False
87 except Exception as e:
88 log.error("Exception when waiting for deployment status: %s\n" % e)
Mandeep Khindad6ea9872017-06-24 11:49:37 -040089
90def is_ready(container_name):
Mandeep Khindad6ea9872017-06-24 11:49:37 -040091 ready = False
BorislavG06518262018-02-14 17:40:49 +020092 log.info("Checking if " + container_name + " is ready")
Mandeep Khindad6ea9872017-06-24 11:49:37 -040093 try:
BorislavG06518262018-02-14 17:40:49 +020094 response = coreV1Api.list_namespaced_pod(namespace=namespace, watch=False)
Mandeep Khindad6ea9872017-06-24 11:49:37 -040095 for i in response.items:
BorislavG07dc7db2018-02-14 15:23:51 +020096 # container_statuses can be None, which is non-iterable.
97 if i.status.container_statuses is None:
BorislavG06518262018-02-14 17:40:49 +020098 continue
Mandeep Khindad6ea9872017-06-24 11:49:37 -040099 for s in i.status.container_statuses:
Mahendra Raghuwanshi6a8304c2019-01-28 09:49:30 +0000100 if s.name == container_name:
101 if i.metadata.owner_references[0].kind == "StatefulSet":
102 ready = wait_for_statefulset_complete(i.metadata.owner_references[0].name)
103 elif i.metadata.owner_references[0].kind == "ReplicaSet":
104 api_response = api_instance.read_namespaced_replica_set_status(i.metadata.owner_references[0].name, namespace)
105 ready = wait_for_deployment_complete(api_response.metadata.owner_references[0].name)
106 elif i.metadata.owner_references[0].kind == "Job":
107 ready = is_job_complete(i.metadata.owner_references[0].name)
108
109 return ready
110
Mandeep Khindad6ea9872017-06-24 11:49:37 -0400111 else:
112 continue
113 return ready
114 except Exception as e:
115 log.error("Exception when calling list_namespaced_pod: %s\n" % e)
116
BorislavG06518262018-02-14 17:40:49 +0200117DEF_TIMEOUT = 10
118DESCRIPTION = "Kubernetes container readiness check utility"
119USAGE = "Usage: ready.py [-t <timeout>] -c <container_name> [-c <container_name> ...]\n" \
120 "where\n" \
121 "<timeout> - wait for container readiness timeout in min, default is " + str(DEF_TIMEOUT) + "\n" \
122 "<container_name> - name of the container to wait for\n"
123
124def main(argv):
Mandeep Khindad6ea9872017-06-24 11:49:37 -0400125 # args are a list of container names
BorislavG06518262018-02-14 17:40:49 +0200126 container_names = []
127 timeout = DEF_TIMEOUT
128 try:
129 opts, args = getopt.getopt(argv, "hc:t:", ["container-name=", "timeout=", "help"])
130 for opt, arg in opts:
131 if opt in ("-h", "--help"):
132 print("%s\n\n%s" % (DESCRIPTION, USAGE))
133 sys.exit()
134 elif opt in ("-c", "--container-name"):
135 container_names.append(arg)
136 elif opt in ("-t", "--timeout"):
137 timeout = float(arg)
138 except (getopt.GetoptError, ValueError) as e:
139 print("Error parsing input parameters: %s\n" % e)
140 print(USAGE)
141 sys.exit(2)
142 if container_names.__len__() == 0:
143 print("Missing required input parameter(s)\n")
144 print(USAGE)
145 sys.exit(2)
146
147 for container_name in container_names:
148 timeout = time.time() + timeout * 60
Mandeep Khindad6ea9872017-06-24 11:49:37 -0400149 while True:
150 ready = is_ready(container_name)
151 if ready is True:
152 break
153 elif time.time() > timeout:
BorislavG06518262018-02-14 17:40:49 +0200154 log.warning("timed out waiting for '" + container_name + "' to be ready")
Mandeep Khindad6ea9872017-06-24 11:49:37 -0400155 exit(1)
156 else:
157 time.sleep(5)
158
Mandeep Khindad6ea9872017-06-24 11:49:37 -0400159if __name__ == "__main__":
BorislavG06518262018-02-14 17:40:49 +0200160 main(sys.argv[1:])
Mandeep Khindad6ea9872017-06-24 11:49:37 -0400161