77from dataclasses import dataclass , field
88from enum import Enum
99from io import StringIO
10- from typing import Any , ClassVar , Self , Union
10+ from typing import Any , ClassVar , Literal , Self , Union
1111
1212import yaml
1313from kubernetes import client
@@ -256,6 +256,16 @@ def from_env(cls) -> Self:
256256 annotations = yaml .safe_load (StringIO (os .environ .get ("NB_SESSIONS__INGRESS__ANNOTATIONS" , "{}" ))),
257257 )
258258
259+ @property
260+ def scheme (self ) -> Literal ["https" ] | Literal ["http" ]:
261+ if self .tls_secret or self .use_default_cluster_tls_cert :
262+ return "https"
263+ return "http"
264+
265+ @property
266+ def renku_url (self ) -> str :
267+ return f"{ self .scheme } ::{ self .host } "
268+
259269
260270@dataclass
261271class _GenericCullingConfig :
@@ -356,6 +366,11 @@ def from_env(cls) -> Self:
356366 )
357367
358368
369+ def _get_renku_url_fallback (ingress_config : _SessionIngress ) -> str :
370+ scheme = "https" if ingress_config .tls_secret or ingress_config .use_default_cluster_tls_cert else "http"
371+ return f"{ scheme } ::{ ingress_config .host } "
372+
373+
359374@dataclass
360375class _SessionConfig :
361376 culling : _SessionCullingConfig
@@ -368,6 +383,7 @@ class _SessionConfig:
368383 storage : _SessionStorageConfig
369384 containers : _SessionContainers
370385 ssh : _SessionSshConfig
386+ renku_url : str
371387 default_image : str = "renku/singleuser:latest"
372388 enforce_cpu_limits : CPUEnforcement = CPUEnforcement .OFF
373389 termination_warning_duration_seconds : int = 12 * 60 * 60
@@ -386,12 +402,13 @@ class _SessionConfig:
386402
387403 @classmethod
388404 def from_env (cls ) -> Self :
405+ ingress = _SessionIngress .from_env ()
389406 return cls (
390407 culling = _SessionCullingConfig .from_env (),
391408 git_proxy = _GitProxyConfig .from_env (),
392409 git_rpc_server = _GitRpcServerConfig .from_env (),
393410 git_clone = _GitCloneConfig .from_env (),
394- ingress = _SessionIngress . from_env () ,
411+ ingress = ingress ,
395412 ca_certs = _CustomCaCertsConfig .from_env (),
396413 oidc = _SessionOidcConfig .from_env (),
397414 storage = _SessionStorageConfig .from_env (),
@@ -404,16 +421,18 @@ def from_env(cls) -> Self:
404421 node_selector = yaml .safe_load (StringIO (os .environ .get ("NB_SESSIONS__NODE_SELECTOR" , "{}" ))),
405422 affinity = yaml .safe_load (StringIO (os .environ .get ("NB_SESSIONS__AFFINITY" , "{}" ))),
406423 tolerations = yaml .safe_load (StringIO (os .environ .get ("NB_SESSIONS__TOLERATIONS" , "[]" ))),
424+ renku_url = os .environ .get ("RENKU_URL" ) or _get_renku_url_fallback (ingress ),
407425 )
408426
409427 @classmethod
410428 def _for_testing (cls ) -> Self :
429+ ingress = _SessionIngress (host = "localhost" , tls_secret = "some-secret" ) # nosec: B106
411430 return cls (
412431 culling = _SessionCullingConfig .from_env (),
413432 git_proxy = _GitProxyConfig (renku_client_secret = "not-defined" ), # nosec B106
414433 git_rpc_server = _GitRpcServerConfig .from_env (),
415434 git_clone = _GitCloneConfig .from_env (),
416- ingress = _SessionIngress ( host = "localhost" , tls_secret = "some-secret" ), # nosec: B106
435+ ingress = ingress ,
417436 ca_certs = _CustomCaCertsConfig .from_env (),
418437 oidc = _SessionOidcConfig (
419438 client_id = "not-defined" ,
@@ -432,6 +451,7 @@ def _for_testing(cls) -> Self:
432451 node_selector = yaml .safe_load (StringIO (os .environ .get ("" , "{}" ))),
433452 affinity = yaml .safe_load (StringIO (os .environ .get ("" , "{}" ))),
434453 tolerations = yaml .safe_load (StringIO (os .environ .get ("" , "[]" ))),
454+ renku_url = os .environ .get ("RENKU_URL" ) or _get_renku_url_fallback (ingress ),
435455 )
436456
437457 @property
0 commit comments