Merge "Readiness check for the deployment/statefulsets"
[oom.git] / kubernetes / readiness / docker / init / ready.py
1 #!/usr/bin/python
2 import getopt
3 import logging
4 import os
5 import sys
6 import time
7
8 from kubernetes import client
9
10 # extract env variables.
11 namespace = os.environ['NAMESPACE']
12 cert = os.environ['CERT']
13 host = os.environ['KUBERNETES_SERVICE_HOST']
14 token_path = os.environ['TOKEN']
15
16 with open(token_path, 'r') as token_file:
17     token = token_file.read().replace('\n', '')
18
19 # setup logging
20 log = logging.getLogger(__name__)
21 handler = logging.StreamHandler(sys.stdout)
22 handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
23 handler.setLevel(logging.INFO)
24 log.addHandler(handler)
25 log.setLevel(logging.INFO)
26
27 configuration = client.Configuration()
28 configuration.host = "https://" + host
29 configuration.ssl_ca_cert = cert
30 configuration.api_key['authorization'] = token
31 configuration.api_key_prefix['authorization'] = 'Bearer'
32 coreV1Api = client.CoreV1Api(client.ApiClient(configuration))
33 api_instance=client.ExtensionsV1beta1Api(client.ApiClient(configuration))
34 api = client.AppsV1beta1Api(client.ApiClient(configuration))
35 batchV1Api = client.BatchV1Api(client.ApiClient(configuration))
36
37 def 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
56 def 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
73 def 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)
89
90 def is_ready(container_name):
91     ready = False
92     log.info("Checking if " + container_name + "  is ready")
93     try:
94         response = coreV1Api.list_namespaced_pod(namespace=namespace, watch=False)
95         for i in response.items:
96             # container_statuses can be None, which is non-iterable.
97             if i.status.container_statuses is None:
98                 continue
99             for s in i.status.container_statuses:
100                 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
111                 else:
112                     continue
113         return ready
114     except Exception as e:
115         log.error("Exception when calling list_namespaced_pod: %s\n" % e)
116
117 DEF_TIMEOUT = 10
118 DESCRIPTION = "Kubernetes container readiness check utility"
119 USAGE = "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
124 def main(argv):
125     # args are a list of container names
126     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
149         while True:
150             ready = is_ready(container_name)
151             if ready is True:
152                 break
153             elif time.time() > timeout:
154                 log.warning("timed out waiting for '" + container_name + "' to be ready")
155                 exit(1)
156             else:
157                 time.sleep(5)
158
159 if __name__ == "__main__":
160     main(sys.argv[1:])
161