blob: 2b3180aa2ed862afd2cddd2a95a4967b5c0c28e7 [file] [log] [blame]
Dmitry Puzikovd0101df2019-04-25 14:33:48 +02001#!/usr/bin/env python
Mandeep Khinda67edf6d2018-03-26 23:30:25 +00002import getopt
3import logging
4import os
5import sys
6import time
7
8from kubernetes import client
9
10# extract env variables.
11namespace = os.environ['NAMESPACE']
12cert = os.environ['CERT']
13host = os.environ['KUBERNETES_SERVICE_HOST']
14token_path = os.environ['TOKEN']
15
16with open(token_path, 'r') as token_file:
17 token = token_file.read().replace('\n', '')
18
19# setup logging
20log = logging.getLogger(__name__)
21handler = logging.StreamHandler(sys.stdout)
22handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
23handler.setLevel(logging.INFO)
24log.addHandler(handler)
25log.setLevel(logging.INFO)
26
27configuration = client.Configuration()
28configuration.host = "https://" + host
29configuration.ssl_ca_cert = cert
30configuration.api_key['authorization'] = token
31configuration.api_key_prefix['authorization'] = 'Bearer'
32batchV1Api = client.BatchV1Api(client.ApiClient(configuration))
33
34
35def is_job_complete(job_name):
36 complete = False
37 log.info("Checking if " + job_name + " is complete")
38 response = ""
39 try:
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":
44 complete = True
45 else:
46 log.info(job_name + " is not complete")
47 else:
48 log.info(job_name + " has not succeeded yet")
49 return complete
50 except Exception as e:
51 log.error("Exception when calling read_namespaced_job_status: %s\n" % e)
52
53
54DEF_TIMEOUT = 10
55DESCRIPTION = "Kubernetes container job complete check utility"
56USAGE = "Usage: job_complete.py [-t <timeout>] -j <job_name> [-j <job_name> ...]\n" \
57 "where\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"
60
61def main(argv):
62 # args are a list of job names
63 job_names = []
64 timeout = DEF_TIMEOUT
65 try:
66 opts, args = getopt.getopt(argv, "hj:t:", ["job-name=", "timeout=", "help"])
67 for opt, arg in opts:
68 if opt in ("-h", "--help"):
69 print("%s\n\n%s" % (DESCRIPTION, USAGE))
70 sys.exit()
71 elif opt in ("-j", "--job-name"):
72 job_names.append(arg)
73 elif opt in ("-t", "--timeout"):
74 timeout = float(arg)
75 except (getopt.GetoptError, ValueError) as e:
76 print("Error parsing input parameters: %s\n" % e)
77 print(USAGE)
78 sys.exit(2)
79 if job_names.__len__() == 0:
80 print("Missing required input parameter(s)\n")
81 print(USAGE)
82 sys.exit(2)
83
84 for job_name in job_names:
85 timeout = time.time() + timeout * 60
86 while True:
87 complete = is_job_complete(job_name)
88 if complete is True:
89 break
90 elif time.time() > timeout:
91 log.warning("timed out waiting for '" + job_name + "' to be completed")
92 exit(1)
93 else:
94 time.sleep(5)
95
96if __name__ == "__main__":
97 main(sys.argv[1:])