Dmitry Puzikov | d0101df | 2019-04-25 14:33:48 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
BorislavG | 0651826 | 2018-02-14 17:40:49 +0200 | [diff] [blame] | 2 | import getopt |
| 3 | import logging |
| 4 | import os |
| 5 | import sys |
| 6 | import time |
Dmitry Puzikov | 0c588d5 | 2019-04-25 14:53:03 +0200 | [diff] [blame] | 7 | import random |
Mandeep Khinda | d6ea987 | 2017-06-24 11:49:37 -0400 | [diff] [blame] | 8 | |
BorislavG | 0651826 | 2018-02-14 17:40:49 +0200 | [diff] [blame] | 9 | from kubernetes import client |
| 10 | |
| 11 | # extract env variables. |
Mandeep Khinda | d6ea987 | 2017-06-24 11:49:37 -0400 | [diff] [blame] | 12 | namespace = os.environ['NAMESPACE'] |
| 13 | cert = os.environ['CERT'] |
| 14 | host = os.environ['KUBERNETES_SERVICE_HOST'] |
| 15 | token_path = os.environ['TOKEN'] |
| 16 | |
| 17 | with open(token_path, 'r') as token_file: |
| 18 | token = token_file.read().replace('\n', '') |
| 19 | |
BorislavG | 0651826 | 2018-02-14 17:40:49 +0200 | [diff] [blame] | 20 | # setup logging |
Mandeep Khinda | d6ea987 | 2017-06-24 11:49:37 -0400 | [diff] [blame] | 21 | log = logging.getLogger(__name__) |
| 22 | handler = logging.StreamHandler(sys.stdout) |
k.kedron | 19664b3 | 2019-05-14 16:13:00 +0200 | [diff] [blame] | 23 | formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') |
| 24 | handler.setFormatter(formatter) |
Mandeep Khinda | d6ea987 | 2017-06-24 11:49:37 -0400 | [diff] [blame] | 25 | handler.setLevel(logging.INFO) |
| 26 | log.addHandler(handler) |
| 27 | log.setLevel(logging.INFO) |
| 28 | |
BorislavG | 0651826 | 2018-02-14 17:40:49 +0200 | [diff] [blame] | 29 | configuration = client.Configuration() |
| 30 | configuration.host = "https://" + host |
| 31 | configuration.ssl_ca_cert = cert |
| 32 | configuration.api_key['authorization'] = token |
| 33 | configuration.api_key_prefix['authorization'] = 'Bearer' |
| 34 | coreV1Api = client.CoreV1Api(client.ApiClient(configuration)) |
k.kedron | 19664b3 | 2019-05-14 16:13:00 +0200 | [diff] [blame] | 35 | api_instance = client.ExtensionsV1beta1Api(client.ApiClient(configuration)) |
Mahendra Raghuwanshi | 6a8304c | 2019-01-28 09:49:30 +0000 | [diff] [blame] | 36 | api = client.AppsV1beta1Api(client.ApiClient(configuration)) |
| 37 | batchV1Api = client.BatchV1Api(client.ApiClient(configuration)) |
| 38 | |
k.kedron | 19664b3 | 2019-05-14 16:13:00 +0200 | [diff] [blame] | 39 | |
Mahendra Raghuwanshi | 6a8304c | 2019-01-28 09:49:30 +0000 | [diff] [blame] | 40 | def is_job_complete(job_name): |
| 41 | complete = False |
| 42 | log.info("Checking if " + job_name + " is complete") |
Mahendra Raghuwanshi | 6a8304c | 2019-01-28 09:49:30 +0000 | [diff] [blame] | 43 | try: |
| 44 | response = batchV1Api.read_namespaced_job_status(job_name, namespace) |
| 45 | if response.status.succeeded == 1: |
| 46 | job_status_type = response.status.conditions[0].type |
| 47 | if job_status_type == "Complete": |
| 48 | complete = True |
| 49 | log.info(job_name + " is complete") |
| 50 | else: |
| 51 | log.info(job_name + " is not complete") |
| 52 | else: |
| 53 | log.info(job_name + " has not succeeded yet") |
| 54 | return complete |
| 55 | except Exception as e: |
| 56 | log.error("Exception when calling read_namespaced_job_status: %s\n" % e) |
| 57 | |
k.kedron | 19664b3 | 2019-05-14 16:13:00 +0200 | [diff] [blame] | 58 | |
Mahendra Raghuwanshi | 6a8304c | 2019-01-28 09:49:30 +0000 | [diff] [blame] | 59 | def wait_for_statefulset_complete(statefulset_name): |
| 60 | try: |
| 61 | response = api.read_namespaced_stateful_set(statefulset_name, namespace) |
| 62 | s = response.status |
Sebastien Premont-Tendland | c1e95ee | 2020-05-26 18:32:33 -0400 | [diff] [blame^] | 63 | if (s.replicas == response.spec.replicas and |
Mahendra Raghuwanshi | 6a8304c | 2019-01-28 09:49:30 +0000 | [diff] [blame] | 64 | s.ready_replicas == response.spec.replicas and |
Mahendra Raghuwanshi | 6a8304c | 2019-01-28 09:49:30 +0000 | [diff] [blame] | 65 | s.observed_generation == response.metadata.generation): |
| 66 | log.info("Statefulset " + statefulset_name + " is ready") |
| 67 | return True |
| 68 | else: |
| 69 | log.info("Statefulset " + statefulset_name + " is not ready") |
| 70 | return False |
| 71 | except Exception as e: |
| 72 | log.error("Exception when waiting for Statefulset status: %s\n" % e) |
| 73 | |
k.kedron | 19664b3 | 2019-05-14 16:13:00 +0200 | [diff] [blame] | 74 | |
Mahendra Raghuwanshi | 6a8304c | 2019-01-28 09:49:30 +0000 | [diff] [blame] | 75 | def wait_for_deployment_complete(deployment_name): |
| 76 | try: |
| 77 | response = api.read_namespaced_deployment(deployment_name, namespace) |
| 78 | s = response.status |
k.kedron | 19664b3 | 2019-05-14 16:13:00 +0200 | [diff] [blame] | 79 | if (s.unavailable_replicas is None and |
Akansha Dua | b77da7a | 2019-10-03 17:15:21 +0530 | [diff] [blame] | 80 | ( s.updated_replicas is None or s.updated_replicas == response.spec.replicas ) and |
Mahendra Raghuwanshi | 6a8304c | 2019-01-28 09:49:30 +0000 | [diff] [blame] | 81 | s.replicas == response.spec.replicas and |
| 82 | s.ready_replicas == response.spec.replicas and |
| 83 | s.observed_generation == response.metadata.generation): |
| 84 | log.info("Deployment " + deployment_name + " is ready") |
| 85 | return True |
| 86 | else: |
| 87 | log.info("Deployment " + deployment_name + " is not ready") |
| 88 | return False |
| 89 | except Exception as e: |
| 90 | log.error("Exception when waiting for deployment status: %s\n" % e) |
Mandeep Khinda | d6ea987 | 2017-06-24 11:49:37 -0400 | [diff] [blame] | 91 | |
k.kedron | 19664b3 | 2019-05-14 16:13:00 +0200 | [diff] [blame] | 92 | |
dfx1971 | 5d3e392 | 2020-01-16 09:55:27 +0200 | [diff] [blame] | 93 | def wait_for_daemonset_complete(daemonset_name): |
| 94 | try: |
| 95 | response = api_instance.read_namespaced_daemon_set(daemonset_name, namespace) |
| 96 | s = response.status |
| 97 | if s.desired_number_scheduled == s.number_ready: |
| 98 | log.info("DaemonSet: " + str(s.number_ready) + "/" + str(s.desired_number_scheduled) + " nodes ready --> " + daemonset_name + " is ready") |
| 99 | return True |
| 100 | else: |
| 101 | log.info("DaemonSet: " + str(s.number_ready) + "/" + str(s.desired_number_scheduled) + " nodes ready --> " + daemonset_name + " is not ready") |
| 102 | return False |
| 103 | except Exception as e: |
| 104 | log.error("Exception when waiting for DaemonSet status: %s\n" % e) |
| 105 | |
| 106 | |
Mandeep Khinda | d6ea987 | 2017-06-24 11:49:37 -0400 | [diff] [blame] | 107 | def is_ready(container_name): |
Mandeep Khinda | d6ea987 | 2017-06-24 11:49:37 -0400 | [diff] [blame] | 108 | ready = False |
BorislavG | 0651826 | 2018-02-14 17:40:49 +0200 | [diff] [blame] | 109 | log.info("Checking if " + container_name + " is ready") |
Mandeep Khinda | d6ea987 | 2017-06-24 11:49:37 -0400 | [diff] [blame] | 110 | try: |
k.kedron | 19664b3 | 2019-05-14 16:13:00 +0200 | [diff] [blame] | 111 | response = coreV1Api.list_namespaced_pod(namespace=namespace, |
| 112 | watch=False) |
Mandeep Khinda | d6ea987 | 2017-06-24 11:49:37 -0400 | [diff] [blame] | 113 | for i in response.items: |
BorislavG | 07dc7db | 2018-02-14 15:23:51 +0200 | [diff] [blame] | 114 | # container_statuses can be None, which is non-iterable. |
| 115 | if i.status.container_statuses is None: |
BorislavG | 0651826 | 2018-02-14 17:40:49 +0200 | [diff] [blame] | 116 | continue |
Mandeep Khinda | d6ea987 | 2017-06-24 11:49:37 -0400 | [diff] [blame] | 117 | for s in i.status.container_statuses: |
Mahendra Raghuwanshi | 6a8304c | 2019-01-28 09:49:30 +0000 | [diff] [blame] | 118 | if s.name == container_name: |
k.kedron | 19664b3 | 2019-05-14 16:13:00 +0200 | [diff] [blame] | 119 | name = read_name(i) |
| 120 | if i.metadata.owner_references[0].kind == "StatefulSet": |
| 121 | ready = wait_for_statefulset_complete(name) |
Mahendra Raghuwanshi | 6a8304c | 2019-01-28 09:49:30 +0000 | [diff] [blame] | 122 | elif i.metadata.owner_references[0].kind == "ReplicaSet": |
k.kedron | 19664b3 | 2019-05-14 16:13:00 +0200 | [diff] [blame] | 123 | deployment_name = get_deployment_name(name) |
| 124 | ready = wait_for_deployment_complete(deployment_name) |
Mahendra Raghuwanshi | 6a8304c | 2019-01-28 09:49:30 +0000 | [diff] [blame] | 125 | elif i.metadata.owner_references[0].kind == "Job": |
k.kedron | 19664b3 | 2019-05-14 16:13:00 +0200 | [diff] [blame] | 126 | ready = is_job_complete(name) |
dfx1971 | 5d3e392 | 2020-01-16 09:55:27 +0200 | [diff] [blame] | 127 | elif i.metadata.owner_references[0].kind == "DaemonSet": |
| 128 | ready = wait_for_daemonset_complete(i.metadata.owner_references[0].name) |
Mahendra Raghuwanshi | 6a8304c | 2019-01-28 09:49:30 +0000 | [diff] [blame] | 129 | |
| 130 | return ready |
| 131 | |
Mandeep Khinda | d6ea987 | 2017-06-24 11:49:37 -0400 | [diff] [blame] | 132 | else: |
| 133 | continue |
| 134 | return ready |
| 135 | except Exception as e: |
| 136 | log.error("Exception when calling list_namespaced_pod: %s\n" % e) |
| 137 | |
k.kedron | 19664b3 | 2019-05-14 16:13:00 +0200 | [diff] [blame] | 138 | |
| 139 | def read_name(item): |
dfx1971 | 5d3e392 | 2020-01-16 09:55:27 +0200 | [diff] [blame] | 140 | return item.metadata.owner_references[0].name |
k.kedron | 19664b3 | 2019-05-14 16:13:00 +0200 | [diff] [blame] | 141 | |
| 142 | |
| 143 | def get_deployment_name(replicaset): |
| 144 | api_response = api_instance.read_namespaced_replica_set_status(replicaset, |
| 145 | namespace) |
| 146 | deployment_name = read_name(api_response) |
| 147 | return deployment_name |
| 148 | |
| 149 | |
BorislavG | 0651826 | 2018-02-14 17:40:49 +0200 | [diff] [blame] | 150 | DEF_TIMEOUT = 10 |
| 151 | DESCRIPTION = "Kubernetes container readiness check utility" |
k.kedron | 19664b3 | 2019-05-14 16:13:00 +0200 | [diff] [blame] | 152 | USAGE = "Usage: ready.py [-t <timeout>] -c <container_name> " \ |
| 153 | "[-c <container_name> ...]\n" \ |
BorislavG | 0651826 | 2018-02-14 17:40:49 +0200 | [diff] [blame] | 154 | "where\n" \ |
k.kedron | 19664b3 | 2019-05-14 16:13:00 +0200 | [diff] [blame] | 155 | "<timeout> - wait for container readiness timeout in min, " \ |
| 156 | "default is " + str(DEF_TIMEOUT) + "\n" \ |
BorislavG | 0651826 | 2018-02-14 17:40:49 +0200 | [diff] [blame] | 157 | "<container_name> - name of the container to wait for\n" |
| 158 | |
k.kedron | 19664b3 | 2019-05-14 16:13:00 +0200 | [diff] [blame] | 159 | |
BorislavG | 0651826 | 2018-02-14 17:40:49 +0200 | [diff] [blame] | 160 | def main(argv): |
Mandeep Khinda | d6ea987 | 2017-06-24 11:49:37 -0400 | [diff] [blame] | 161 | # args are a list of container names |
BorislavG | 0651826 | 2018-02-14 17:40:49 +0200 | [diff] [blame] | 162 | container_names = [] |
| 163 | timeout = DEF_TIMEOUT |
| 164 | try: |
k.kedron | 19664b3 | 2019-05-14 16:13:00 +0200 | [diff] [blame] | 165 | opts, args = getopt.getopt(argv, "hc:t:", ["container-name=", |
| 166 | "timeout=", |
| 167 | "help"]) |
BorislavG | 0651826 | 2018-02-14 17:40:49 +0200 | [diff] [blame] | 168 | for opt, arg in opts: |
| 169 | if opt in ("-h", "--help"): |
| 170 | print("%s\n\n%s" % (DESCRIPTION, USAGE)) |
| 171 | sys.exit() |
| 172 | elif opt in ("-c", "--container-name"): |
| 173 | container_names.append(arg) |
| 174 | elif opt in ("-t", "--timeout"): |
| 175 | timeout = float(arg) |
| 176 | except (getopt.GetoptError, ValueError) as e: |
| 177 | print("Error parsing input parameters: %s\n" % e) |
| 178 | print(USAGE) |
| 179 | sys.exit(2) |
| 180 | if container_names.__len__() == 0: |
| 181 | print("Missing required input parameter(s)\n") |
| 182 | print(USAGE) |
| 183 | sys.exit(2) |
| 184 | |
| 185 | for container_name in container_names: |
| 186 | timeout = time.time() + timeout * 60 |
Mandeep Khinda | d6ea987 | 2017-06-24 11:49:37 -0400 | [diff] [blame] | 187 | while True: |
| 188 | ready = is_ready(container_name) |
| 189 | if ready is True: |
| 190 | break |
| 191 | elif time.time() > timeout: |
k.kedron | 19664b3 | 2019-05-14 16:13:00 +0200 | [diff] [blame] | 192 | log.warning("timed out waiting for '" + container_name + |
| 193 | "' to be ready") |
Mandeep Khinda | d6ea987 | 2017-06-24 11:49:37 -0400 | [diff] [blame] | 194 | exit(1) |
| 195 | else: |
k.kedron | 19664b3 | 2019-05-14 16:13:00 +0200 | [diff] [blame] | 196 | # spread in time potentially parallel execution in multiple |
| 197 | # containers |
Dmitry Puzikov | 0c588d5 | 2019-04-25 14:53:03 +0200 | [diff] [blame] | 198 | time.sleep(random.randint(5, 11)) |
Mandeep Khinda | d6ea987 | 2017-06-24 11:49:37 -0400 | [diff] [blame] | 199 | |
k.kedron | 19664b3 | 2019-05-14 16:13:00 +0200 | [diff] [blame] | 200 | |
Mandeep Khinda | d6ea987 | 2017-06-24 11:49:37 -0400 | [diff] [blame] | 201 | if __name__ == "__main__": |
BorislavG | 0651826 | 2018-02-14 17:40:49 +0200 | [diff] [blame] | 202 | main(sys.argv[1:]) |
Mandeep Khinda | d6ea987 | 2017-06-24 11:49:37 -0400 | [diff] [blame] | 203 | |