Feat(netns): 添加netns-rs来管理网络命名空间 (#69)

* 添加netns-rs来管理网络命名空间

* fix: cargo hakari generate

---------

Co-authored-by: Samuka007 <samuka007@dragonos.org>
This commit is contained in:
火花 2025-04-16 11:35:51 +08:00 committed by GitHub
parent 8c4107a8b4
commit 6ecde71c52
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 66 additions and 19 deletions

33
Cargo.lock generated
View File

@ -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"

View File

@ -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"

View File

@ -1,6 +1,7 @@
type Err = Box<dyn std::error::Error>;
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 {

View File

@ -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