2 #from kubernetes import client, config
4 import time, argparse, logging, sys, os, base64
8 log = logging.getLogger(__name__)
9 handler = logging.StreamHandler(sys.stdout)
10 handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
11 handler.setLevel(logging.DEBUG)
12 log.addHandler(handler)
13 log.setLevel(logging.DEBUG)
16 def is_ready(container_name):
17 log.info( "Checking if " + container_name + " is ready")
19 kubernetes.config.kube_config.KubeConfigLoader(config_dict=get_k8s_config_env()).load_and_set()
20 client = kubernetes.client
21 namespace = get_namespace_env()
22 v1 = client.CoreV1Api()
27 response = v1.list_namespaced_pod(namespace=namespace, watch=False)
28 for i in response.items:
29 for s in i.status.container_statuses:
30 if s.name == container_name:
31 log.debug ( "response %s" % response )
34 log.info( container_name + " is not ready.")
36 log.info( container_name + " is ready!")
40 except Exception as e:
41 log.error("Exception when calling list_namespaced_pod: %s\n" % e)
44 def get_k8s_config_env():
46 k8s_config_env = os.environ.get("K8S_CONFIG_B64")
47 decoded = base64.b64decode(k8s_config_env)
48 return yaml.load(decoded)
49 except KeyError as ke:
50 raise Exception("K8S_CONFIG_B64 variable is not set.")
53 def get_namespace_env():
55 namespace_env = os.environ.get("NAMESPACE")
57 except KeyError as ke:
58 raise Exception("NAMESPACE variable is not set.")
61 def main(args):#from kubernetes import client, config
63 # args are a list of container names
64 for container_name in args:
65 # 5 min, TODO: make configurable
66 timeout = time.time() + 60 * 10
68 ready = is_ready(container_name)
71 elif time.time() > timeout:
72 log.warning( "timed out waiting for '" + container_name + "' to be ready")
78 if __name__ == "__main__":
79 parser = argparse.ArgumentParser(description='Process some names.')
80 parser.add_argument('--container-name', action='append', required=True, help='A container name')
81 args = parser.parse_args()
84 for arg in arg_dict.itervalues():