@@ -56,6 +56,72 @@ pub use openshell_core::forward::{
5656 find_forward_by_port, list_forwards, stop_forward, stop_forwards_for_sandbox,
5757} ;
5858
59+ /// Compute resource arguments for sandbox creation.
60+ #[ derive( Debug , Default ) ]
61+ pub struct ResourceArgs {
62+ pub cpu_request : Option < String > ,
63+ pub cpu_limit : Option < String > ,
64+ pub memory_request : Option < String > ,
65+ pub memory_limit : Option < String > ,
66+ }
67+
68+ impl ResourceArgs {
69+ /// Build a `prost_types::Struct` matching the Kubernetes resources shape
70+ /// expected by `extract_typed_resources` in the gateway server.
71+ ///
72+ /// Returns `None` when no CPU or memory fields are set (GPU is handled
73+ /// separately via `SandboxSpec.gpu`).
74+ pub fn to_resources_struct ( & self ) -> Option < prost_types:: Struct > {
75+ use prost_types:: { Struct , Value , value:: Kind } ;
76+
77+ let mut requests = std:: collections:: BTreeMap :: new ( ) ;
78+ let mut limits = std:: collections:: BTreeMap :: new ( ) ;
79+
80+ if let Some ( ref v) = self . cpu_request {
81+ requests. insert (
82+ "cpu" . to_string ( ) ,
83+ Value { kind : Some ( Kind :: StringValue ( v. clone ( ) ) ) } ,
84+ ) ;
85+ }
86+ if let Some ( ref v) = self . memory_request {
87+ requests. insert (
88+ "memory" . to_string ( ) ,
89+ Value { kind : Some ( Kind :: StringValue ( v. clone ( ) ) ) } ,
90+ ) ;
91+ }
92+ if let Some ( ref v) = self . cpu_limit {
93+ limits. insert (
94+ "cpu" . to_string ( ) ,
95+ Value { kind : Some ( Kind :: StringValue ( v. clone ( ) ) ) } ,
96+ ) ;
97+ }
98+ if let Some ( ref v) = self . memory_limit {
99+ limits. insert (
100+ "memory" . to_string ( ) ,
101+ Value { kind : Some ( Kind :: StringValue ( v. clone ( ) ) ) } ,
102+ ) ;
103+ }
104+ if requests. is_empty ( ) && limits. is_empty ( ) {
105+ return None ;
106+ }
107+
108+ let mut fields = std:: collections:: BTreeMap :: new ( ) ;
109+ if !requests. is_empty ( ) {
110+ fields. insert (
111+ "requests" . to_string ( ) ,
112+ Value { kind : Some ( Kind :: StructValue ( Struct { fields : requests } ) ) } ,
113+ ) ;
114+ }
115+ if !limits. is_empty ( ) {
116+ fields. insert (
117+ "limits" . to_string ( ) ,
118+ Value { kind : Some ( Kind :: StructValue ( Struct { fields : limits } ) ) } ,
119+ ) ;
120+ }
121+ Some ( Struct { fields } )
122+ }
123+ }
124+
59125/// Convert a sandbox phase integer to a human-readable string.
60126fn phase_name ( phase : i32 ) -> & ' static str {
61127 match SandboxPhase :: try_from ( phase) {
@@ -1923,6 +1989,7 @@ pub async fn sandbox_create_with_bootstrap(
19231989 upload : Option < & ( String , Option < String > , bool ) > ,
19241990 keep : bool ,
19251991 gpu : bool ,
1992+ resource_args : & ResourceArgs ,
19261993 editor : Option < Editor > ,
19271994 remote : Option < & str > ,
19281995 ssh_key : Option < & str > ,
@@ -1954,6 +2021,7 @@ pub async fn sandbox_create_with_bootstrap(
19542021 upload,
19552022 keep,
19562023 gpu,
2024+ resource_args,
19572025 editor,
19582026 remote,
19592027 ssh_key,
@@ -2010,6 +2078,7 @@ pub async fn sandbox_create(
20102078 upload : Option < & ( String , Option < String > , bool ) > ,
20112079 keep : bool ,
20122080 gpu : bool ,
2081+ resource_args : & ResourceArgs ,
20132082 editor : Option < Editor > ,
20142083 remote : Option < & str > ,
20152084 ssh_key : Option < & str > ,
@@ -2109,10 +2178,19 @@ pub async fn sandbox_create(
21092178
21102179 let policy = load_sandbox_policy ( policy) ?;
21112180
2112- let template = image. map ( |img| SandboxTemplate {
2113- image : img,
2114- ..SandboxTemplate :: default ( )
2115- } ) ;
2181+ let resources_struct = resource_args. to_resources_struct ( ) ;
2182+ let template = match ( image, & resources_struct) {
2183+ ( Some ( img) , _) => Some ( SandboxTemplate {
2184+ image : img,
2185+ resources : resources_struct,
2186+ ..SandboxTemplate :: default ( )
2187+ } ) ,
2188+ ( None , Some ( _) ) => Some ( SandboxTemplate {
2189+ resources : resources_struct,
2190+ ..SandboxTemplate :: default ( )
2191+ } ) ,
2192+ ( None , None ) => None ,
2193+ } ;
21162194
21172195 let request = CreateSandboxRequest {
21182196 spec : Some ( SandboxSpec {
@@ -6184,4 +6262,61 @@ mod tests {
61846262 "should end with single-quote: {ssh_escaped}"
61856263 ) ;
61866264 }
6265+
6266+ // ------------------------------------------------------------------
6267+ // ResourceArgs tests
6268+ // ------------------------------------------------------------------
6269+
6270+ use super :: ResourceArgs ;
6271+ use prost_types:: value:: Kind ;
6272+
6273+ #[ test]
6274+ fn resource_args_empty_returns_none ( ) {
6275+ let args = ResourceArgs :: default ( ) ;
6276+ assert ! ( args. to_resources_struct( ) . is_none( ) ) ;
6277+ }
6278+
6279+ #[ test]
6280+ fn resource_args_cpu_only_request ( ) {
6281+ let args = ResourceArgs {
6282+ cpu_request : Some ( "500m" . to_string ( ) ) ,
6283+ ..ResourceArgs :: default ( )
6284+ } ;
6285+ let s = args. to_resources_struct ( ) . expect ( "should return Some" ) ;
6286+ let requests = & s. fields [ "requests" ] ;
6287+ let Kind :: StructValue ( inner) = requests. kind . as_ref ( ) . unwrap ( ) else {
6288+ panic ! ( "expected StructValue" ) ;
6289+ } ;
6290+ let Kind :: StringValue ( cpu) = inner. fields [ "cpu" ] . kind . as_ref ( ) . unwrap ( ) else {
6291+ panic ! ( "expected StringValue" ) ;
6292+ } ;
6293+ assert_eq ! ( cpu, "500m" ) ;
6294+ assert ! ( !s. fields. contains_key( "limits" ) ) ;
6295+ }
6296+
6297+ #[ test]
6298+ fn resource_args_all_four_fields ( ) {
6299+ let args = ResourceArgs {
6300+ cpu_request : Some ( "500m" . to_string ( ) ) ,
6301+ cpu_limit : Some ( "4" . to_string ( ) ) ,
6302+ memory_request : Some ( "256Mi" . to_string ( ) ) ,
6303+ memory_limit : Some ( "8Gi" . to_string ( ) ) ,
6304+ } ;
6305+ let s = args. to_resources_struct ( ) . expect ( "should return Some" ) ;
6306+
6307+ let get = |section : & str , key : & str | -> String {
6308+ let Kind :: StructValue ( sec) = s. fields [ section] . kind . as_ref ( ) . unwrap ( ) else {
6309+ panic ! ( "expected StructValue for {section}" ) ;
6310+ } ;
6311+ let Kind :: StringValue ( val) = sec. fields [ key] . kind . as_ref ( ) . unwrap ( ) else {
6312+ panic ! ( "expected StringValue for {section}.{key}" ) ;
6313+ } ;
6314+ val. clone ( )
6315+ } ;
6316+
6317+ assert_eq ! ( get( "requests" , "cpu" ) , "500m" ) ;
6318+ assert_eq ! ( get( "requests" , "memory" ) , "256Mi" ) ;
6319+ assert_eq ! ( get( "limits" , "cpu" ) , "4" ) ;
6320+ assert_eq ! ( get( "limits" , "memory" ) , "8Gi" ) ;
6321+ }
61876322}
0 commit comments