From 6ecde71c52b8d150eb5f16fff5359e665fe40437 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=81=AB=E8=8A=B1?= Date: Wed, 16 Apr 2025 11:35:51 +0800 Subject: [PATCH] =?UTF-8?q?Feat(netns):=20=E6=B7=BB=E5=8A=A0netns-rs?= =?UTF-8?q?=E6=9D=A5=E7=AE=A1=E7=90=86=E7=BD=91=E7=BB=9C=E5=91=BD=E5=90=8D?= =?UTF-8?q?=E7=A9=BA=E9=97=B4=20(#69)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 添加netns-rs来管理网络命名空间 * fix: cargo hakari generate --------- Co-authored-by: Samuka007 --- Cargo.lock | 33 +++++++++++++++++++++++ crates/cni/Cargo.toml | 2 +- crates/cni/src/lib.rs | 42 +++++++++++++++++++---------- crates/my-workspace-hack/Cargo.toml | 8 +++--- 4 files changed, 66 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8db840c..590e5d6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -521,6 +521,7 @@ dependencies = [ "lazy_static", "log", "my-workspace-hack", + "netns-rs", "serde_json", ] @@ -1625,6 +1626,15 @@ version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" +[[package]] +name = "memoffset" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aa361d4faea93603064a027415f07bd8e1d5c88c9fbf68bf56a285428fd79ce" +dependencies = [ + "autocfg", +] + [[package]] name = "mime" version = "0.3.17" @@ -1703,6 +1713,29 @@ dependencies = [ "tempfile", ] +[[package]] +name = "netns-rs" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23541694f1d7d18cd1a0da3a1352a6ea48b01cbb4a8e7a6e547963823fd5276e" +dependencies = [ + "nix", + "thiserror 1.0.69", +] + +[[package]] +name = "nix" +version = "0.23.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f3790c00a0150112de0f4cd161e3d7fc4b2d8a5542ffc35f099a2562aecb35c" +dependencies = [ + "bitflags 1.3.2", + "cc", + "cfg-if", + "libc", + "memoffset", +] + [[package]] name = "nom" version = "5.1.3" diff --git a/crates/cni/Cargo.toml b/crates/cni/Cargo.toml index 6d156bd..d45cbf6 100644 --- a/crates/cni/Cargo.toml +++ b/crates/cni/Cargo.toml @@ -10,5 +10,5 @@ serde_json = "1.0" my-workspace-hack = { version = "0.1", path = "../my-workspace-hack" } log = "0.4.27" dotenv = "0.15.0" - +netns-rs = "0.1.0" lazy_static = "1.4.0" diff --git a/crates/cni/src/lib.rs b/crates/cni/src/lib.rs index 59ba381..4150ad1 100644 --- a/crates/cni/src/lib.rs +++ b/crates/cni/src/lib.rs @@ -1,6 +1,7 @@ type Err = Box; use lazy_static::lazy_static; +use netns_rs::NetNs; use serde_json::Value; use std::{ fmt::Error, @@ -85,15 +86,7 @@ pub fn create_cni_network(cid: String, ns: String) -> Result<(String, String), E let path = get_path(netns.as_str()); let mut ip = String::new(); - let output = std::process::Command::new("ip") - .arg("netns") - .arg("add") - .arg(&netns) - .output()?; - - if !output.status.success() { - return Err(Box::new(Error)); - } + create_netns(&netns); let bin = CNI_BIN_DIR.as_str(); let cnitool = CNI_TOOL.as_str(); @@ -146,11 +139,32 @@ pub fn delete_cni_network(ns: &str, cid: &str) { .arg(&path) .env("CNI_PATH", bin) .output(); - let _output = std::process::Command::new("ip") - .arg("netns") - .arg("delete") - .arg(&netns) - .output(); + delete_netns(&netns); +} + +fn create_netns(namespace_name: &str) { + match NetNs::new(namespace_name) { + Ok(ns) => { + log::info!("Created netns: {}", ns); + } + Err(e) => { + log::error!("Error creating netns: {}", e); + } + } +} + +fn delete_netns(namespace_name: &str) { + match NetNs::get(namespace_name) { + Ok(ns) => { + ns.remove() + .map_err(|e| log::error!("Error deleting netns: {}", e)) + .unwrap(); + log::info!("Deleted netns: {}", namespace_name); + } + Err(e) => { + log::error!("Error getting netns: {}, NotFound", e); + } + } } fn dir_exists(dirname: &Path) -> bool { diff --git a/crates/my-workspace-hack/Cargo.toml b/crates/my-workspace-hack/Cargo.toml index c30d89f..1af1674 100644 --- a/crates/my-workspace-hack/Cargo.toml +++ b/crates/my-workspace-hack/Cargo.toml @@ -51,18 +51,18 @@ tracing-core = { version = "0.1", default-features = false, features = ["std"] } [target.x86_64-unknown-linux-gnu.dependencies] getrandom = { version = "0.2", default-features = false, features = ["std"] } -libc = { version = "0.2" } +libc = { version = "0.2", features = ["extra_traits"] } [target.x86_64-unknown-linux-gnu.build-dependencies] getrandom = { version = "0.2", default-features = false, features = ["std"] } -libc = { version = "0.2" } +libc = { version = "0.2", features = ["extra_traits"] } [target.aarch64-unknown-linux-gnu.dependencies] getrandom = { version = "0.2", default-features = false, features = ["std"] } -libc = { version = "0.2" } +libc = { version = "0.2", features = ["extra_traits"] } [target.aarch64-unknown-linux-gnu.build-dependencies] getrandom = { version = "0.2", default-features = false, features = ["std"] } -libc = { version = "0.2" } +libc = { version = "0.2", features = ["extra_traits"] } ### END HAKARI SECTION