vaibhavjayas | 5ca5465 | 2018-07-31 09:23:16 +0000 | [diff] [blame] | 1 | # Copyright © 2018 Amdocs, Bell Canada |
| 2 | # |
| 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 |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 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. |
| 14 | |
Sonsino, Ofir (os0695) | 2dda01d | 2018-05-30 18:41:14 +0300 | [diff] [blame] | 15 | apiVersion: v1 |
| 16 | kind: ConfigMap |
| 17 | metadata: |
| 18 | name: {{ include "common.fullname" . }}-cluster-ready-configmap |
| 19 | namespace: {{ include "common.namespace" . }} |
| 20 | data: |
| 21 | vid_ready.py : |- |
| 22 | #!/usr/bin/python |
| 23 | from kubernetes import client, config |
| 24 | import time, argparse, logging, sys, os |
| 25 | |
| 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'] |
| 31 | |
| 32 | with open(token_path, 'r') as token_file: |
| 33 | token = token_file.read().replace('\n', '') |
| 34 | |
| 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' |
| 39 | |
| 40 | #setup logging |
| 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) |
| 47 | |
| 48 | |
| 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() |
| 54 | |
| 55 | ready = False |
| 56 | |
| 57 | try: |
| 58 | response = v1.list_namespaced_pod(namespace=namespace, watch=False) |
| 59 | |
| 60 | for i in response.items: |
| 61 | #log.info(i.metadata.name) |
| 62 | for s in i.status.container_statuses: |
| 63 | #log.info(s.name) |
| 64 | if i.metadata.name == container_name: |
| 65 | ready = s.ready |
| 66 | if not ready: |
| 67 | log.info( container_name + " is not ready.") |
| 68 | else: |
| 69 | log.info( container_name + " is ready!") |
| 70 | else: |
| 71 | continue |
| 72 | return ready |
| 73 | except Exception as e: |
| 74 | log.error("Exception when calling list_namespaced_pod: %s\n" % e) |
| 75 | |
| 76 | |
| 77 | def main(args): |
| 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 |
| 82 | while True: |
| 83 | ready = is_ready(container_name) |
| 84 | if ready is True: |
| 85 | break |
| 86 | elif time.time() > timeout: |
| 87 | log.warning( "timed out waiting for '" + container_name + "' to be ready") |
| 88 | exit(1) |
| 89 | else: |
| 90 | time.sleep(5) |
| 91 | |
| 92 | |
| 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() |
| 97 | arg_dict = vars(args) |
| 98 | |
| 99 | for arg in arg_dict.itervalues(): |
| 100 | main(arg) |
| 101 | |
| 102 | |
| 103 | |