Skip to content

Commit

Permalink
Add jinja template support to pod-name config via environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
lresende committed Feb 19, 2024
1 parent 1de1784 commit 2a213b6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 4 additions & 1 deletion docs/source/users/kernel-envs.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ There are several supported `KERNEL_` variables that the Enterprise Gateway serv
cases, Enterprise Gateway will honor that value. However, when provided,
it is the user's responsibility that KERNEL_POD_NAME is unique relative to
any pods in the target namespace. In addition, the pod must NOT exist -
unlike the case if KERNEL_NAMESPACE is provided.
unlike the case if KERNEL_NAMESPACE is provided. The KERNEL_POD_NAME can
also be provided as a jinja2 template string
(e.g "{{ kernel_prefix }}-{{ kernel_id | replace('-', '') }}")
which will be evaluated against existing list of environment variables.
KERNEL_REMOTE_HOST=<remote host name>
DistributedProcessProxy only. When specified, this value will override the
Expand Down
15 changes: 15 additions & 0 deletions enterprise_gateway/services/processproxies/k8s.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from typing import Any

import urllib3
from jinja2 import BaseLoader, Environment
from kubernetes import client, config

from ..kernels.remotemanager import RemoteKernelManager
Expand Down Expand Up @@ -216,8 +217,22 @@ def terminate_container_resources(self) -> bool | None:

def _determine_kernel_pod_name(self, **kwargs: dict[str, Any] | None) -> str:
pod_name = kwargs["env"].get("KERNEL_POD_NAME")

if pod_name is None:
pod_name = KernelSessionManager.get_kernel_username(**kwargs) + "-" + self.kernel_id
else:
self.log.debug(f"Processing KERNEL_POD_NAME based on env var => {pod_name}")
if "{{" in pod_name and "}}" in pod_name:
self.log.debug(f"Processing KERNEL_POD_NAME as jinja template")
# Create Jinja2 environment
keywords = {}
for name, value in kwargs["env"].items():
if name.startswith("KERNEL_"):
keywords[name.lower()] = value
keywords["kernel_id"] = self.kernel_id
self.log.debug("Processing pod_name jinja template")
env = Environment(loader=BaseLoader(), autoescape=True)
pod_name = env.from_string(pod_name).render(**keywords)

# Rewrite pod_name to be compatible with DNS name convention
# And put back into env since kernel needs this
Expand Down

0 comments on commit 2a213b6

Please sign in to comment.