Skip to content

armgit / kiri

Public Imported from GitHub
0

A small, fast container runtime for macOS, Windows and Linux

Rust

-F (#36)

Precious Okwuf7d2cff89b5d
diff --git a/crates/kiri-win/src/k8s.rs b/crates/kiri-win/src/k8s.rs
index 243ed0f..2cfeb2e 100644
--- a/crates/kiri-win/src/k8s.rs
+++ b/crates/kiri-win/src/k8s.rs
@@ -141,9 +141,43 @@ fn config() -> Config {
config_path().ok().and_then(|p| std::fs::read(p).ok()).and_then(|b| serde_json::from_slice(&b).ok()).unwrap_or_default()
}
-/// What of the settings a node is made with, as its label keeps them.
+/// What of the settings a node is made with, as its label keeps them. The
+/// drives' folder is in it too, so a node made before it had it is made
+/// again at its next start.
fn made_with(cfg: &Config) -> String {
- serde_json::to_string(&(cfg.lan, &cfg.tls_san, cfg.api_port())).unwrap_or_default()
+ serde_json::to_string(&(cfg.lan, &cfg.tls_san, cfg.api_port(), drives())).unwrap_or_default()
+}
+
+/// Where WSL puts this PC's drives (`/mnt`, unless `[automount] root` in
+/// the engine distro's wsl.conf moves it), found once. The node sees it at
+/// the same path, so a pod's `hostPath: /mnt/d/...` is drive D, as it is
+/// with k3s in WSL itself (Rancher Desktop). The bind is recursive: every
+/// drive WSL has mounted when the node starts comes with it.
+fn drives() -> Option<String> {
+ static ROOT: std::sync::OnceLock<Option<String>> = std::sync::OnceLock::new();
+ ROOT.get_or_init(|| {
+ let mut wsl = crate::win::tool("wsl.exe");
+ std::os::windows::process::CommandExt::creation_flags(&mut wsl, 0x0800_0000); // CREATE_NO_WINDOW
+ if let Some(d) = crate::engine::distro() {
+ wsl.args(["-d", &d]);
+ }
+ let out = wsl.args(["--", "wslpath", "-a", "C:/"]).stdin(std::process::Stdio::null()).output().ok()?;
+ drives_root(&String::from_utf8_lossy(&out.stdout))
+ })
+ .clone()
+}
+
+/// The drives' folder from `wslpath -a C:/` (`/mnt/c/` → `/mnt`); none
+/// when drives sit at the top (`/c/`), which isn't a folder to bind.
+fn drives_root(c: &str) -> Option<String> {
+ let c = c.trim().trim_end_matches('/');
+ let (root, letter) = c.rsplit_once('/')?;
+ (letter.eq_ignore_ascii_case("c") && root.starts_with('/') && root.len() > 1 && !root.contains(['\\', ':'])).then(|| root.to_owned())
+}
+
+/// The node's binds: its state, then the drives' folder.
+fn binds() -> Vec<String> {
+ std::iter::once(format!("{VOLUME}:/var/lib/rancher/k3s")).chain(drives().map(|d| format!("{d}:{d}"))).collect()
}
/// The port a node container publishes its API server on: its live binding
@@ -425,7 +459,7 @@ fn start(say: &dyn Fn(&str)) -> io::Result<()> {
"HostConfig": {
"Privileged": true,
"Tmpfs": {"/run": "", "/var/run": ""},
- "Binds": [format!("{VOLUME}:/var/lib/rancher/k3s")],
+ "Binds": binds(),
"PortBindings": {"6443/tcp": [{"HostIp": if cfg.lan { "0.0.0.0" } else { "127.0.0.1" }, "HostPort": port.to_string()}]},
"RestartPolicy": {"Name": "unless-stopped"},
},
@@ -707,6 +741,24 @@ fn stop_logs() {
#[cfg(test)]
mod tests {
+ #[test]
+ fn the_drives_folder_comes_from_wslpath() {
+ assert_eq!(super::drives_root("/mnt/c/\n").as_deref(), Some("/mnt"));
+ assert_eq!(super::drives_root("/windir/c/").as_deref(), Some("/windir"));
+ for none in ["/c/", "", "C:/", "wslpath: error", "/mnt/d/"] {
+ assert_eq!(super::drives_root(none), None, "{none}");
+ }
+ }
+
+ #[test]
+ fn the_node_sees_the_drives_at_their_own_paths() {
+ let b = super::binds();
+ assert_eq!(b[0], "kiri-k8s:/var/lib/rancher/k3s");
+ if let Some(d) = super::drives() {
+ assert!(b.contains(&format!("{d}:{d}")), "{b:?}");
+ }
+ }
+
#[test]
fn the_port_comes_from_the_binding_then_the_label() {
let running = serde_json::json!({"NetworkSettings": {"Ports": {"6443/tcp": [{"HostIp": "127.0.0.1", "HostPort": "6444"}]}}, "Config": {"Labels": {"dev.kiri.k8s.port": "6450"}}});
diff --git a/crates/kiri/src/k8s.rs b/crates/kiri/src/k8s.rs
index 0ff3b07..263a12a 100644
--- a/crates/kiri/src/k8s.rs
+++ b/crates/kiri/src/k8s.rs
@@ -156,9 +156,27 @@ fn set(args: &[String]) -> io::Result<()> {
/// The label that keeps the settings a node was made with.
const CONFIG_LABEL: &str = "dev.kiri.k8s.config";
-/// What of the settings a node is made with, as its label keeps them.
+/// What of the settings a node is made with, as its label keeps them. The
+/// host folders are in it too, so a node made before it had them (or before
+/// one existed) is made again at its next start.
fn made_with(cfg: &Config) -> String {
- serde_json::to_string(&(cfg.lan, &cfg.tls_san, cfg.api_port())).unwrap_or_default()
+ serde_json::to_string(&(cfg.lan, &cfg.tls_san, cfg.api_port(), host_folders())).unwrap_or_default()
+}
+
+/// The folders of this computer the node sees at their own paths, so a pod's
+/// `hostPath` names a file here as it would with k3s on the host itself
+/// (Rancher Desktop, k3s): the Mac's /Users and /Volumes, a Linux host's
+/// /home, /mnt and /media. Binds are recursive, so what is mounted under
+/// them when the node starts comes too.
+const HOST_FOLDERS: &[&str] = if cfg!(target_os = "macos") { &["/Users", "/Volumes"] } else { &["/home", "/mnt", "/media"] };
+
+fn host_folders() -> Vec<&'static str> {
+ HOST_FOLDERS.iter().copied().filter(|d| std::path::Path::new(d).is_dir()).collect()
+}
+
+/// The node's binds: its state, then the host folders.
+fn binds() -> Vec<String> {
+ std::iter::once(format!("{VOLUME}:/var/lib/rancher/k3s")).chain(host_folders().into_iter().map(|d| format!("{d}:{d}"))).collect()
}
fn ensure_daemon() -> io::Result<()> {
@@ -262,7 +280,7 @@ fn start(use_context: bool) -> io::Result<()> {
"HostConfig": {
"Privileged": true,
"Tmpfs": {"/run": "", "/var/run": ""},
- "Binds": [format!("{VOLUME}:/var/lib/rancher/k3s")],
+ "Binds": binds(),
"PortBindings": {"6443/tcp": [{"HostIp": if cfg.lan { "0.0.0.0" } else { "127.0.0.1" }, "HostPort": port.to_string()}]},
"RestartPolicy": {"Name": "unless-stopped"},
},
@@ -511,6 +529,17 @@ mod tests {
assert_eq!(super::bound_port(&serde_json::json!({})), None);
}
+ #[test]
+ fn the_node_sees_the_host_folders_at_their_own_paths() {
+ let b = super::binds();
+ assert_eq!(b[0], "kiri-k8s:/var/lib/rancher/k3s");
+ let home = if cfg!(target_os = "macos") { "/Users" } else { "/home" };
+ assert!(b.contains(&format!("{home}:{home}")), "{b:?}");
+ // A node made before the folders were bound is made again.
+ let old = serde_json::to_string(&(false, Vec::<String>::new(), 6443u16)).unwrap();
+ assert_ne!(super::made_with(&super::Config::default()), old);
+ }
+
#[test]
fn api_ports_are_unprivileged() {
assert_eq!(super::valid_port("6444"), Some(6444));
diff --git a/docs/releases/0.1.18.md b/docs/releases/0.1.18.md
index 9fe4877..06d6a53 100644
--- a/docs/releases/0.1.18.md
+++ b/docs/releases/0.1.18.md
@@ -2,6 +2,7 @@
- **A terminal in the window.** Click Terminal in the status bar, or press Control and the backtick key (F4 on Linux). Your own shell with tabs, splits, find and pinned tabs, and Kiri's `docker` first on the PATH.
## Fixed and improved
+- **Kubernetes pods can use your own folders.** A pod's `hostPath` reaches your drives at `/mnt/c`, `/mnt/d` on Windows, `/Users` on the Mac and `/home` on Linux. Stop and start the cluster once to get it.
- **Linux: Kubernetes no longer breaks your terminals.** Privileged containers get their own `/dev`, as on Docker.
- **Containers keep their connections** while others start and stop.
- **Ports below 1024 and `ping` work without root,** as on Docker.