Sonsino, Ofir (os0695) | 2dda01d | 2018-05-30 18:41:14 +0300 | [diff] [blame] | 1 | apiVersion: v1 |
| 2 | kind: ConfigMap |
| 3 | metadata: |
| 4 | name: {{ include "common.fullname" . }}-check-job-completion |
| 5 | namespace: {{ include "common.namespace" . }} |
| 6 | data: |
| 7 | vid_check_job_completion.py: | |
| 8 | #!/usr/bin/python |
| 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 |
| 14 | |
| 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'] |
| 20 | |
| 21 | with open(token_path, 'r') as token_file: |
| 22 | token = token_file.read().replace('\n', '') |
| 23 | |
| 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 |
| 28 | |
| 29 | api_instance = client.BatchV1Api() |
| 30 | |
| 31 | #setup logging |
| 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) |
| 38 | |
| 39 | |
| 40 | def is_ready(job_name): |
| 41 | log.info( "[INFO] Checking if " + job_name + " is completed") |
| 42 | pretty = True |
| 43 | job_status = False |
| 44 | |
| 45 | try: |
| 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) |
| 49 | |
| 50 | pprint(api_response) |
| 51 | if api_response.status.succeeded == 1: |
| 52 | job_status_type = api_response.status.conditions[0].type |
| 53 | if job_status_type == "Complete": |
| 54 | job_status = True |
| 55 | |
| 56 | print("[DBG] jobStatus: " + unicode(job_status)) |
| 57 | return job_status |
| 58 | |
| 59 | |
| 60 | def main(args): |
| 61 | for job_name in args: |
| 62 | timeout = time.time() + 60 * 10 |
| 63 | while True: |
| 64 | ready = is_ready(job_name) |
| 65 | if ready is True : |
| 66 | break |
| 67 | elif time.time() > timeout: |
| 68 | log.warning( "timed out waiting for '" + job_name + "' to be ready") |
| 69 | exit(1) |
| 70 | else: |
| 71 | time.sleep(5) |
| 72 | |
| 73 | |
| 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() |
| 78 | arg_dict = vars(args) |
| 79 | |
| 80 | for arg in arg_dict.itervalues(): |
| 81 | main(arg) |
| 82 | |
| 83 | |