Skip to content

Commit

Permalink
support for bind propagation (#526)
Browse files Browse the repository at this point in the history
* added support for propagation

* added homedir propagation modifications

* only set bind propagation if provided

simplifies support, removed need for latest docker-py

---------

Co-authored-by: Janne Jakob Fleischer <[email protected]>
  • Loading branch information
minrk and jannefleischer authored Feb 12, 2025
1 parent 1a65caa commit e7d609b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
11 changes: 9 additions & 2 deletions dockerspawner/dockerspawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,9 @@ async def poll(self):

container_state = container["State"]
self.log.debug(
"Container %s status: %s", self.container_id[:7], json.dumps(container_state, ensure_ascii=False)
"Container %s status: %s",
self.container_id[:7],
json.dumps(container_state, ensure_ascii=False),
)

if container_state["Running"]:
Expand Down Expand Up @@ -1453,11 +1455,16 @@ def _fmt(v):

for k, v in volumes.items():
m = mode
propagation = None
if isinstance(v, dict):
if "mode" in v:
m = v["mode"]
if "propagation" in v:
propagation = v["propagation"]
v = v["bind"]
binds[_fmt(k)] = {"bind": _fmt(v), "mode": m}
binds[_fmt(k)] = bind = {"bind": _fmt(v), "mode": m}
if propagation:
bind["propagation"] = propagation
return binds

def _render_templates(self, obj, ns=None):
Expand Down
23 changes: 22 additions & 1 deletion dockerspawner/systemuserspawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ class SystemUserSpawner(DockerSpawner):
),
)

homedir_bind_propagation = Unicode(
"",
config=True,
help=dedent(
"""
Mode for bind mount propagation for home directory.
Requires docker-py 7.0.
See https://docs.docker.com/engine/storage/bind-mounts/#configure-bind-propagation
.. versionadded:: 13.1
"""
),
)

run_as_root = Bool(
False,
config=True,
Expand Down Expand Up @@ -129,7 +145,12 @@ def volume_binds(self):
}
"""
volumes = super().volume_binds
volumes[self.host_homedir] = {'bind': self.homedir, 'ro': False}
volumes[self.host_homedir] = home_volume = {
'bind': self.homedir,
'ro': False,
}
if self.homedir_bind_propagation:
home_volume["propagation"] = self.homedir_bind_propagation
return volumes

def get_env(self):
Expand Down
4 changes: 2 additions & 2 deletions tests/volumes_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ def test_binds(monkeypatch):

d = DockerSpawner()
d.user = types.SimpleNamespace(name="xyz")
d.volumes = {"a": "b", "c": {"bind": "d", "mode": "Z"}}
d.volumes = {"a": "b", "c": {"bind": "d", "mode": "Z", "propagation": "rprivate"}}
assert d.volume_binds == {
"a": {"bind": "b", "mode": "rw"},
"c": {"bind": "d", "mode": "Z"},
"c": {"bind": "d", "mode": "Z", "propagation": 'rprivate'},
}
d.volumes = {"a": "b", "c": "d", "e": "f"}
assert d.volume_mount_points == ["b", "d", "f"]
Expand Down

0 comments on commit e7d609b

Please sign in to comment.