Akansha Dua | 7b6e198 | 2019-09-04 13:36:12 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | import getopt |
| 3 | import logging |
| 4 | import os |
| 5 | import sys |
| 6 | import time |
| 7 | |
| 8 | from kubernetes import config |
| 9 | from kubernetes.client import Configuration |
| 10 | from kubernetes.client.apis import core_v1_api |
| 11 | from kubernetes.client.rest import ApiException |
| 12 | from kubernetes.stream import stream |
| 13 | |
| 14 | from kubernetes import client |
| 15 | |
| 16 | # extract env variables. |
| 17 | namespace = os.environ['NAMESPACE'] |
| 18 | cert = os.environ['CERT'] |
| 19 | host = os.environ['KUBERNETES_SERVICE_HOST'] |
| 20 | token_path = os.environ['TOKEN'] |
| 21 | |
| 22 | with open(token_path, 'r') as token_file: |
| 23 | token = token_file.read().replace('\n', '') |
| 24 | |
| 25 | # setup logging |
| 26 | log = logging.getLogger(__name__) |
| 27 | handler = logging.StreamHandler(sys.stdout) |
| 28 | handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')) |
| 29 | handler.setLevel(logging.INFO) |
| 30 | log.addHandler(handler) |
| 31 | log.setLevel(logging.INFO) |
| 32 | |
| 33 | configuration = client.Configuration() |
| 34 | configuration.host = "https://" + host |
| 35 | configuration.ssl_ca_cert = cert |
| 36 | configuration.api_key['authorization'] = token |
| 37 | configuration.api_key_prefix['authorization'] = 'Bearer' |
| 38 | configuration.assert_hostname = False |
| 39 | coreV1Api = client.CoreV1Api(client.ApiClient(configuration)) |
| 40 | api_instance = client.CoreV1Api(client.ApiClient(configuration)) |
| 41 | |
| 42 | def run_command( pod_name, command ): |
| 43 | try: |
| 44 | exec_command = [ |
| 45 | '/bin/sh', |
| 46 | '-c', |
| 47 | command] |
| 48 | resp = stream(api_instance.connect_get_namespaced_pod_exec, pod_name, namespace, |
| 49 | command=exec_command, |
| 50 | stderr=True, stdin=False, |
| 51 | stdout=True, tty=False) |
| 52 | except ApiException as e: |
| 53 | print("Exception when calling CoreV1Api->connect_get_namespaced_pod_exec: %s\n" % e) |
| 54 | return False |
| 55 | print(resp) |
| 56 | return True |
| 57 | |
| 58 | def find_pod(container_name,command,pods): |
| 59 | ready = False |
| 60 | try: |
| 61 | response = coreV1Api.list_namespaced_pod(namespace=namespace, watch=False) |
| 62 | for i in response.items: |
| 63 | # container_statuses can be None, which is non-iterable. |
| 64 | if i.status.container_statuses is None: |
| 65 | continue |
| 66 | for s in i.status.container_statuses: |
| 67 | if s.name == container_name: |
| 68 | if pods == True: |
| 69 | print (i.metadata.name) |
| 70 | else: |
| 71 | ready = run_command(i.metadata.name,command) |
| 72 | else: |
| 73 | continue |
| 74 | except Exception as e: |
| 75 | log.error("Exception when calling list_namespaced_pod: %s\n" % e) |
| 76 | |
| 77 | return ready |
| 78 | |
| 79 | |
| 80 | DESCRIPTION = "Kubernetes container readiness check utility" |
| 81 | USAGE = "Usage: ready.py [-t <timeout>] -c <container_name> [-c <container_name> ...]\n" \ |
| 82 | "where\n" \ |
| 83 | "<container_name> - name of the container to wait for\n" |
| 84 | |
| 85 | def main(argv): |
| 86 | pods = False |
| 87 | command = "" |
| 88 | container_name = "" |
| 89 | try: |
| 90 | opts, args = getopt.getopt(argv, "ghp:c:", ["pod-container-name=", "command=", "help","getpods"]) |
| 91 | for opt, arg in opts: |
| 92 | if opt in ("-h", "--help"): |
| 93 | print("%s\n\n%s" % (DESCRIPTION, USAGE)) |
| 94 | sys.exit() |
| 95 | elif opt in ("-p", "--pod-container-name"): |
| 96 | container_name = arg |
| 97 | elif opt in ("-c", "--command"): |
| 98 | command = arg |
| 99 | elif opt in ("-g", "--getpods"): |
| 100 | pods = True |
| 101 | except (getopt.GetoptError, ValueError) as e: |
| 102 | print("Error parsing input parameters: %s\n" % e) |
| 103 | print(USAGE) |
| 104 | sys.exit(2) |
| 105 | if container_name.__len__() == 0: |
| 106 | print("Missing required input parameter(s)\n") |
| 107 | print(USAGE) |
| 108 | sys.exit(2) |
| 109 | |
| 110 | if pods == False: |
| 111 | if command.__len__() == 0: |
| 112 | print("Missing required input parameter(s)\n") |
| 113 | print(USAGE) |
| 114 | sys.exit(2) |
| 115 | ready = find_pod(container_name,command,pods) |
| 116 | if ready == False: |
| 117 | sys.exit(2) |
| 118 | |
| 119 | if __name__ == "__main__": |
| 120 | main(sys.argv[1:]) |
| 121 | |
| 122 | |