mirror of
https://github.com/faas-rs/faasd-in-rust.git
synced 2025-06-08 15:56:48 +00:00
Feat(netns): 添加netns-rs来管理网络命名空间 (#69)
* 添加netns-rs来管理网络命名空间 * fix: cargo hakari generate --------- Co-authored-by: Samuka007 <samuka007@dragonos.org>
This commit is contained in:
parent
8c4107a8b4
commit
6ecde71c52
33
Cargo.lock
generated
33
Cargo.lock
generated
@ -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"
|
||||
|
@ -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"
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user