3
3
import json
4
4
import os
5
5
from dataclasses import asdict
6
- from pathlib import Path
6
+ from pathlib import Path , PurePosixPath
7
7
from typing import TYPE_CHECKING , Any
8
8
9
9
from kubernetes import client
10
10
11
+ from renku_data_services .base_models .core import AnonymousAPIUser , AuthenticatedAPIUser
11
12
from 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
13
15
14
16
if TYPE_CHECKING :
15
17
# NOTE: If these are directly imported then you get circular imports.
16
18
from renku_data_services .notebooks .api .classes .server import UserServer
17
19
18
20
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 :
20
30
"""Returns the specification for the container that clones the user's repositories for new operator."""
21
31
amalthea_session_work_volume : str = "amalthea-volume"
22
- repositories = await server .repositories ()
23
32
if not repositories :
24
33
return None
25
34
26
35
etc_cert_volume_mount = get_certificates_volume_mounts (
27
- server . config ,
36
+ config ,
28
37
custom_certs = False ,
29
38
etc_certs = True ,
30
39
read_only_etc_certs = True ,
31
40
)
32
41
33
- user_is_anonymous = not server .user .is_authenticated
34
42
prefix = "GIT_CLONE_"
35
43
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 ()},
40
45
{
41
46
"name" : f"{ prefix } MOUNT_PATH" ,
42
- "value" : server . work_dir .as_posix (),
47
+ "value" : work_dir .as_posix (),
43
48
},
44
49
{
45
50
"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" ,
47
52
},
48
53
{
49
54
"name" : f"{ prefix } USER__USERNAME" ,
50
- "value" : server . user .email ,
55
+ "value" : user .email ,
51
56
},
52
57
{
53
58
"name" : f"{ prefix } USER__RENKU_TOKEN" ,
54
- "value" : str (server . user .access_token ),
59
+ "value" : str (user .access_token ),
55
60
},
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" },
57
62
{
58
63
"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 (),
60
65
},
61
66
{
62
67
"name" : f"{ prefix } SENTRY__DSN" ,
63
- "value" : server . config .sessions .git_clone .sentry .dsn ,
68
+ "value" : config .sessions .git_clone .sentry .dsn ,
64
69
},
65
70
{
66
71
"name" : f"{ prefix } SENTRY__ENVIRONMENT" ,
67
- "value" : server . config .sessions .git_clone .sentry .env ,
72
+ "value" : config .sessions .git_clone .sentry .env ,
68
73
},
69
74
{
70
75
"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 ),
72
77
},
73
78
{"name" : "SENTRY_RELEASE" , "value" : os .environ .get ("SENTRY_RELEASE" )},
74
79
{
@@ -80,12 +85,12 @@ async def git_clone_container_v2(server: "UserServer") -> dict[str, Any] | None:
80
85
"value" : str (Path (etc_cert_volume_mount [0 ]["mountPath" ]) / "ca-certificates.crt" ),
81
86
},
82
87
]
83
- if server . user .is_authenticated :
84
- if server . user .email :
88
+ if user .is_authenticated :
89
+ if user .email :
85
90
env .append (
86
- {"name" : f"{ prefix } USER__EMAIL" , "value" : server . user .email },
91
+ {"name" : f"{ prefix } USER__EMAIL" , "value" : user .email },
87
92
)
88
- full_name = server . user .get_full_name ()
93
+ full_name = user .get_full_name ()
89
94
if full_name :
90
95
env .append (
91
96
{
@@ -105,7 +110,8 @@ async def git_clone_container_v2(server: "UserServer") -> dict[str, Any] | None:
105
110
)
106
111
107
112
# 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 ]
109
115
for idx , provider in enumerate (required_git_providers ):
110
116
obj_env = f"{ prefix } GIT_PROVIDERS_{ idx } _"
111
117
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:
117
123
)
118
124
119
125
return {
120
- "image" : server . config .sessions .git_clone .image ,
126
+ "image" : config .sessions .git_clone .image ,
121
127
"name" : "git-clone" ,
122
128
"resources" : {
123
129
"requests" : {
@@ -134,7 +140,7 @@ async def git_clone_container_v2(server: "UserServer") -> dict[str, Any] | None:
134
140
},
135
141
"volumeMounts" : [
136
142
{
137
- "mountPath" : server . workspace_mount_path .as_posix (),
143
+ "mountPath" : workspace_mount_path .as_posix (),
138
144
"name" : amalthea_session_work_volume ,
139
145
},
140
146
* etc_cert_volume_mount ,
@@ -156,7 +162,6 @@ async def git_clone_container(server: "UserServer") -> dict[str, Any] | None:
156
162
read_only_etc_certs = True ,
157
163
)
158
164
159
- user_is_anonymous = not server .user .is_authenticated
160
165
prefix = "GIT_CLONE_"
161
166
env = [
162
167
{
@@ -179,7 +184,7 @@ async def git_clone_container(server: "UserServer") -> dict[str, Any] | None:
179
184
"name" : f"{ prefix } USER__RENKU_TOKEN" ,
180
185
"value" : str (server .user .access_token ),
181
186
},
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" },
183
188
{
184
189
"name" : f"{ prefix } SENTRY__ENABLED" ,
185
190
"value" : str (server .config .sessions .git_clone .sentry .enabled ).lower (),
@@ -288,7 +293,7 @@ async def git_clone(server: "UserServer") -> list[dict[str, Any]]:
288
293
]
289
294
290
295
291
- def certificates_container (config : _NotebooksConfig ) -> tuple [client .V1Container , list [client .V1Volume ]]:
296
+ def certificates_container (config : NotebooksConfig ) -> tuple [client .V1Container , list [client .V1Volume ]]:
292
297
"""The specification for the container that setups self signed CAs."""
293
298
init_container = client .V1Container (
294
299
name = "init-certificates" ,
@@ -321,7 +326,7 @@ def certificates_container(config: _NotebooksConfig) -> tuple[client.V1Container
321
326
return (init_container , [volume_etc_certs , volume_custom_certs ])
322
327
323
328
324
- def certificates (config : _NotebooksConfig ) -> list [dict [str , Any ]]:
329
+ def certificates (config : NotebooksConfig ) -> list [dict [str , Any ]]:
325
330
"""Add a container that initializes custom certificate authorities for a session."""
326
331
container , vols = certificates_container (config )
327
332
api_client = client .ApiClient ()
0 commit comments