diff options
-rwxr-xr-x | ready.py | 32 |
1 files changed, 25 insertions, 7 deletions
@@ -1,6 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright © 2020 Orange +# Copyright © 2020 Nokia # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -56,7 +57,7 @@ configuration.ssl_ca_cert = cert configuration.api_key['authorization'] = token configuration.api_key_prefix['authorization'] = 'Bearer' coreV1Api = client.CoreV1Api(client.ApiClient(configuration)) -api = client.AppsV1(client.ApiClient(configuration)) +api = client.AppsV1Api(client.ApiClient(configuration)) batchV1Api = client.BatchV1Api(client.ApiClient(configuration)) @@ -247,12 +248,12 @@ def get_deployment_name(replicaset): DEF_TIMEOUT = 10 DESCRIPTION = "Kubernetes container readiness check utility" -USAGE = "Usage: ready.py [-t <timeout>] -c <container_name> " \ - "[-c <container_name> ...]\n" \ +USAGE = "Usage: ready.py [-t <timeout>] -c <container_name> .. | -j <job_name> .. \n" \ "where\n" \ "<timeout> - wait for container readiness timeout in min, " \ "default is " + str(DEF_TIMEOUT) + "\n" \ - "<container_name> - name of the container to wait for\n" + "<container_name> - name of the container to wait for\n" \ + "<job_name> - name of the job to wait for\n" def main(argv): @@ -266,10 +267,12 @@ def main(argv): """ # args are a list of container names container_names = [] + job_names = [] timeout = DEF_TIMEOUT try: - opts, _args = getopt.getopt(argv, "hc:t:", ["container-name=", + opts, _args = getopt.getopt(argv, "hj:c:t:", ["container-name=", "timeout=", + "job-name=", "help"]) for opt, arg in opts: if opt in ("-h", "--help"): @@ -277,13 +280,15 @@ def main(argv): sys.exit() elif opt in ("-c", "--container-name"): container_names.append(arg) + elif opt in ("-j", "--job-name"): + job_names.append(arg) elif opt in ("-t", "--timeout"): timeout = float(arg) except (getopt.GetoptError, ValueError) as exc: print("Error parsing input parameters: {}\n".format(exc)) print(USAGE) sys.exit(2) - if container_names.__len__() == 0: + if container_names.__len__() == 0 and job_names.__len__() == 0: print("Missing required input parameter(s)\n") print(USAGE) sys.exit(2) @@ -302,7 +307,20 @@ def main(argv): # spread in time potentially parallel execution in multiple # containers time.sleep(random.randint(5, 11)) - + for job_name in job_names: + timeout = time.time() + timeout * 60 + while True: + ready = is_job_complete(job_name) + if ready is True: + break + if time.time() > timeout: + log.warning("timed out waiting for '%s' to be ready", + job_name) + sys.exit(1) + else: + # spread in time potentially parallel execution in multiple + # containers + time.sleep(random.randint(5, 11)) if __name__ == "__main__": main(sys.argv[1:]) |