Mandeep Khinda | d6ea987 | 2017-06-24 11:49:37 -0400 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | from kubernetes import client, config |
| 3 | import time, argparse, logging, sys, os |
| 4 | |
| 5 | #extract env variables. |
| 6 | namespace = os.environ['NAMESPACE'] |
| 7 | cert = os.environ['CERT'] |
| 8 | host = os.environ['KUBERNETES_SERVICE_HOST'] |
| 9 | token_path = os.environ['TOKEN'] |
| 10 | |
| 11 | with open(token_path, 'r') as token_file: |
| 12 | token = token_file.read().replace('\n', '') |
| 13 | |
| 14 | client.configuration.host = "https://" + host |
| 15 | client.configuration.ssl_ca_cert = cert |
| 16 | client.configuration.api_key['authorization'] = token |
| 17 | client.configuration.api_key_prefix['authorization'] = 'Bearer' |
| 18 | |
| 19 | #setup logging |
| 20 | log = logging.getLogger(__name__) |
| 21 | handler = logging.StreamHandler(sys.stdout) |
| 22 | handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')) |
| 23 | handler.setLevel(logging.INFO) |
| 24 | log.addHandler(handler) |
| 25 | log.setLevel(logging.INFO) |
| 26 | |
| 27 | |
| 28 | def is_ready(container_name): |
| 29 | log.info( "Checking if " + container_name + " is ready") |
| 30 | # config.load_kube_config() # for local testing |
| 31 | # namespace='onap-sdc' # for local testing |
| 32 | v1 = client.CoreV1Api() |
| 33 | |
| 34 | ready = False |
| 35 | |
| 36 | try: |
| 37 | response = v1.list_namespaced_pod(namespace=namespace, watch=False) |
| 38 | for i in response.items: |
BorislavG | 07dc7db | 2018-02-14 15:23:51 +0200 | [diff] [blame] | 39 | # container_statuses can be None, which is non-iterable. |
| 40 | if i.status.container_statuses is None: |
| 41 | continue |
Mandeep Khinda | d6ea987 | 2017-06-24 11:49:37 -0400 | [diff] [blame] | 42 | for s in i.status.container_statuses: |
| 43 | if s.name == container_name: |
| 44 | ready = s.ready |
| 45 | if not ready: |
| 46 | log.info( container_name + " is not ready.") |
| 47 | else: |
| 48 | log.info( container_name + " is ready!") |
| 49 | else: |
| 50 | continue |
| 51 | return ready |
| 52 | except Exception as e: |
| 53 | log.error("Exception when calling list_namespaced_pod: %s\n" % e) |
| 54 | |
| 55 | |
| 56 | def main(args): |
| 57 | # args are a list of container names |
| 58 | for container_name in args: |
| 59 | # 5 min, TODO: make configurable |
| 60 | timeout = time.time() + 60 * 10 |
| 61 | while True: |
| 62 | ready = is_ready(container_name) |
| 63 | if ready is True: |
| 64 | break |
| 65 | elif time.time() > timeout: |
| 66 | log.warning( "timed out waiting for '" + container_name + "' to be ready") |
| 67 | exit(1) |
| 68 | else: |
| 69 | time.sleep(5) |
| 70 | |
| 71 | |
| 72 | if __name__ == "__main__": |
| 73 | parser = argparse.ArgumentParser(description='Process some names.') |
| 74 | parser.add_argument('--container-name', action='append', required=True, help='A container name') |
| 75 | args = parser.parse_args() |
| 76 | arg_dict = vars(args) |
| 77 | |
| 78 | for arg in arg_dict.itervalues(): |
| 79 | main(arg) |