From e342cedaf3a4bdc47fe968a078e3cb6ee27cd0fa Mon Sep 17 00:00:00 2001 From: Krzysztof Kuzmicki Date: Mon, 10 Aug 2020 15:56:21 +0200 Subject: Make job completion function independent from container name Issue-ID: OOM-2535 Signed-off-by: Krzysztof Kuzmicki Change-Id: I3dfd071a0a597108d61ef5c86c64fe79780e16a2 --- ready.py | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/ready.py b/ready.py index 2195a49..3956049 100755 --- a/ready.py +++ b/ready.py @@ -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 ] -c " \ - "[-c ...]\n" \ +USAGE = "Usage: ready.py [-t ] -c .. | -j .. \n" \ "where\n" \ " - wait for container readiness timeout in min, " \ "default is " + str(DEF_TIMEOUT) + "\n" \ - " - name of the container to wait for\n" + " - name of the container to wait for\n" \ + " - 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:]) -- cgit 1.2.3-korg