Skip to main content

hyprstream_workers/runtime/
container.rs

1//! Container types and lifecycle
2//!
3//! A Container runs within a PodSandbox (Kata VM).
4//! Maps directly to Kubernetes CRI Container concept.
5
6use chrono::{DateTime, Utc};
7
8use hyprstream_rpc_std::worker_client::{ContainerState, KeyValue};
9// Use generated ContainerMetadata (matches what's in generated ContainerConfig/ContainerStatus)
10use hyprstream_rpc_std::worker_client::ContainerMetadata;
11
12// DTO types (ContainerConfig, ImageSpec, Mount, Device, ContainerStatus, etc.)
13// are imported directly from the canonical rpc-std client module.
14
15/// Runtime representation of a container
16#[derive(Debug, Clone)]
17pub struct Container {
18    /// Unique container ID
19    pub id: String,
20
21    /// Pod sandbox ID this container belongs to
22    pub pod_sandbox_id: String,
23
24    /// Container metadata
25    pub metadata: ContainerMetadata,
26
27    /// Current state
28    pub state: ContainerState,
29
30    /// Creation timestamp
31    pub created_at: DateTime<Utc>,
32
33    /// Image reference (uses generated ImageSpec type)
34    pub image: hyprstream_rpc_std::worker_client::ImageSpec,
35
36    /// Labels
37    pub labels: Vec<KeyValue>,
38
39    /// Annotations
40    pub annotations: Vec<KeyValue>,
41
42    // Internal fields for process management
43    /// Container process ID (if running)
44    pub(crate) pid: Option<u32>,
45
46    /// Exit code (if exited)
47    pub(crate) exit_code: Option<i32>,
48}
49
50impl Container {
51    /// Create a new container from configuration (uses generated ContainerConfig)
52    pub fn new(
53        id: String,
54        pod_sandbox_id: String,
55        config: &hyprstream_rpc_std::worker_client::ContainerConfig,
56    ) -> Self {
57        Self {
58            id,
59            pod_sandbox_id,
60            metadata: config.metadata.clone(),
61            state: ContainerState::ContainerCreated,
62            created_at: Utc::now(),
63            image: config.image.clone(),
64            labels: config.labels.clone(),
65            annotations: config.annotations.clone(),
66            pid: None,
67            exit_code: None,
68        }
69    }
70
71    /// Create a container from RPC response info
72    ///
73    /// Used when parsing list_containers response from WorkerService.
74    #[allow(clippy::too_many_arguments)]
75    pub fn from_info(
76        id: String,
77        pod_sandbox_id: String,
78        metadata: ContainerMetadata,
79        image: hyprstream_rpc_std::worker_client::ImageSpec,
80        state: ContainerState,
81        created_at: DateTime<Utc>,
82        labels: Vec<KeyValue>,
83        annotations: Vec<KeyValue>,
84    ) -> Self {
85        Self {
86            id,
87            pod_sandbox_id,
88            metadata,
89            state,
90            created_at,
91            image,
92            labels,
93            annotations,
94            pid: None,
95            exit_code: None,
96        }
97    }
98
99    /// Check if container is running
100    pub fn is_running(&self) -> bool {
101        self.state == ContainerState::ContainerRunning
102    }
103}