Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions crates/tools/src/sandbox/apple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ use super::paths::{
};
#[cfg(target_os = "macos")]
use super::types::{
BuildImageResult, DEFAULT_SANDBOX_IMAGE, ManagedFilesMount, NetworkPolicy, SANDBOX_FILES_DIR,
SANDBOX_HOME_DIR, Sandbox, SandboxConfig, SandboxId, truncate_output_for_display,
BuildImageResult, DEFAULT_SANDBOX_IMAGE, ManagedFilesMount, NetworkPolicy, ResourceLimits,
SANDBOX_FILES_DIR, SANDBOX_HOME_DIR, Sandbox, SandboxConfig, SandboxId,
truncate_output_for_display,
};
#[cfg(target_os = "macos")]
use crate::error::{Error, Result};
Expand Down Expand Up @@ -466,8 +467,10 @@ impl AppleContainerSandbox {
image: &str,
tz: Option<&str>,
volumes: &[String],
resource_limits: &ResourceLimits,
) -> std::result::Result<(), CreateError> {
let args = apple_container_run_args(name, image, tz, volumes);
let args = apple_container_run_args(name, image, tz, volumes, resource_limits)
.map_err(|error| CreateError::Other(error.to_string()))?;

let output = tokio::process::Command::new("container")
.args(&args)
Expand Down Expand Up @@ -865,7 +868,9 @@ impl Sandbox for AppleContainerSandbox {

// Phase 2: Create a new container.
info!(name, image = %image, attempt, "creating apple container");
match Self::run_container(&name, &image, tz, &volumes).await {
match Self::run_container(&name, &image, tz, &volumes, &self.config.resource_limits)
.await
Comment thread
penso marked this conversation as resolved.
{
Ok(()) => {},
Err(CreateError::AlreadyExists) => {
warn!(
Expand Down
26 changes: 24 additions & 2 deletions crates/tools/src/sandbox/containers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use {

#[cfg(any(target_os = "macos", test))]
use super::types::APPLE_CONTAINER_SAFE_WORKDIR;
#[cfg(target_os = "macos")]
use super::types::ResourceLimits;
use {
super::types::{
GO_TOOL_INSTALLS, GOGCLI_MODULE_PATH, GOGCLI_VERSION, SANDBOX_HOME_DIR,
Expand Down Expand Up @@ -119,7 +121,8 @@ pub(crate) fn apple_container_run_args(
image: &str,
tz: Option<&str>,
volumes: &[String],
) -> Vec<String> {
resource_limits: &ResourceLimits,
) -> Result<Vec<String>> {
let mut args = vec![
"run".to_string(),
"-d".to_string(),
Expand All @@ -132,6 +135,25 @@ pub(crate) fn apple_container_run_args(
if let Some(tz) = tz {
args.extend(["-e".to_string(), format!("TZ={tz}")]);
}
if let Some(ref memory) = resource_limits.memory_limit {
args.extend(["--memory".to_string(), memory.clone()]);
}
if let Some(cpus) = resource_limits.cpu_quota {
let cpus = cpus.to_string().parse::<i64>().map_err(|_| {
Error::message(format!(
"Apple Container requires cpu_quota to be a positive whole number, got {cpus}"
))
})?;
if cpus < 1 {
return Err(Error::message(format!(
"Apple Container requires cpu_quota to be a positive whole number, got {cpus}"
)));
}
args.extend(["--cpus".to_string(), cpus.to_string()]);
}
if let Some(pids) = resource_limits.pids_max {
args.extend(["--ulimit".to_string(), format!("nproc={pids}")]);
}
for volume in volumes {
args.extend(["--volume".to_string(), volume.to_string()]);
}
Expand All @@ -142,7 +164,7 @@ pub(crate) fn apple_container_run_args(
"-c".to_string(),
apple_container_bootstrap_command(),
]);
args
Ok(args)
}

#[cfg(any(target_os = "macos", test))]
Expand Down
78 changes: 73 additions & 5 deletions crates/tools/src/sandbox/tests/apple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,14 @@ fn test_apple_container_bootstrap_command_uses_portable_sleep() {

#[test]
fn test_apple_container_run_args_pin_workdir_and_bootstrap_home() {
let args = apple_container_run_args("moltis-sandbox-test", "ubuntu:25.10", Some("UTC"), &[]);
let args = apple_container_run_args(
"moltis-sandbox-test",
"ubuntu:25.10",
Some("UTC"),
&[],
&ResourceLimits::default(),
)
.unwrap();
let expected = vec![
"run",
"-d",
Expand All @@ -208,9 +215,14 @@ fn test_apple_container_run_args_pin_workdir_and_bootstrap_home() {

#[test]
fn test_apple_container_run_args_with_home_volume() {
let args = apple_container_run_args("moltis-sandbox-test", "ubuntu:25.10", Some("UTC"), &[
"/tmp/home:/home/sandbox".to_string(),
]);
let args = apple_container_run_args(
"moltis-sandbox-test",
"ubuntu:25.10",
Some("UTC"),
&["/tmp/home:/home/sandbox".to_string()],
&ResourceLimits::default(),
)
.unwrap();
let expected = vec![
"run",
"-d",
Expand Down Expand Up @@ -239,7 +251,14 @@ fn test_apple_container_run_args_with_multiple_volumes() {
"/tmp/home:/home/sandbox".to_string(),
"/tmp/files:/home/sandbox/files:ro".to_string(),
];
let args = apple_container_run_args("moltis-sandbox-test", "ubuntu:25.10", None, &volumes);
let args = apple_container_run_args(
"moltis-sandbox-test",
"ubuntu:25.10",
None,
&volumes,
&ResourceLimits::default(),
)
.unwrap();

assert_eq!(
args.windows(2)
Expand All @@ -250,6 +269,55 @@ fn test_apple_container_run_args_with_multiple_volumes() {
);
}

#[test]
fn test_apple_container_run_args_apply_resource_limits() {
let args = apple_container_run_args(
"moltis-sandbox-test",
"ubuntu:25.10",
None,
&[],
&ResourceLimits {
memory_limit: Some("1G".into()),
cpu_quota: Some(2.0),
pids_max: Some(512),
},
)
.unwrap();

assert_eq!(
args.windows(2)
.filter(|window| matches!(window[0].as_str(), "--memory" | "--cpus" | "--ulimit"))
.map(|window| (window[0].as_str(), window[1].as_str()))
.collect::<Vec<_>>(),
vec![
("--memory", "1G"),
("--cpus", "2"),
("--ulimit", "nproc=512")
]
);
}

#[test]
fn test_apple_container_run_args_reject_fractional_cpu_quota() {
let error = apple_container_run_args(
"moltis-sandbox-test",
"ubuntu:25.10",
None,
&[],
&ResourceLimits {
cpu_quota: Some(0.5),
..Default::default()
},
)
.unwrap_err();

assert!(
error
.to_string()
.contains("cpu_quota to be a positive whole number")
);
}

#[cfg(target_os = "macos")]
#[test]
fn test_apple_container_managed_files_mount_coexists_with_home_persistence() {
Expand Down
6 changes: 5 additions & 1 deletion docs/src/sandbox.md
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,11 @@ How resource limits are applied depends on the backend:
|-------|--------|-----------------|------|-----------------|----------------|
| `memory_limit` | `--memory` | `--memory` | Wasmtime reservation | `ulimit -v` | `MemoryMax=` |
| `cpu_quota` | `--cpus` | `--cpus` | epoch timeout | `ulimit -t` (seconds) | `CPUQuota=` |
| `pids_max` | `--pids-limit` | `--pids-limit` | n/a | `ulimit -u` | `TasksMax=` |
| `pids_max` | `--pids-limit` | `--ulimit nproc=` | n/a | `ulimit -u` | `TasksMax=` |

Apple Container requires version 0.9 or newer to apply `pids_max`.
Its `--cpus` option only accepts positive whole CPU counts, so fractional
`cpu_quota` values are not supported by that backend.

## Comparison

Expand Down
Loading