Skip to content

Commit a67790e

Browse files
committed
feat(cli): add CPU/memory resource limit flags to sandbox create
Add four new optional CLI flags to openshell sandbox create: --cpu-request, --cpu-limit, --memory-request, --memory-limit These map to Kubernetes resource requests/limits via the existing DriverResourceRequirements proto fields and K8s driver. When omitted, behavior is unchanged (default K8s resource allocations). Includes ResourceArgs struct, prost_types::Struct serialization, unit tests, and integration test updates. Closes #1003 Signed-off-by: Shridhar Damale <sdamale@nvidia.com> Made-with: Cursor
1 parent 5975805 commit a67790e

3 files changed

Lines changed: 173 additions & 4 deletions

File tree

‎crates/openshell-cli/src/main.rs‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,22 @@ enum SandboxCommands {
11381138
#[arg(long)]
11391139
gpu: bool,
11401140

1141+
/// Minimum CPU cores requested (Kubernetes quantity, e.g. "500m", "2").
1142+
#[arg(long, value_name = "QUANTITY", help_heading = "RESOURCE FLAGS")]
1143+
cpu_request: Option<String>,
1144+
1145+
/// Maximum CPU cores allowed (Kubernetes quantity, e.g. "500m", "4").
1146+
#[arg(long, value_name = "QUANTITY", help_heading = "RESOURCE FLAGS")]
1147+
cpu_limit: Option<String>,
1148+
1149+
/// Minimum memory requested (Kubernetes quantity, e.g. "256Mi", "4Gi").
1150+
#[arg(long, value_name = "QUANTITY", help_heading = "RESOURCE FLAGS")]
1151+
memory_request: Option<String>,
1152+
1153+
/// Maximum memory allowed (Kubernetes quantity, e.g. "512Mi", "8Gi").
1154+
#[arg(long, value_name = "QUANTITY", help_heading = "RESOURCE FLAGS")]
1155+
memory_limit: Option<String>,
1156+
11411157
/// SSH destination for remote bootstrap (e.g., user@hostname).
11421158
/// Only used when no cluster exists yet; ignored if a cluster is
11431159
/// already active.
@@ -2307,6 +2323,10 @@ async fn main() -> Result<()> {
23072323
no_keep,
23082324
editor,
23092325
gpu,
2326+
cpu_request,
2327+
cpu_limit,
2328+
memory_request,
2329+
memory_limit,
23102330
remote,
23112331
ssh_key,
23122332
providers,
@@ -2368,6 +2388,13 @@ async fn main() -> Result<()> {
23682388
(local, remote, !no_git_ignore)
23692389
});
23702390

2391+
let resource_args = run::ResourceArgs {
2392+
cpu_request,
2393+
cpu_limit,
2394+
memory_request,
2395+
memory_limit,
2396+
};
2397+
23712398
let editor = editor.map(Into::into);
23722399
let forward = forward
23732400
.map(|s| openshell_core::forward::ForwardSpec::parse(&s))
@@ -2402,6 +2429,7 @@ async fn main() -> Result<()> {
24022429
upload_spec.as_ref(),
24032430
keep,
24042431
gpu,
2432+
&resource_args,
24052433
editor,
24062434
remote.as_deref(),
24072435
ssh_key.as_deref(),
@@ -2425,6 +2453,7 @@ async fn main() -> Result<()> {
24252453
upload_spec.as_ref(),
24262454
keep,
24272455
gpu,
2456+
&resource_args,
24282457
editor,
24292458
remote.as_deref(),
24302459
ssh_key.as_deref(),

‎crates/openshell-cli/src/run.rs‎

Lines changed: 139 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
60126
fn 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
}

‎crates/openshell-cli/tests/sandbox_create_lifecycle_integration.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ async fn sandbox_create_keeps_command_sessions_by_default() {
571571
None,
572572
true,
573573
false,
574+
&run::ResourceArgs::default(),
574575
None,
575576
None,
576577
None,
@@ -612,6 +613,7 @@ async fn sandbox_create_deletes_command_sessions_with_no_keep() {
612613
None,
613614
false,
614615
false,
616+
&run::ResourceArgs::default(),
615617
None,
616618
None,
617619
None,
@@ -656,6 +658,7 @@ async fn sandbox_create_deletes_shell_sessions_with_no_keep() {
656658
None,
657659
false,
658660
false,
661+
&run::ResourceArgs::default(),
659662
None,
660663
None,
661664
None,
@@ -700,6 +703,7 @@ async fn sandbox_create_keeps_sandbox_with_hidden_keep_flag() {
700703
None,
701704
true,
702705
false,
706+
&run::ResourceArgs::default(),
703707
None,
704708
None,
705709
None,
@@ -741,6 +745,7 @@ async fn sandbox_create_keeps_sandbox_with_forwarding() {
741745
None,
742746
false,
743747
false,
748+
&run::ResourceArgs::default(),
744749
None,
745750
None,
746751
None,

0 commit comments

Comments
 (0)