Skip to main content

krab_runtime/
lib.rs

1pub mod cgroup;
2pub mod docker;
3pub mod host;
4pub mod policy;
5
6use bytes::Bytes;
7use std::collections::BTreeMap;
8use std::future::Future;
9use std::io;
10use tokio::sync::mpsc;
11
12#[derive(Clone, Debug, Default, PartialEq)]
13pub struct ContainerInfo {
14    pub id: String,
15    pub name: String,
16    pub image: String,
17    pub image_id: String,
18    pub state: String,
19    pub created: i64,
20    pub labels: BTreeMap<String, String>,
21    pub ports: Vec<(u16, String)>,
22    pub ip: String,
23    pub started_at: String,
24    pub finished_at: String,
25    pub exit_code: i64,
26    pub restart_count: u64,
27    pub tty: bool,
28    pub pid: i64,
29}
30
31#[derive(Clone, Debug, Default)]
32pub struct LogOpts {
33    pub tail: Option<usize>,
34    pub follow: bool,
35    pub timestamps: bool,
36    pub since_seconds: Option<u64>,
37}
38
39#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
40pub enum RestartPolicy {
41    #[default]
42    Always,
43    OnFailure,
44    Never,
45}
46
47impl RestartPolicy {
48    pub fn parse(s: &str) -> Self {
49        match s {
50            "OnFailure" => RestartPolicy::OnFailure,
51            "Never" => RestartPolicy::Never,
52            _ => RestartPolicy::Always,
53        }
54    }
55
56    pub fn docker_name(self) -> &'static str {
57        match self {
58            RestartPolicy::Always => "unless-stopped",
59            RestartPolicy::OnFailure => "on-failure",
60            RestartPolicy::Never => "no",
61        }
62    }
63}
64
65#[derive(Clone, Debug, Default)]
66pub struct ExecOpts {
67    pub cmd: Vec<String>,
68    pub tty: bool,
69    pub stdin: bool,
70}
71
72pub struct ExecHandle {
73    pub exec_id: String,
74    pub stdout: mpsc::Receiver<Bytes>,
75    pub stderr: mpsc::Receiver<Bytes>,
76    pub stdin: Option<Box<dyn tokio::io::AsyncWrite + Unpin + Send>>,
77    pub hold: Option<Box<dyn tokio::io::AsyncWrite + Unpin + Send>>,
78}
79
80#[derive(Clone, Debug, Default, PartialEq)]
81pub struct PortSpec {
82    pub container_port: u16,
83    pub protocol: String,
84    pub host_port: Option<u16>,
85    pub host_ip: Option<String>,
86}
87
88#[derive(Clone, Debug, PartialEq)]
89pub enum Mount {
90    Bind {
91        src: String,
92        dst: String,
93        read_only: bool,
94    },
95    Volume {
96        name: String,
97        dst: String,
98        read_only: bool,
99    },
100    Tmpfs {
101        dst: String,
102    },
103}
104
105#[derive(Clone, Debug, Default, PartialEq)]
106pub struct ContainerSpec {
107    pub name: String,
108    pub image: String,
109    pub command: Vec<String>,
110    pub args: Vec<String>,
111    pub env: Vec<String>,
112    pub working_dir: Option<String>,
113    pub ports: Vec<PortSpec>,
114    pub labels: BTreeMap<String, String>,
115    pub restart: RestartPolicy,
116    pub memory_bytes: Option<i64>,
117    pub nano_cpus: Option<i64>,
118    pub mounts: Vec<Mount>,
119    pub hostname: Option<String>,
120    pub dns_search: Vec<String>,
121    pub dns_options: Vec<String>,
122    pub network: Option<String>,
123    pub aliases: Vec<String>,
124    pub user: Option<String>,
125    pub tty: bool,
126    pub stdin: bool,
127    pub allow_privilege_escalation: bool,
128    pub read_only_root: bool,
129}
130
131pub const MANAGED_LABEL: &str = "krab.io/managed";
132pub const NAME_PREFIX: &str = "krab_";
133
134pub fn container_name(ns: &str, pod: &str, container: &str) -> String {
135    format!("{NAME_PREFIX}{ns}_{pod}_{container}")
136}
137
138pub fn is_managed(info: &ContainerInfo) -> bool {
139    info.name.trim_start_matches('/').starts_with(NAME_PREFIX)
140        && info.labels.get(MANAGED_LABEL).map(String::as_str) == Some("true")
141}
142
143fn unsupported() -> io::Error {
144    io::Error::new(io::ErrorKind::Unsupported, "runtime operation unsupported")
145}
146
147pub trait Runtime: Send + Sync + 'static {
148    fn name(&self) -> &str;
149    fn list(&self) -> impl Future<Output = io::Result<Vec<ContainerInfo>>> + Send;
150    fn inspect(&self, info: &mut ContainerInfo) -> impl Future<Output = io::Result<()>> + Send;
151    fn logs(
152        &self,
153        info: &ContainerInfo,
154        opts: &LogOpts,
155    ) -> impl Future<Output = io::Result<mpsc::Receiver<Bytes>>> + Send;
156    fn pull_image(&self, _image: &str) -> impl Future<Output = io::Result<()>> + Send {
157        async { Err(unsupported()) }
158    }
159    fn create_container(
160        &self,
161        _spec: &ContainerSpec,
162    ) -> impl Future<Output = io::Result<String>> + Send {
163        async { Err(unsupported()) }
164    }
165    fn start(&self, _id: &str) -> impl Future<Output = io::Result<()>> + Send {
166        async { Err(unsupported()) }
167    }
168    fn stop(&self, _id: &str, _timeout_secs: u64) -> impl Future<Output = io::Result<()>> + Send {
169        async { Err(unsupported()) }
170    }
171    fn remove(&self, _id: &str, _force: bool) -> impl Future<Output = io::Result<()>> + Send {
172        async { Err(unsupported()) }
173    }
174    fn ensure_network(&self, _name: &str) -> impl Future<Output = io::Result<()>> + Send {
175        async { Err(unsupported()) }
176    }
177    fn remove_network(&self, _name: &str) -> impl Future<Output = io::Result<()>> + Send {
178        async { Err(unsupported()) }
179    }
180    fn connect_network(
181        &self,
182        _container: &str,
183        _network: &str,
184        _aliases: &[String],
185    ) -> impl Future<Output = io::Result<()>> + Send {
186        async { Err(unsupported()) }
187    }
188    fn disconnect_network(
189        &self,
190        _container: &str,
191        _network: &str,
192    ) -> impl Future<Output = io::Result<()>> + Send {
193        async { Err(unsupported()) }
194    }
195    fn remove_volume(&self, _name: &str) -> impl Future<Output = io::Result<()>> + Send {
196        async { Err(unsupported()) }
197    }
198    fn exec_run(
199        &self,
200        _id: &str,
201        _cmd: &[String],
202    ) -> impl Future<Output = io::Result<(i64, Vec<u8>)>> + Send {
203        async { Err(unsupported()) }
204    }
205    fn exec_stream(
206        &self,
207        _id: &str,
208        _opts: &ExecOpts,
209    ) -> impl Future<Output = io::Result<ExecHandle>> + Send {
210        async { Err(unsupported()) }
211    }
212    fn exec_resize(
213        &self,
214        _exec_id: &str,
215        _width: u32,
216        _height: u32,
217    ) -> impl Future<Output = io::Result<()>> + Send {
218        async { Err(unsupported()) }
219    }
220    fn exec_exit_code(&self, _exec_id: &str) -> impl Future<Output = io::Result<i64>> + Send {
221        async { Err(unsupported()) }
222    }
223}