def port_forward(component_port_pairs=[], deployment_target=None, wait=True, open_browser=False, use_kubectl_proxy=False): """Executes kubectl command to set up port forwarding between localhost and the given pod. While this is running, connecting to localhost:<port> will be the same as connecting to that port from the pod's internal network. Args: component_port_pairs (list): 2-tuple(s) containing keyword to use for looking up a kubernetes pod, along with the port to forward to that pod (eg. ('phenotips', 8080)) deployment_target (string): value from DEPLOYMENT_TARGETS - eg. "gcloud-dev" wait (bool): Whether to block indefinitely as long as the forwarding process is running. open_browser (bool): If component_port_pairs includes components that have an http server (eg. "seqr" or "phenotips"), then open a web browser window to the forwarded port. use_kubectl_proxy (bool): Whether to use kubectl proxy instead of kubectl port-forward (see https://kubernetes.io/docs/tasks/access-application-cluster/access-cluster/#manually-constructing-apiserver-proxy-urls) Returns: (list): Popen process objects for the kubectl port-forward processes. """ procs = [] for component_label, port in component_port_pairs: if component_label == "kube-scan": continue # See https://github.com/octarinesec/kube-scan for how to connect to the kube-scan pod. wait_until_pod_is_running(component_label, deployment_target) logger.info("Forwarding port %s for %s" % (port, component_label)) pod_name = get_pod_name(component_label, deployment_target=deployment_target) if use_kubectl_proxy: command = "kubectl proxy --port 8001" else: command = "kubectl port-forward %(pod_name)s %(port)s" % locals() p = run_in_background(command) if open_browser and component_label in COMPONENTS_TO_OPEN_IN_BROWSER: if use_kubectl_proxy: url = "http://localhost:8001/api/v1/namespaces/default/services/%(component_label)s:%(port)s/proxy/" % locals( ) else: url = "http://localhost:%s" % port time.sleep(3) os.system("open " + url) procs.append(p) if wait: wait_for(procs) return procs
def print_log(components, deployment_target, enable_stream_log, previous=False, wait=True): """Executes kubernetes command to print logs for the given pod. Args: components (list): one or more kubernetes pod labels (eg. 'postgres' or 'nginx'). If more than one is specified, logs will be printed from all components in parallel. deployment_target (string): value from DEPLOYMENT_TARGETS - eg. "gcloud-dev", etc. enable_stream_log (bool): whether to continuously stream the log instead of just printing the log up to now. previous (bool): Prints logs from a previous instance of the container. This is useful for debugging pods that don't start or immediately enter crash-loop. wait (bool): If False, this method will return without waiting for the log streaming process to finish printing all logs. Returns: (list): Popen process objects for the kubectl port-forward processes. """ stream_arg = "-f" if enable_stream_log else "" previous_flag = "--previous" if previous else "" procs = [] for component_label in components: if component_label == "kube-scan": continue # See https://github.com/octarinesec/kube-scan for how to connect to the kube-scan pod. if not previous: wait_until_pod_is_running(component_label, deployment_target) pod_name = get_pod_name(component_label, deployment_target=deployment_target) p = run_in_background( "kubectl logs %(stream_arg)s %(previous_flag)s %(pod_name)s --all-containers" % locals(), print_command=True) def print_command_log(): for line in iter(p.stdout.readline, ''): logger.info(line.strip('\n')) t = Thread(target=print_command_log) t.start() procs.append(p) if wait: wait_for(procs) return procs
def show_dashboard(): """Opens the kubernetes dashboard in a new browser window.""" p = run_in_background('kubectl proxy') run('open http://localhost:8001/ui') p.wait()