Skip to content

Commit

Permalink
merge create_kwargs/host_config instead of clobbering
Browse files Browse the repository at this point in the history
  • Loading branch information
minrk committed Sep 18, 2023
1 parent e30c944 commit 94f819f
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions dockerspawner/dockerspawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,24 @@ def validate(self, obj, value):
_jupyterhub_xy = "%i.%i" % (jupyterhub.version_info[:2])


def _deep_merge(dest, src):
"""Merge dict `src` into `dest`, recursively
Modifies `dest` in-place, returns dest
"""
for key, value in src.items():
if key in dest:
dest_value = dest[key]
if isinstance(dest_value, dict) and isinstance(value, dict):
dest[key] = _deep_merge(dest_value, value)
else:
dest[key] = value
else:
dest[key] = value

return dest


class DockerSpawner(Spawner):
"""A Spawner for JupyterHub that runs each user's server in a separate docker container"""

Expand Down Expand Up @@ -1162,14 +1180,16 @@ async def create_object(self):
extra_create_kwargs = self._eval_if_callable(self.extra_create_kwargs)
if inspect.isawaitable(extra_create_kwargs):
extra_create_kwargs = await extra_create_kwargs
extra_create_kwargs = self._render_templates(extra_create_kwargs)
extra_host_config = self._eval_if_callable(self.extra_host_config)
if inspect.isawaitable(extra_host_config):
extra_host_config = await extra_host_config
extra_host_config = self._render_templates(extra_host_config)

# ensure internal port is exposed
create_kwargs["ports"] = {"%i/tcp" % self.port: None}

create_kwargs.update(self._render_templates(extra_create_kwargs))
_deep_merge(create_kwargs, extra_create_kwargs)

# build the dictionary of keyword arguments for host_config
host_config = dict(
Expand All @@ -1193,7 +1213,7 @@ async def create_object(self):

if not self.use_internal_ip:
host_config["port_bindings"] = {self.port: (self.host_ip,)}
host_config.update(self._render_templates(extra_host_config))
_deep_merge(host_config, extra_host_config)
host_config.setdefault("network_mode", self.network_name)

self.log.debug("Starting host with config: %s", host_config)
Expand Down

0 comments on commit 94f819f

Please sign in to comment.