Skip to main content

wash_runtime/
types.rs

1//! Types used throughout the wasmcloud crate for workload management and host operations.
2//!
3//! This module contains two main categories of types:
4//!
5//! ## Public API Types (used in [`crate::host::HostApi`])
6//! - Request/Response types: [`WorkloadStartRequest`], [`WorkloadStartResponse`],
7//!   [`WorkloadStatusRequest`], [`WorkloadStatusResponse`],
8//!   [`WorkloadStopRequest`], [`WorkloadStopResponse`]
9//! - Host information: [`HostHeartbeat`]
10//!
11//! ## Core Workload Types (used internally)
12//! - Workload definition: [`Workload`], [`WorkloadState`], [`WorkloadStatus`]
13//! - Component configuration: [`Component`], [`Service`], [`LocalResources`]
14//! - Volume management: [`Volume`], [`VolumeType`], [`VolumeMount`],
15//!   [`EmptyDirVolume`], [`HostPathVolume`]
16
17use bytes::Bytes;
18use std::collections::HashMap;
19
20use crate::wit::WitInterface;
21
22/// Represents a deployable workload containing one or more WebAssembly components.
23/// A workload defines the complete runtime configuration including components,
24/// services, interfaces, and volumes.
25#[derive(Debug, Clone, PartialEq)]
26pub struct Workload {
27    pub namespace: String,
28    pub name: String,
29    pub annotations: HashMap<String, String>,
30    pub service: Option<Service>,
31    pub components: Vec<Component>,
32    pub host_interfaces: Vec<WitInterface>,
33    pub volumes: Vec<Volume>,
34}
35
36/// The current state of a workload in its lifecycle.
37#[derive(Debug, Clone, PartialEq, Eq)]
38pub enum WorkloadState {
39    Unspecified,
40    Starting,
41    Running,
42    Completed,
43    Stopping,
44    Error,
45}
46
47/// Configuration for a long-running service component that handles requests.
48/// Services can be restarted if they fail and have resource limits.
49#[derive(Debug, Clone, PartialEq)]
50pub struct Service {
51    pub bytes: Bytes,
52    pub local_resources: LocalResources,
53    pub max_restarts: u64,
54}
55
56/// A WebAssembly component that can be executed as part of a workload.
57/// Components can be pooled for concurrent execution and have invocation limits.
58#[derive(Debug, Default, Clone, PartialEq)]
59pub struct Component {
60    pub bytes: Bytes,
61    pub local_resources: LocalResources,
62    pub pool_size: i32,
63    pub max_invocations: i32,
64}
65
66/// Resource limits and configuration for a component or service.
67/// Defines memory, CPU limits, configuration values, and volume mounts.
68#[derive(Debug, Clone, PartialEq)]
69pub struct LocalResources {
70    pub memory_limit_mb: i32,
71    pub cpu_limit: i32,
72    /// Opaque key-value configuration shared between operator + runtime + plugins.
73    /// Allows passing arbitrary configuration values to influence implementation behavior for all component interfaces.
74    /// Example: tracing=disable
75    pub config: HashMap<String, String>,
76    // wasi:cli/env variables, copied to WasiCtxBuilder
77    pub environment: HashMap<String, String>,
78    pub volume_mounts: Vec<VolumeMount>,
79    pub allowed_hosts: Vec<String>,
80}
81
82impl Default for LocalResources {
83    fn default() -> Self {
84        Self {
85            memory_limit_mb: -1,
86            cpu_limit: -1,
87            config: HashMap::new(),
88            environment: HashMap::new(),
89            volume_mounts: Vec::new(),
90            allowed_hosts: Vec::new(),
91        }
92    }
93}
94
95/// A named volume that can be mounted into components.
96#[derive(Debug, Clone, PartialEq)]
97pub struct Volume {
98    pub name: String,
99    pub volume_type: VolumeType,
100}
101
102/// The type of volume - either host path or empty directory.
103#[derive(Debug, Clone, PartialEq)]
104pub enum VolumeType {
105    HostPath(HostPathVolume),
106    EmptyDir(EmptyDirVolume),
107}
108
109/// Describes how a volume should be mounted into a component.
110#[derive(Debug, Clone, PartialEq)]
111pub struct VolumeMount {
112    pub name: String,
113    pub mount_path: String,
114    pub read_only: bool,
115}
116
117/// An ephemeral empty directory volume that exists for the lifetime of the workload.
118#[derive(Debug, Clone, PartialEq)]
119pub struct EmptyDirVolume {}
120
121/// A volume that mounts a directory from the host filesystem.
122#[derive(Debug, Clone, PartialEq)]
123pub struct HostPathVolume {
124    pub local_path: String,
125}
126
127/// Information about the host's current state and capabilities.
128/// Returned by [`crate::host::HostApi::heartbeat`].
129#[derive(Debug, Clone, PartialEq)]
130pub struct HostHeartbeat {
131    pub id: String,
132    pub hostname: String,
133    pub friendly_name: String,
134    pub version: String,
135    pub labels: HashMap<String, String>,
136    pub started_at: chrono::DateTime<chrono::Utc>,
137    pub os_arch: String,
138    pub os_name: String,
139    pub os_kernel: String,
140    /// System CPU usage in percent (0.0 - 100.0)
141    pub system_cpu_usage: f32,
142    /// System total memory in bytes
143    pub system_memory_total: u64,
144    /// System free memory in bytes
145    pub system_memory_free: u64,
146    pub component_count: u64,
147    pub workload_count: u64,
148    pub imports: Vec<WitInterface>,
149    pub exports: Vec<WitInterface>,
150}
151
152/// Status information about a workload including its ID, state, and any messages.
153#[derive(Debug, Clone, PartialEq)]
154pub struct WorkloadStatus {
155    pub workload_id: String,
156    pub workload_state: WorkloadState,
157    pub message: String,
158}
159
160/// Request to start a new workload on the host.
161#[derive(Debug, Clone, PartialEq)]
162pub struct WorkloadStartRequest {
163    pub workload: Workload,
164}
165
166/// Response after attempting to start a workload.
167#[derive(Debug, Clone, PartialEq)]
168pub struct WorkloadStartResponse {
169    pub workload_status: WorkloadStatus,
170}
171
172/// Request to get the status of a specific workload.
173#[derive(Debug, Clone, PartialEq)]
174pub struct WorkloadStatusRequest {
175    pub workload_id: String,
176}
177
178/// Response containing the status of a requested workload.
179#[derive(Debug, Clone, PartialEq)]
180pub struct WorkloadStatusResponse {
181    pub workload_status: WorkloadStatus,
182}
183
184/// Request to stop a running workload.
185#[derive(Debug, Clone, PartialEq)]
186pub struct WorkloadStopRequest {
187    pub workload_id: String,
188}
189
190/// Response after attempting to stop a workload.
191#[derive(Debug, Clone, PartialEq)]
192pub struct WorkloadStopResponse {
193    pub workload_status: WorkloadStatus,
194}