Skip to content
This repository has been archived by the owner on Jun 12, 2023. It is now read-only.

Refactor to stronger typing and consolidate CONSTs #46

Merged
merged 1 commit into from Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Expand Up @@ -35,5 +35,9 @@ nv = { package = "netavark", version = "1.4"}
rtnetlink = "0.11.0"
ipnet = { version = "2", features = ["serde"] }

[dev-dependencies]
once_cell = "1.8.0"
rand = "0.8.5"

[build-dependencies]
tonic-build = "0.8"
29 changes: 11 additions & 18 deletions src/cache.rs
Expand Up @@ -3,10 +3,7 @@ use log::debug;
use std::collections::HashMap;
use std::fs::OpenOptions;
use std::io;
use std::path::{Path, PathBuf};

/// Path to the lease json cache
pub const DEFAULT_CACHE_DIR: &str = "/var/tmp/";
use std::path::PathBuf;

/// LeaseCache holds the locked memory map of the mac address to a vector of leases - for multi homing
/// It also stores a locked path buffer to change the FS cache
Expand All @@ -29,15 +26,20 @@ impl LeaseCache {
/// On success a new lease cache instance will be returned. On failure an IO error will
/// be returned.
/// This likely means it could not find the file directory
pub fn new(dir: String) -> Result<LeaseCache, io::Error> {
let fq_path = Path::new(&dir).join("nv-leases");
debug!("lease cache file: {:?}", fq_path.to_str().unwrap_or(""));
pub fn new(file_path: PathBuf) -> Result<LeaseCache, io::Error> {
debug!(
"lease cache file: {:?}",
file_path.to_str().unwrap_or_default()
);

OpenOptions::new().write(true).create(true).open(&fq_path)?;
OpenOptions::new()
.write(true)
.create(true)
.open(&file_path)?;

Ok(LeaseCache {
mem: HashMap::new(),
path: fq_path,
path: file_path,
})
}

Expand Down Expand Up @@ -148,15 +150,6 @@ impl LeaseCache {
}
}

impl Default for LeaseCache {
fn default() -> Self {
LeaseCache {
mem: HashMap::<String, Vec<NetavarkLease>>::new(),
path: PathBuf::from(DEFAULT_CACHE_DIR),
}
}
}

#[cfg(test)]
mod cache_tests {
#[test]
Expand Down
4 changes: 2 additions & 2 deletions src/client/client.rs
Expand Up @@ -4,7 +4,7 @@ use std::process;
use tonic::{Code, Status};

use netavark_proxy::g_rpc::{Lease, NetworkConfig};
use netavark_proxy::{DEFAULT_NETWORK_CONFIG, DEFAULT_UDS_PATH};
use netavark_proxy::proxy_conf::{DEFAULT_NETWORK_CONFIG, DEFAULT_UDS_PATH};

pub mod commands;

Expand Down Expand Up @@ -58,7 +58,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
Ok(r) => r,
Err(e) => {
eprintln!("Error: {}", e.message());
eprintln!("Error: {}", e);
eprintln!("Error: {e}");
process_failure(e)
}
};
Expand Down
4 changes: 2 additions & 2 deletions src/ip.rs
Expand Up @@ -82,7 +82,7 @@ fn handle_gws(g: Vec<String>, netmask: &str) -> Result<Vec<IpNet>, ProxyError> {
};
let gw = match IpNet::new(IpAddr::from(ip), prefix as u8) {
Ok(r) => r,
Err(e) => return Err(ProxyError::new(format!("{}:'{}'", e, route))),
Err(e) => return Err(ProxyError::new(format!("{e}:'{route}'"))),
};
gws.push(gw);
}
Expand Down Expand Up @@ -116,7 +116,7 @@ impl Address<Ipv4Addr> for MacVLAN {
let address = match IpAddr::from_str(&l.yiaddr) {
Ok(a) => a,
Err(e) => {
return Err(ProxyError::new(format!("bad address: {}", e)));
return Err(ProxyError::new(format!("bad address: {e}")));
}
};
let gateways = match handle_gws(l.gateways.clone(), &l.subnet_mask) {
Expand Down
13 changes: 2 additions & 11 deletions src/lib.rs
Expand Up @@ -4,6 +4,7 @@ use std::error::Error;
pub mod cache;
pub mod dhcp_service;
pub mod ip;
pub mod proxy_conf;
pub mod types;

use crate::g_rpc::netavark_proxy_client::NetavarkProxyClient;
Expand All @@ -18,16 +19,6 @@ use tonic::transport::{Channel, Endpoint};
use tonic::{Request, Status};
use tower::service_fn;

// TODO these constant destinations are not final.
// Default UDS path for gRPC to communicate on.
pub const DEFAULT_UDS_PATH: &str = "/run/podman/nv-proxy.sock";
// Default configuration directory.
pub const DEFAULT_CONFIG_DIR: &str = "";
// Default Network configuration path
pub const DEFAULT_NETWORK_CONFIG: &str = "/dev/stdin";
// Default epoll wait time before dhcp socket times out
pub const DEFAULT_TIMEOUT: isize = 8;

#[allow(clippy::unwrap_used)]
pub mod g_rpc {
include!("../proto-build/netavark_proxy.rs");
Expand Down Expand Up @@ -160,7 +151,7 @@ pub mod g_rpc {
use std::str::FromStr;
let mut ips: Vec<std::net::Ipv4Addr> = Vec::new();
for i in 0..5 {
let ip = format!("10.1.{}.1", i);
let ip = format!("10.1.{i}.1");
let ipv4 = std::net::Ipv4Addr::from_str(&ip).expect("failed hard");
ips.push(ipv4);
}
Expand Down