33from clipped .utils .lists import to_list
44
55from polyaxon ._connections import V1Connection , V1ConnectionResource
6+ from polyaxon ._env_vars .keys import ENV_KEYS_SANDBOX_TOKEN
67from polyaxon ._flow import V1Init , V1Plugins
78from polyaxon ._k8s import k8s_schemas
89from polyaxon ._runner .converter import BaseConverter as _BaseConverter
10+ from polyaxon ._sandbox .auth import derive_sandbox_token_from_env
11+ from polyaxon ._sandbox .constants import SANDBOX_BOOTSTRAP_PATH , SANDBOX_PORT
912from polyaxon .exceptions import PolyaxonConverterError
1013
1114
@@ -33,6 +36,21 @@ def _get_main_container(
3336 if artifacts_store and not run_path :
3437 raise PolyaxonConverterError ("Run path is required for main container." )
3538
39+ if plugins and plugins .sandbox :
40+ if not main_container :
41+ raise PolyaxonConverterError (
42+ "plugins.sandbox requires a main container."
43+ )
44+ if main_container .args and not main_container .command :
45+ raise PolyaxonConverterError (
46+ "plugins.sandbox does not support args without command."
47+ )
48+ user_argv = to_list (main_container .command , check_none = True ) + to_list (
49+ main_container .args , check_none = True
50+ )
51+ main_container .command = [SANDBOX_BOOTSTRAP_PATH ]
52+ main_container .args = user_argv
53+
3654 if artifacts_store and (
3755 not plugins .collect_artifacts or plugins .mount_artifacts_store
3856 ):
@@ -59,6 +77,7 @@ def _get_main_container(
5977 use_docker_context = plugins .docker ,
6078 use_shm_context = plugins .shm ,
6179 use_tmux_context = plugins .tmux ,
80+ use_sandbox_context = plugins .sandbox ,
6281 run_path = run_path ,
6382 )
6483 if plugins
@@ -82,17 +101,25 @@ def _get_main_container(
82101 secrets = requested_secrets ,
83102 config_maps = requested_config_maps ,
84103 )
104+ if plugins and plugins .sandbox :
105+ env .append (
106+ self ._get_env_var (
107+ name = ENV_KEYS_SANDBOX_TOKEN ,
108+ value = derive_sandbox_token_from_env (self .run_uuid ),
109+ )
110+ )
85111 env += self ._get_resources_env_vars (main_container .resources )
86112
87113 # Env from
88114 env_from = self ._get_env_from_k8s_resources (
89115 secrets = requested_secrets , config_maps = requested_config_maps
90116 )
91117
92- ports = [
93- k8s_schemas .V1ContainerPort (container_port = port )
94- for port in to_list (ports , check_none = True )
95- ]
118+ ports = list (to_list (ports , check_none = True ))
119+ if plugins and plugins .sandbox and SANDBOX_PORT not in ports :
120+ ports .append (SANDBOX_PORT )
121+
122+ ports = [k8s_schemas .V1ContainerPort (container_port = port ) for port in ports ]
96123
97124 return self ._patch_container (
98125 container = main_container ,
0 commit comments