Make job completion function independent from container name 89/111089/16 3.0.1
authorKrzysztof Kuzmicki <krzysztof.kuzmicki@nokia.com>
Mon, 10 Aug 2020 13:56:21 +0000 (15:56 +0200)
committerGrzegorz Lis <grzegorz.lis@nokia.com>
Wed, 19 Aug 2020 08:41:33 +0000 (08:41 +0000)
Issue-ID: OOM-2535

Signed-off-by: Krzysztof Kuzmicki <krzysztof.kuzmicki@nokia.com>
Change-Id: I3dfd071a0a597108d61ef5c86c64fe79780e16a2

ready.py

index 2195a49..3956049 100755 (executable)
--- 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 <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:])