Skip to content

Commit

Permalink
fix: do not skip the k8s cache by default (#571)
Browse files Browse the repository at this point in the history
  • Loading branch information
olevski authored Dec 11, 2024
1 parent 098f032 commit f2d7753
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
21 changes: 17 additions & 4 deletions components/renku_data_services/notebooks/api/classes/k8s_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,13 +404,17 @@ def __init__(
cache: ServerCache[_SessionType],
renku_ns_client: NamespacedK8sClient[_SessionType, _Kr8sType],
username_label: str,
# NOTE: If cache skipping is enabled then when the cache fails a large number of
# sessions can overload the k8s API by submitting a lot of calls directly.
skip_cache_if_unavailable: bool = False,
):
self.cache: ServerCache[_SessionType] = cache
self.renku_ns_client: NamespacedK8sClient[_SessionType, _Kr8sType] = renku_ns_client
self.username_label = username_label
if not self.username_label:
raise ProgrammingError("username_label has to be provided to K8sClient")
self.sanitize = self.renku_ns_client.sanitize
self.skip_cache_if_unavailable = skip_cache_if_unavailable

async def list_servers(self, safe_username: str) -> list[_SessionType]:
"""Get a list of servers that belong to a user.
Expand All @@ -420,9 +424,13 @@ async def list_servers(self, safe_username: str) -> list[_SessionType]:
try:
return await self.cache.list_servers(safe_username)
except JSCacheError:
logging.warning(f"Skipping the cache to list servers for user: {safe_username}")
label_selector = f"{self.username_label}={safe_username}"
return await self.renku_ns_client.list_servers(label_selector)
if self.skip_cache_if_unavailable:
logging.warning(f"Skipping the cache to list servers for user: {safe_username}")
# NOTE: The label selector ensures that users can only see their own servers
label_selector = f"{self.username_label}={safe_username}"
return await self.renku_ns_client.list_servers(label_selector)
else:
raise

async def get_server(self, name: str, safe_username: str) -> _SessionType | None:
"""Attempt to get a specific server by name from the cache.
Expand All @@ -433,8 +441,13 @@ async def get_server(self, name: str, safe_username: str) -> _SessionType | None
try:
server = await self.cache.get_server(name)
except JSCacheError:
server = await self.renku_ns_client.get_server(name)
if self.skip_cache_if_unavailable:
server = await self.renku_ns_client.get_server(name)
else:
raise

# NOTE: only the user that the server belongs to can read it, without the line
# below anyone can request and read any one else's server
if server and server.metadata and server.metadata.labels.get(self.username_label) != safe_username:
return None
return server
Expand Down
4 changes: 4 additions & 0 deletions components/renku_data_services/notebooks/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ def from_env(cls, db_config: DBConfig) -> Self:
cache=js_cache,
renku_ns_client=renku_ns_client,
username_label="renku.io/safe-username",
# NOTE: if testing then we should skip the cache if unavailable because we dont deploy the cache in tests
skip_cache_if_unavailable=dummy_stores,
)
v2_cache = ServerCache(amalthea_v2_config.cache_url, AmaltheaSessionV1Alpha1)
renku_ns_v2_client = NamespacedK8sClient(
Expand All @@ -175,6 +177,8 @@ def from_env(cls, db_config: DBConfig) -> Self:
cache=v2_cache,
renku_ns_client=renku_ns_v2_client,
username_label="renku.io/safe-username",
# NOTE: if testing then we should skip the cache if unavailable because we dont deploy the cache in tests
skip_cache_if_unavailable=dummy_stores,
)
return cls(
server_options=server_options,
Expand Down

0 comments on commit f2d7753

Please sign in to comment.