zlayer-agent
Container runtime agent for ZLayer, providing lifecycle management for containers, WebAssembly modules, and jobs.
Features
The agent supports multiple container runtimes through feature flags:
- youki (default): Linux-native container runtime using libcontainer. Preferred on Linux for performance (no daemon overhead).
- docker: Cross-platform runtime using Docker daemon. Works on Linux, Windows, and macOS.
- wasm: WebAssembly runtime using wasmtime for executing WASM workloads with WASI support.
Installation
Add to your Cargo.toml:
[]
= { = "../zlayer-agent" }
# Or with specific features
= { = "../zlayer-agent", = ["docker", "wasm"] }
Runtime Selection
Automatic Selection
Use RuntimeConfig::Auto to automatically select the best available runtime:
use ;
let runtime = create_runtime.await?;
Selection logic:
- Linux: Prefers youki if available, falls back to Docker
- Windows/macOS: Uses Docker directly (youki is Linux-only)
- WASM artifacts: Use
RuntimeConfig::Wasmexplicitly
Explicit Selection
use ;
// Use youki runtime
let youki = create_runtime.await?;
// Use Docker runtime
let docker = create_runtime.await?;
// Use WASM runtime
WebAssembly Runtime Support
The wasm feature enables running WebAssembly modules with WASI support as an alternative to traditional containers.
Enabling WASM Support
[]
= { = "../zlayer-agent", = ["wasm"] }
WASM Runtime Configuration
use ;
use PathBuf;
use Duration;
let config = WasmConfig ;
let runtime = new.await?;
WASI Support
The WASM runtime supports WASI Preview 1 (wasip1) with the following capabilities:
- Environment variables
- Command-line arguments
- Stdout/stderr output
- Exit codes via
proc_exit
WASM vs Container Runtime Comparison
| Feature | Container (youki/Docker) | WASM |
|---|---|---|
| Isolation | Process/cgroup | In-process sandbox |
| Startup time | 100ms+ | <10ms |
| Memory overhead | Higher (full rootfs) | Lower (just module) |
exec support |
Yes | No |
| Cgroup stats | Yes | No (stub values) |
| Filesystem mounts | Yes | Limited |
| Network | Full | Limited |
| Platform | Linux/Windows/macOS | Cross-platform |
WASM Limitations
- No
execsupport: WASM modules are single-process and don't support executing additional commands - No cgroup stats: WASM runs in-process without kernel isolation, so resource stats return stub values
- Limited filesystem: Currently only environment variables are passed; filesystem mounts are not yet supported
- Single entry point: WASM modules must export
_startormainfunction
Example: Running a WASM Workload
use ;
use ServiceSpec;
use Duration;
async
Service Manager
The ServiceManager provides higher-level orchestration for services:
use ;
use DeploymentSpec;
// Load deployment spec
let spec: DeploymentSpec = from_str?;
// Create service manager with auto-selected runtime
let manager = new.await?;
// Deploy services
for in &spec.services
Testing
Running All Tests
# Unit tests
# With Docker runtime tests
# With WASM runtime tests
# All features
WASM-Specific Tests
The WASM tests use inline WAT (WebAssembly Text Format) modules compiled at test time, so no external WASM binaries are required.
Module Structure
runtime.rs: AbstractRuntimetrait andContainerStatetypesruntimes/: Runtime implementationsyouki.rs: Linux-native runtime using libcontainerdocker.rs: Docker daemon runtime (feature:docker)wasm.rs: WebAssembly runtime using wasmtime (feature:wasm)
service.rs:ServiceManagerfor higher-level orchestrationcontainer_supervisor.rs: Container health monitoring and restart logicjob.rs: Job execution support for run-to-completion workloadscron_scheduler.rs: Cron-based job schedulinghealth.rs: Health check implementationsinit.rs: Init action orchestrationenv.rs: Environment variable resolutionbundle.rs: OCI bundle creation for youki
Environment Variables
| Variable | Description | Default |
|---|---|---|
ZLAYER_WASM_CACHE_DIR |
WASM module cache directory | /var/lib/zlayer/wasm |
ZLAYER_RUNTIME_DIR |
Runtime state directory | /var/run/zlayer |
ZLAYER_ROOT_DIR |
Root directory for bundles | /var/lib/zlayer |
License
Apache-2.0