33import json
44import os
55from dataclasses import asdict
6- from pathlib import Path
6+ from pathlib import Path , PurePosixPath
77from typing import TYPE_CHECKING , Any
88
99from kubernetes import client
1010
11+ from renku_data_services .base_models .core import AnonymousAPIUser , AuthenticatedAPIUser
1112from renku_data_services .notebooks .api .amalthea_patches .utils import get_certificates_volume_mounts
12- from renku_data_services .notebooks .config import _NotebooksConfig
13+ from renku_data_services .notebooks .api .classes .repository import GitProvider , Repository
14+ from renku_data_services .notebooks .config import NotebooksConfig
1315
1416if TYPE_CHECKING :
1517 # NOTE: If these are directly imported then you get circular imports.
1618 from renku_data_services .notebooks .api .classes .server import UserServer
1719
1820
19- async def git_clone_container_v2 (server : "UserServer" ) -> dict [str , Any ] | None :
21+ async def git_clone_container_v2 (
22+ user : AuthenticatedAPIUser | AnonymousAPIUser ,
23+ config : NotebooksConfig ,
24+ repositories : list [Repository ],
25+ git_providers : list [GitProvider ],
26+ workspace_mount_path : PurePosixPath ,
27+ work_dir : PurePosixPath ,
28+ lfs_auto_fetch : bool = False ,
29+ ) -> dict [str , Any ] | None :
2030 """Returns the specification for the container that clones the user's repositories for new operator."""
2131 amalthea_session_work_volume : str = "amalthea-volume"
22- repositories = await server .repositories ()
2332 if not repositories :
2433 return None
2534
2635 etc_cert_volume_mount = get_certificates_volume_mounts (
27- server . config ,
36+ config ,
2837 custom_certs = False ,
2938 etc_certs = True ,
3039 read_only_etc_certs = True ,
3140 )
3241
33- user_is_anonymous = not server .user .is_authenticated
3442 prefix = "GIT_CLONE_"
3543 env = [
36- {
37- "name" : f"{ prefix } WORKSPACE_MOUNT_PATH" ,
38- "value" : server .workspace_mount_path .as_posix (),
39- },
44+ {"name" : f"{ prefix } WORKSPACE_MOUNT_PATH" , "value" : workspace_mount_path .as_posix ()},
4045 {
4146 "name" : f"{ prefix } MOUNT_PATH" ,
42- "value" : server . work_dir .as_posix (),
47+ "value" : work_dir .as_posix (),
4348 },
4449 {
4550 "name" : f"{ prefix } LFS_AUTO_FETCH" ,
46- "value" : "1" if server . server_options . lfs_auto_fetch else "0" ,
51+ "value" : "1" if lfs_auto_fetch else "0" ,
4752 },
4853 {
4954 "name" : f"{ prefix } USER__USERNAME" ,
50- "value" : server . user .email ,
55+ "value" : user .email ,
5156 },
5257 {
5358 "name" : f"{ prefix } USER__RENKU_TOKEN" ,
54- "value" : str (server . user .access_token ),
59+ "value" : str (user .access_token ),
5560 },
56- {"name" : f"{ prefix } IS_GIT_PROXY_ENABLED" , "value" : "0" if user_is_anonymous else "1" },
61+ {"name" : f"{ prefix } IS_GIT_PROXY_ENABLED" , "value" : "0" if user . is_anonymous else "1" },
5762 {
5863 "name" : f"{ prefix } SENTRY__ENABLED" ,
59- "value" : str (server . config .sessions .git_clone .sentry .enabled ).lower (),
64+ "value" : str (config .sessions .git_clone .sentry .enabled ).lower (),
6065 },
6166 {
6267 "name" : f"{ prefix } SENTRY__DSN" ,
63- "value" : server . config .sessions .git_clone .sentry .dsn ,
68+ "value" : config .sessions .git_clone .sentry .dsn ,
6469 },
6570 {
6671 "name" : f"{ prefix } SENTRY__ENVIRONMENT" ,
67- "value" : server . config .sessions .git_clone .sentry .env ,
72+ "value" : config .sessions .git_clone .sentry .env ,
6873 },
6974 {
7075 "name" : f"{ prefix } SENTRY__SAMPLE_RATE" ,
71- "value" : str (server . config .sessions .git_clone .sentry .sample_rate ),
76+ "value" : str (config .sessions .git_clone .sentry .sample_rate ),
7277 },
7378 {"name" : "SENTRY_RELEASE" , "value" : os .environ .get ("SENTRY_RELEASE" )},
7479 {
@@ -80,12 +85,12 @@ async def git_clone_container_v2(server: "UserServer") -> dict[str, Any] | None:
8085 "value" : str (Path (etc_cert_volume_mount [0 ]["mountPath" ]) / "ca-certificates.crt" ),
8186 },
8287 ]
83- if server . user .is_authenticated :
84- if server . user .email :
88+ if user .is_authenticated :
89+ if user .email :
8590 env .append (
86- {"name" : f"{ prefix } USER__EMAIL" , "value" : server . user .email },
91+ {"name" : f"{ prefix } USER__EMAIL" , "value" : user .email },
8792 )
88- full_name = server . user .get_full_name ()
93+ full_name = user .get_full_name ()
8994 if full_name :
9095 env .append (
9196 {
@@ -105,7 +110,8 @@ async def git_clone_container_v2(server: "UserServer") -> dict[str, Any] | None:
105110 )
106111
107112 # Set up git providers
108- required_git_providers = await server .required_git_providers ()
113+ required_provider_ids : set [str ] = {r .provider for r in repositories if r .provider }
114+ required_git_providers = [p for p in git_providers if p .id in required_provider_ids ]
109115 for idx , provider in enumerate (required_git_providers ):
110116 obj_env = f"{ prefix } GIT_PROVIDERS_{ idx } _"
111117 data = dict (id = provider .id , access_token_url = provider .access_token_url )
@@ -117,7 +123,7 @@ async def git_clone_container_v2(server: "UserServer") -> dict[str, Any] | None:
117123 )
118124
119125 return {
120- "image" : server . config .sessions .git_clone .image ,
126+ "image" : config .sessions .git_clone .image ,
121127 "name" : "git-clone" ,
122128 "resources" : {
123129 "requests" : {
@@ -134,7 +140,7 @@ async def git_clone_container_v2(server: "UserServer") -> dict[str, Any] | None:
134140 },
135141 "volumeMounts" : [
136142 {
137- "mountPath" : server . workspace_mount_path .as_posix (),
143+ "mountPath" : workspace_mount_path .as_posix (),
138144 "name" : amalthea_session_work_volume ,
139145 },
140146 * etc_cert_volume_mount ,
@@ -156,7 +162,6 @@ async def git_clone_container(server: "UserServer") -> dict[str, Any] | None:
156162 read_only_etc_certs = True ,
157163 )
158164
159- user_is_anonymous = not server .user .is_authenticated
160165 prefix = "GIT_CLONE_"
161166 env = [
162167 {
@@ -179,7 +184,7 @@ async def git_clone_container(server: "UserServer") -> dict[str, Any] | None:
179184 "name" : f"{ prefix } USER__RENKU_TOKEN" ,
180185 "value" : str (server .user .access_token ),
181186 },
182- {"name" : f"{ prefix } IS_GIT_PROXY_ENABLED" , "value" : "0" if user_is_anonymous else "1" },
187+ {"name" : f"{ prefix } IS_GIT_PROXY_ENABLED" , "value" : "0" if server . user . is_anonymous else "1" },
183188 {
184189 "name" : f"{ prefix } SENTRY__ENABLED" ,
185190 "value" : str (server .config .sessions .git_clone .sentry .enabled ).lower (),
@@ -288,7 +293,7 @@ async def git_clone(server: "UserServer") -> list[dict[str, Any]]:
288293 ]
289294
290295
291- def certificates_container (config : _NotebooksConfig ) -> tuple [client .V1Container , list [client .V1Volume ]]:
296+ def certificates_container (config : NotebooksConfig ) -> tuple [client .V1Container , list [client .V1Volume ]]:
292297 """The specification for the container that setups self signed CAs."""
293298 init_container = client .V1Container (
294299 name = "init-certificates" ,
@@ -321,7 +326,7 @@ def certificates_container(config: _NotebooksConfig) -> tuple[client.V1Container
321326 return (init_container , [volume_etc_certs , volume_custom_certs ])
322327
323328
324- def certificates (config : _NotebooksConfig ) -> list [dict [str , Any ]]:
329+ def certificates (config : NotebooksConfig ) -> list [dict [str , Any ]]:
325330 """Add a container that initializes custom certificate authorities for a session."""
326331 container , vols = certificates_container (config )
327332 api_client = client .ApiClient ()
0 commit comments