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" . }}-cluster-ready-configmap
19 namespace: {{ include "common.namespace" . }}
23 from kubernetes import client, config
24 import time, argparse, logging, sys, os
26 #extract env variables.
27 namespace = os.environ['NAMESPACE']
28 cert = os.environ['CERT']
29 host = os.environ['KUBERNETES_SERVICE_HOST']
30 token_path = os.environ['TOKEN']
32 with open(token_path, 'r') as token_file:
33 token = token_file.read().replace('\n', '')
35 client.configuration.host = "https://" + host
36 client.configuration.ssl_ca_cert = cert
37 client.configuration.api_key['authorization'] = token
38 client.configuration.api_key_prefix['authorization'] = 'Bearer'
41 log = logging.getLogger(__name__)
42 handler = logging.StreamHandler(sys.stdout)
43 handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
44 handler.setLevel(logging.INFO)
45 log.addHandler(handler)
46 log.setLevel(logging.INFO)
49 def is_ready(container_name):
50 log.info( "Checking if " + container_name + " is ready")
51 # config.load_kube_config() # for local testing
52 # namespace='onap-sdc' # for local testing
53 v1 = client.CoreV1Api()
58 response = v1.list_namespaced_pod(namespace=namespace, watch=False)
60 for i in response.items:
61 #log.info(i.metadata.name)
62 for s in i.status.container_statuses:
64 if i.metadata.name == container_name:
67 log.info( container_name + " is not ready.")
69 log.info( container_name + " is ready!")
73 except Exception as e:
74 log.error("Exception when calling list_namespaced_pod: %s\n" % e)
78 # args are a list of container names
79 for container_name in args:
80 # 5 min, TODO: make configurable
81 timeout = time.time() + 60 * 10
83 ready = is_ready(container_name)
86 elif time.time() > timeout:
87 log.warning( "timed out waiting for '" + container_name + "' to be ready")
93 if __name__ == "__main__":
94 parser = argparse.ArgumentParser(description='Process some names.')
95 parser.add_argument('--container-name', action='append', required=True, help='A container name')
96 args = parser.parse_args()
99 for arg in arg_dict.itervalues():