1 # Copyright © 2018 Amdocs, Bell Canada
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
18 name: {{ include "common.fullname" . }}-check-job-completion
19 namespace: {{ include "common.namespace" . }}
21 vid_check_job_completion.py: |
23 from __future__ import print_function
24 import time, argparse, logging, sys, os
25 import kubernetes.client
26 from kubernetes import client, config
27 from pprint import pprint
29 #extract env variables.
30 namespace = os.environ['NAMESPACE']
31 cert = os.environ['CERT']
32 host = os.environ['KUBERNETES_SERVICE_HOST']
33 token_path = os.environ['TOKEN']
35 with open(token_path, 'r') as token_file:
36 token = token_file.read().replace('\n', '')
38 client.configuration.api_key['authorization'] = token
39 client.configuration.api_key_prefix['authorization'] = 'Bearer'
40 client.configuration.host = "https://" + str(host)
41 client.configuration.ssl_ca_cert = cert
43 api_instance = client.BatchV1Api()
46 log = logging.getLogger(__name__)
47 handler = logging.StreamHandler(sys.stdout)
48 handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
49 handler.setLevel(logging.INFO)
50 log.addHandler(handler)
51 log.setLevel(logging.INFO)
54 def is_ready(job_name):
55 log.info( "[INFO] Checking if " + job_name + " is completed")
60 api_response = api_instance.read_namespaced_job_status(job_name, namespace, pretty=pretty)
61 except Exception as e:
62 print("Exception when calling BatchV1Api->read_namespaced_job_status: %s\n" % e)
65 if api_response.status.succeeded == 1:
66 job_status_type = api_response.status.conditions[0].type
67 if job_status_type == "Complete":
70 print("[DBG] jobStatus: " + unicode(job_status))
76 timeout = time.time() + 60 * 10
78 ready = is_ready(job_name)
81 elif time.time() > timeout:
82 log.warning( "timed out waiting for '" + job_name + "' to be ready")
88 if __name__ == "__main__":
89 parser = argparse.ArgumentParser(description='Process some names.')
90 parser.add_argument('--job-name', action='append', required=True, help='A container name')
91 args = parser.parse_args()
94 for arg in arg_dict.itervalues():