Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Templatize the notebook container name #805

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion kubespawner/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def make_pod(
ssl_secret_name=None,
ssl_secret_mount_path=None,
logger=None,
notebook_container_name='notebook',
):
"""
Make a k8s pod specification for running a user notebook.
Expand Down Expand Up @@ -333,6 +334,9 @@ def make_pod(

ssl_secret_mount_path:
Specifies the name of the ssl secret mount path for the pod

notebook_container_name:
The name of the notebook container in the pod
"""

pod = V1Pod()
Expand Down Expand Up @@ -529,7 +533,7 @@ def _get_env_var_deps(env):
break

notebook_container = V1Container(
name='notebook',
name=notebook_container_name,
image=image,
working_dir=working_dir,
ports=[V1ContainerPort(name='notebook-port', container_port=port)],
Expand Down
29 changes: 28 additions & 1 deletion kubespawner/spawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ def __init__(self, *args, **kwargs):
self.secret_name = self._expand_user_properties(self.secret_name_template)

self.pvc_name = self._expand_user_properties(self.pvc_name_template)
self.notebook_container_name = self._expand_user_properties(
self.notebook_container_name_template
)
if self.working_dir:
self.working_dir = self._expand_user_properties(self.working_dir)
if self.port == 0:
Expand Down Expand Up @@ -760,6 +763,22 @@ def _deprecated_changed(self, change):
""",
)

notebook_container_name_template = Unicode(
'notebook',
config=True,
help="""
Template to use to form the name of the notebook container in the pod.

`{username}`, `{userid}`, `{servername}`, `{hubnamespace}`,
`{unescaped_username}`, and `{unescaped_servername}` will be expanded if
found within strings of this configuration. The username and servername
come escaped to follow the `DNS label standard
<https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-label-names>`__.

Trailing `-` characters are stripped.
""",
)

@validate('image_pull_secrets')
def _validate_image_pull_secrets(self, proposal):
if type(proposal['value']) == str:
Expand Down Expand Up @@ -1859,6 +1878,9 @@ def _build_pod_labels(self, extra_labels):
'hub.jupyter.org/servername': escapism.escape(
self.name, safe=self.safe_chars, escape_char='-'
).lower(),
# we put the container name in a label so if the template is updated we don't
# lose track of which pods are no longer running and leave stranded pods.
'hub.jupyter.org/notebook_container_name': self.notebook_container_name,
}
)
return labels
Expand Down Expand Up @@ -2031,6 +2053,7 @@ async def get_pod_manifest(self):
ssl_secret_name=self.secret_name if self.internal_ssl else None,
ssl_secret_mount_path=self.secret_mount_path,
logger=self.log,
notebook_container_name=self.notebook_container_name,
)

def get_secret_manifest(self, owner_reference):
Expand Down Expand Up @@ -2209,7 +2232,11 @@ async def poll(self):
return 1
for c in ctr_stat:
# return exit code if notebook container has terminated
if c["name"] == 'notebook':
notebook_container_name = pod["metadata"]["labels"].get(
"hub.jupyter.org/notebook_container_name", "notebook"
)

if c["name"] == notebook_container_name:
if "terminated" in c["state"]:
# call self.stop to delete the pod
if self.delete_stopped_pods:
Expand Down
4 changes: 4 additions & 0 deletions tests/test_spawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -1618,6 +1618,10 @@ async def test_variable_expansion(ssl_app):
"configured_value": {"schedulerName": "extra-pod-config-{username}"},
"findable_in": ["pod"],
},
"notebook_container_name_template": {
"configured_value": "notebook-container-name-template-{username}",
"findable_in": ["pod"],
},
}

c = Config()
Expand Down