Skip to content

Commit ff04ae8

Browse files
committed
persist spawner.port in pod annotations
avoids changes to self.port (e.g. dynamic or changed config) being interpreted as a changed URL for a running pod
1 parent a1de028 commit ff04ae8

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

kubespawner/spawner.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2230,11 +2230,21 @@ def _get_pod_url(self, pod):
22302230
for s in self.pod_connect_ip.split(".")
22312231
]
22322232
)
2233+
# extract port from pod manifest
2234+
port_str = pod["metadata"]["annotations"].get("hub.jupyter.org/port")
2235+
if port_str:
2236+
port = int(port_str)
2237+
else:
2238+
# this should only happend
2239+
self.log.warning(
2240+
f"Pod {pod['metadata']['name']} missing annotation 'hub.jupyter.org/port'"
2241+
)
2242+
port = self.port
22332243

22342244
return "{}://{}:{}".format(
22352245
proto,
22362246
hostname,
2237-
self.port,
2247+
port,
22382248
)
22392249

22402250
async def get_pod_manifest(self):
@@ -2285,6 +2295,7 @@ async def get_pod_manifest(self):
22852295
annotations = self._build_common_annotations(
22862296
self._expand_all(self.extra_annotations)
22872297
)
2298+
annotations["hub.jupyter.org/port"] = str(self.port)
22882299

22892300
return make_pod(
22902301
name=self.pod_name,

tests/test_spawner.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1853,6 +1853,18 @@ async def test_url_changed(kube_ns, kube_client, config, hub_pod, hub):
18531853
await spawner.poll()
18541854
assert spawner.db.commit.call_count == previous_commit_count
18551855

1856+
# changing spawner.port (e.g. updated config while pod is running)
1857+
# should _not_ indicate changed url of running pod
1858+
spawner.port = 1234
1859+
await spawner.poll()
1860+
ref_key = f"{spawner.namespace}/{spawner.pod_name}"
1861+
pod = spawner.pod_reflector.pods.get(ref_key, None)
1862+
assert pod["metadata"]["annotations"]["hub.jupyter.org/port"] == "8888"
1863+
url = spawner._get_pod_url(pod)
1864+
assert url == pod_host
1865+
# didn't commit to the db
1866+
assert spawner.db.commit.call_count == previous_commit_count
1867+
18561868
await spawner.stop()
18571869

18581870

@@ -1917,7 +1929,9 @@ async def test_ipv6_addr():
19171929
spawner = KubeSpawner(
19181930
_mock=True,
19191931
)
1920-
url = spawner._get_pod_url({"status": {"podIP": "cafe:f00d::"}})
1932+
url = spawner._get_pod_url(
1933+
{"metadata": {"annotations": {}}, "status": {"podIP": "cafe:f00d::"}}
1934+
)
19211935
assert "[" in url and "]" in url
19221936

19231937

0 commit comments

Comments
 (0)