Skip to content

Commit ea2cc43

Browse files
committed
Templatize the notebook name
1 parent de31a7e commit ea2cc43

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

kubespawner/objects.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ def make_pod(
122122
ssl_secret_name=None,
123123
ssl_secret_mount_path=None,
124124
logger=None,
125+
notebook_container_name='notebook',
125126
):
126127
"""
127128
Make a k8s pod specification for running a user notebook.
@@ -333,6 +334,9 @@ def make_pod(
333334
334335
ssl_secret_mount_path:
335336
Specifies the name of the ssl secret mount path for the pod
337+
338+
notebook_container_name:
339+
The name of the notebook container in the pod
336340
"""
337341

338342
pod = V1Pod()
@@ -529,7 +533,7 @@ def _get_env_var_deps(env):
529533
break
530534

531535
notebook_container = V1Container(
532-
name='notebook',
536+
name=notebook_container_name,
533537
image=image,
534538
working_dir=working_dir,
535539
ports=[V1ContainerPort(name='notebook-port', container_port=port)],

kubespawner/spawner.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ def __init__(self, *args, **kwargs):
193193
self.secret_name = self._expand_user_properties(self.secret_name_template)
194194

195195
self.pvc_name = self._expand_user_properties(self.pvc_name_template)
196+
self.notebook_container_name = self._expand_user_properties(
197+
self.notebook_container_name_template
198+
)
196199
if self.working_dir:
197200
self.working_dir = self._expand_user_properties(self.working_dir)
198201
if self.port == 0:
@@ -760,6 +763,22 @@ def _deprecated_changed(self, change):
760763
""",
761764
)
762765

766+
notebook_container_name_template = Unicode(
767+
'notebook',
768+
config=True,
769+
help="""
770+
Template to use to form the name of the notebook container in the pod.
771+
772+
`{username}`, `{userid}`, `{servername}`, `{hubnamespace}`,
773+
`{unescaped_username}`, and `{unescaped_servername}` will be expanded if
774+
found within strings of this configuration. The username and servername
775+
come escaped to follow the `DNS label standard
776+
<https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-label-names>`__.
777+
778+
Trailing `-` characters are stripped.
779+
""",
780+
)
781+
763782
@validate('image_pull_secrets')
764783
def _validate_image_pull_secrets(self, proposal):
765784
if type(proposal['value']) == str:
@@ -1859,6 +1878,9 @@ def _build_pod_labels(self, extra_labels):
18591878
'hub.jupyter.org/servername': escapism.escape(
18601879
self.name, safe=self.safe_chars, escape_char='-'
18611880
).lower(),
1881+
# we put the container name in a label so if the template is updated we don't
1882+
# lose track of which pods are no longer running and leave stranded pods.
1883+
'hub.jupyter.org/notebook_container_name': self.notebook_container_name,
18621884
}
18631885
)
18641886
return labels
@@ -2031,6 +2053,7 @@ async def get_pod_manifest(self):
20312053
ssl_secret_name=self.secret_name if self.internal_ssl else None,
20322054
ssl_secret_mount_path=self.secret_mount_path,
20332055
logger=self.log,
2056+
notebook_container_name=self.notebook_container_name,
20342057
)
20352058

20362059
def get_secret_manifest(self, owner_reference):
@@ -2209,7 +2232,11 @@ async def poll(self):
22092232
return 1
22102233
for c in ctr_stat:
22112234
# return exit code if notebook container has terminated
2212-
if c["name"] == 'notebook':
2235+
notebook_container_name = pod["metadata"]["labels"].get(
2236+
"hub.jupyter.org/notebook_container_name", "notebook"
2237+
)
2238+
2239+
if c["name"] == notebook_container_name:
22132240
if "terminated" in c["state"]:
22142241
# call self.stop to delete the pod
22152242
if self.delete_stopped_pods:

tests/test_spawner.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,6 +1618,10 @@ async def test_variable_expansion(ssl_app):
16181618
"configured_value": {"schedulerName": "extra-pod-config-{username}"},
16191619
"findable_in": ["pod"],
16201620
},
1621+
"notebook_container_name_template": {
1622+
"configured_value": "notebook-container-name-template-{username}",
1623+
"findable_in": ["pod"],
1624+
},
16211625
}
16221626

16231627
c = Config()

0 commit comments

Comments
 (0)