|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import yaml |
| 4 | +import argparse |
| 5 | +from kubernetes import client, config |
| 6 | +import urllib3 |
| 7 | + |
| 8 | +from jinja2 import FileSystemLoader, Environment |
| 9 | + |
| 10 | +urllib3.disable_warnings() |
| 11 | + |
| 12 | +KERNEL_POD_TEMPLATE_PATH = '/kernel-pod.yaml.j2' |
| 13 | + |
| 14 | + |
| 15 | +def generate_kernel_pod_yaml(keywords): |
| 16 | + """Return the kubernetes pod spec as a yaml string. |
| 17 | +
|
| 18 | + - load jinja2 template from this file directory. |
| 19 | + - substitute template variables with keywords items. |
| 20 | + """ |
| 21 | + j_env = Environment(loader=FileSystemLoader(os.path.dirname(__file__)), trim_blocks=True, lstrip_blocks=True) |
| 22 | + # jinja2 template substitutes template variables with None though keywords doesn't contain corresponding item. |
| 23 | + # Therefore, no need to check if any are left unsubstituted; Kubernetes API server will validate the pod spec. |
| 24 | + k8s_yaml = j_env.get_template(KERNEL_POD_TEMPLATE_PATH).render(**keywords) |
| 25 | + |
| 26 | + return k8s_yaml |
| 27 | + |
| 28 | + |
| 29 | +def launch_kubernetes_kernel(kernel_id, response_addr, spark_context_init_mode): |
| 30 | + # Launches a containerized kernel as a kubernetes pod. |
| 31 | + |
| 32 | + config.load_incluster_config() |
| 33 | + |
| 34 | + # Capture keywords and their values. |
| 35 | + keywords = dict() |
| 36 | + |
| 37 | + # Factory values... |
| 38 | + # Since jupyter lower cases the kernel directory as the kernel-name, we need to capture its case-sensitive |
| 39 | + # value since this is used to locate the kernel launch script within the image. |
| 40 | + keywords['kernel_name'] = os.path.basename(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 41 | + keywords['kernel_id'] = kernel_id |
| 42 | + keywords['eg_response_address'] = response_addr |
| 43 | + keywords['kernel_spark_context_init_mode'] = spark_context_init_mode |
| 44 | + |
| 45 | + # Walk env variables looking for names prefixed with KERNEL_. When found, set corresponding keyword value |
| 46 | + # with name in lower case. |
| 47 | + for name, value in os.environ.items(): |
| 48 | + if name.startswith('KERNEL_'): |
| 49 | + keywords[name.lower()] = yaml.safe_load(value) |
| 50 | + |
| 51 | + # Substitute all template variable (wrapped with {{ }}) and generate `yaml` string. |
| 52 | + k8s_yaml = generate_kernel_pod_yaml(keywords) |
| 53 | + |
| 54 | + # For each k8s object (kind), call the appropriate API method. Too bad there isn't a method |
| 55 | + # that can take a set of objects. |
| 56 | + # |
| 57 | + # Creation for additional kinds of k8s objects can be added below. Refer to |
| 58 | + # https://github.com/kubernetes-client/python for API signatures. Other examples can be found in |
| 59 | + # https://github.com/jupyter-incubator/enterprise_gateway/blob/master/enterprise_gateway/services/processproxies/k8s.py |
| 60 | + # |
| 61 | + kernel_namespace = keywords['kernel_namespace'] |
| 62 | + k8s_objs = yaml.safe_load_all(k8s_yaml) |
| 63 | + for k8s_obj in k8s_objs: |
| 64 | + if k8s_obj.get('kind'): |
| 65 | + if k8s_obj['kind'] == 'Pod': |
| 66 | + # print("{}".format(k8s_obj)) # useful for debug |
| 67 | + client.CoreV1Api(client.ApiClient()).create_namespaced_pod(body=k8s_obj, namespace=kernel_namespace) |
| 68 | + elif k8s_obj['kind'] == 'Secret': |
| 69 | + client.CoreV1Api(client.ApiClient()).create_namespaced_secret(body=k8s_obj, namespace=kernel_namespace) |
| 70 | + elif k8s_obj['kind'] == 'PersistentVolumeClaim': |
| 71 | + client.CoreV1Api(client.ApiClient()).create_namespaced_persistent_volume_claim( |
| 72 | + body=k8s_obj, namespace=kernel_namespace) |
| 73 | + elif k8s_obj['kind'] == 'PersistentVolume': |
| 74 | + client.CoreV1Api(client.ApiClient()).create_persistent_volume(body=k8s_obj) |
| 75 | + else: |
| 76 | + sys.exit("ERROR - Unhandled Kubernetes object kind '{}' found in yaml file - " |
| 77 | + "kernel launch terminating!".format(k8s_obj['kind'])) |
| 78 | + else: |
| 79 | + sys.exit("ERROR - Unknown Kubernetes object '{}' found in yaml file - kernel launch terminating!". |
| 80 | + format(k8s_obj)) |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == '__main__': |
| 84 | + """ |
| 85 | + Usage: launch_kubernetes_kernel |
| 86 | + [--RemoteProcessProxy.kernel-id <kernel_id>] |
| 87 | + [--RemoteProcessProxy.response-address <response_addr>] |
| 88 | + [--RemoteProcessProxy.spark-context-initialization-mode <mode>] |
| 89 | + """ |
| 90 | + |
| 91 | + parser = argparse.ArgumentParser() |
| 92 | + parser.add_argument('--RemoteProcessProxy.kernel-id', dest='kernel_id', nargs='?', |
| 93 | + help='Indicates the id associated with the launched kernel.') |
| 94 | + parser.add_argument('--RemoteProcessProxy.response-address', dest='response_address', nargs='?', |
| 95 | + metavar='<ip>:<port>', help='Connection address (<ip>:<port>) for returning connection file') |
| 96 | + parser.add_argument('--RemoteProcessProxy.spark-context-initialization-mode', dest='spark_context_init_mode', |
| 97 | + nargs='?', help='Indicates whether or how a spark context should be created', |
| 98 | + default='none') |
| 99 | + |
| 100 | + arguments = vars(parser.parse_args()) |
| 101 | + kernel_id = arguments['kernel_id'] |
| 102 | + response_addr = arguments['response_address'] |
| 103 | + spark_context_init_mode = arguments['spark_context_init_mode'] |
| 104 | + |
| 105 | + launch_kubernetes_kernel(kernel_id, response_addr, spark_context_init_mode) |
0 commit comments