VID Resiliency & Scalability

Change-Id: Ifc593e2e33c3430c74156b4b153263c11127bfa9
Issue-ID: VID-160
Signed-off-by: Michael Lando <ml636r@att.com>
diff --git a/kubernetes/vid/templates/check-job-completion-configmap.yaml b/kubernetes/vid/templates/check-job-completion-configmap.yaml
new file mode 100644
index 0000000..b9c4488
--- /dev/null
+++ b/kubernetes/vid/templates/check-job-completion-configmap.yaml
@@ -0,0 +1,83 @@
+apiVersion: v1
+kind: ConfigMap
+metadata:
+  name: {{ include "common.fullname" . }}-check-job-completion
+  namespace: {{ include "common.namespace" . }}
+data:
+  vid_check_job_completion.py: |
+    #!/usr/bin/python
+    from __future__ import print_function
+    import time, argparse, logging, sys, os
+    import kubernetes.client
+    from kubernetes import client, config
+    from pprint import pprint
+
+    #extract env variables.
+    namespace = os.environ['NAMESPACE']
+    cert = os.environ['CERT']
+    host = os.environ['KUBERNETES_SERVICE_HOST']
+    token_path = os.environ['TOKEN']
+
+    with open(token_path, 'r') as token_file:
+        token = token_file.read().replace('\n', '')
+
+    client.configuration.api_key['authorization'] = token
+    client.configuration.api_key_prefix['authorization'] = 'Bearer'
+    client.configuration.host = "https://" + str(host)
+    client.configuration.ssl_ca_cert = cert
+
+    api_instance = client.BatchV1Api()
+
+    #setup logging
+    log = logging.getLogger(__name__)
+    handler = logging.StreamHandler(sys.stdout)
+    handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
+    handler.setLevel(logging.INFO)
+    log.addHandler(handler)
+    log.setLevel(logging.INFO)
+
+
+    def is_ready(job_name):
+        log.info( "[INFO] Checking if " + job_name + "  is completed")
+        pretty = True
+        job_status = False
+
+        try:
+            api_response = api_instance.read_namespaced_job_status(job_name, namespace, pretty=pretty)
+        except Exception as e:
+          print("Exception when calling BatchV1Api->read_namespaced_job_status: %s\n" % e)
+
+        pprint(api_response)
+        if api_response.status.succeeded == 1:
+          job_status_type = api_response.status.conditions[0].type
+          if job_status_type == "Complete":
+            job_status = True
+
+        print("[DBG] jobStatus: " + unicode(job_status))
+        return job_status
+
+
+    def main(args):
+        for job_name in args:
+            timeout = time.time() + 60 * 10
+            while True:
+                ready = is_ready(job_name)
+                if ready is True :
+                    break
+                elif time.time() > timeout:
+                    log.warning( "timed out waiting for '" + job_name + "' to be ready")
+                    exit(1)
+                else:
+                    time.sleep(5)
+
+
+    if __name__ == "__main__":
+        parser = argparse.ArgumentParser(description='Process some names.')
+        parser.add_argument('--job-name', action='append', required=True, help='A container name')
+        args = parser.parse_args()
+        arg_dict = vars(args)
+
+        for arg in arg_dict.itervalues():
+            main(arg)
+
+