8 from kubernetes import client
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']
16 with open(token_path, 'r') as token_file:
17 token = token_file.read().replace('\n', '')
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)
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 batchV1Api = client.BatchV1Api(client.ApiClient(configuration))
35 def is_job_complete(job_name):
37 log.info("Checking if " + job_name + " is complete")
40 response = batchV1Api.read_namespaced_job_status(job_name, namespace)
41 if response.status.succeeded == 1:
42 job_status_type = response.status.conditions[0].type
43 if job_status_type == "Complete":
46 log.info(job_name + " is not complete")
48 log.info(job_name + " has not succeeded yet")
50 except Exception as e:
51 log.error("Exception when calling read_namespaced_job_status: %s\n" % e)
55 DESCRIPTION = "Kubernetes container job complete check utility"
56 USAGE = "Usage: job_complete.py [-t <timeout>] -j <job_name> [-j <job_name> ...]\n" \
58 "<timeout> - wait for container job complete timeout in min, default is " + str(DEF_TIMEOUT) + "\n" \
59 "<job_name> - name of the job to wait for\n"
62 # args are a list of job names
66 opts, args = getopt.getopt(argv, "hj:t:", ["job-name=", "timeout=", "help"])
68 if opt in ("-h", "--help"):
69 print("%s\n\n%s" % (DESCRIPTION, USAGE))
71 elif opt in ("-j", "--job-name"):
73 elif opt in ("-t", "--timeout"):
75 except (getopt.GetoptError, ValueError) as e:
76 print("Error parsing input parameters: %s\n" % e)
79 if job_names.__len__() == 0:
80 print("Missing required input parameter(s)\n")
84 for job_name in job_names:
85 timeout = time.time() + timeout * 60
87 complete = is_job_complete(job_name)
90 elif time.time() > timeout:
91 log.warning("timed out waiting for '" + job_name + "' to be completed")
96 if __name__ == "__main__":