mirror of
https://github.com/faas-rs/faasd-in-rust.git
synced 2025-06-21 14:23:28 +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:
@ -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
|
||||
|
Reference in New Issue
Block a user