4 name: {{ include "common.fullname" . }}-check-job-completion
5 namespace: {{ include "common.namespace" . }}
7 vid_check_job_completion.py: |
9 from __future__ import print_function
10 import time, argparse, logging, sys, os
11 import kubernetes.client
12 from kubernetes import client, config
13 from pprint import pprint
15 #extract env variables.
16 namespace = os.environ['NAMESPACE']
17 cert = os.environ['CERT']
18 host = os.environ['KUBERNETES_SERVICE_HOST']
19 token_path = os.environ['TOKEN']
21 with open(token_path, 'r') as token_file:
22 token = token_file.read().replace('\n', '')
24 client.configuration.api_key['authorization'] = token
25 client.configuration.api_key_prefix['authorization'] = 'Bearer'
26 client.configuration.host = "https://" + str(host)
27 client.configuration.ssl_ca_cert = cert
29 api_instance = client.BatchV1Api()
32 log = logging.getLogger(__name__)
33 handler = logging.StreamHandler(sys.stdout)
34 handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
35 handler.setLevel(logging.INFO)
36 log.addHandler(handler)
37 log.setLevel(logging.INFO)
40 def is_ready(job_name):
41 log.info( "[INFO] Checking if " + job_name + " is completed")
46 api_response = api_instance.read_namespaced_job_status(job_name, namespace, pretty=pretty)
47 except Exception as e:
48 print("Exception when calling BatchV1Api->read_namespaced_job_status: %s\n" % e)
51 if api_response.status.succeeded == 1:
52 job_status_type = api_response.status.conditions[0].type
53 if job_status_type == "Complete":
56 print("[DBG] jobStatus: " + unicode(job_status))
62 timeout = time.time() + 60 * 10
64 ready = is_ready(job_name)
67 elif time.time() > timeout:
68 log.warning( "timed out waiting for '" + job_name + "' to be ready")
74 if __name__ == "__main__":
75 parser = argparse.ArgumentParser(description='Process some names.')
76 parser.add_argument('--job-name', action='append', required=True, help='A container name')
77 args = parser.parse_args()
80 for arg in arg_dict.itervalues():