Skip to content

Commit

Permalink
parse_option(): return option instead of default
Browse files Browse the repository at this point in the history
While for these callers we can set defaults there are some options were
we actually want to know if it was set or not, thus return an Option<T>.

It will be required for the bclim type as it is not clear what the
default is so we do not want to set it at all if the option was not set.

Signed-off-by: Paul Holzinger <[email protected]>
  • Loading branch information
Luap99 committed May 24, 2023
1 parent bd41190 commit 3d3563b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 31 deletions.
14 changes: 7 additions & 7 deletions src/network/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,12 @@ impl driver::NetworkDriver for Bridge<'_> {
}
let ipam = get_ipam_addresses(self.info.per_network_opts, self.info.network)?;

let mtu: u32 = parse_option(&self.info.network.options, OPTION_MTU, 0)?;
let isolate: bool = parse_option(&self.info.network.options, OPTION_ISOLATE, false)?;
let metric: u32 = parse_option(&self.info.network.options, OPTION_METRIC, 100)?;
let mtu: u32 = parse_option(&self.info.network.options, OPTION_MTU)?.unwrap_or(0);
let isolate: bool =
parse_option(&self.info.network.options, OPTION_ISOLATE)?.unwrap_or(false);
let metric: u32 = parse_option(&self.info.network.options, OPTION_METRIC)?.unwrap_or(100);
let no_default_route: bool =
parse_option(&self.info.network.options, OPTION_NO_DEFAULT_ROUTE, false)?;
parse_option(&self.info.network.options, OPTION_NO_DEFAULT_ROUTE)?.unwrap_or(false);

let static_mac = match &self.info.per_network_opts.static_mac {
Some(mac) => Some(CoreUtils::decode_address_from_hex(mac)?),
Expand Down Expand Up @@ -389,9 +390,8 @@ impl<'a> Bridge<'a> {
Some(d) => (&d.ipam.container_addresses, &d.ipam.nameservers, d.isolate),
None => {
// options are not yet parsed
let isolate = match parse_option(&self.info.network.options, OPTION_ISOLATE, false)
{
Ok(i) => i,
let isolate: bool = match parse_option(&self.info.network.options, OPTION_ISOLATE) {
Ok(i) => i.unwrap_or(false),
Err(e) => {
// just log we still try to do as much as possible for cleanup
error!("failed to parse {} option: {}", OPTION_ISOLATE, e);
Expand Down
34 changes: 16 additions & 18 deletions src/network/core_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,10 @@ pub fn get_netavark_dns_port() -> Result<u16, NetavarkError> {
pub fn parse_option<T>(
opts: &Option<HashMap<String, String>>,
name: &str,
default: T,
) -> NetavarkResult<T>
) -> NetavarkResult<Option<T>>
where
T: FromStr,
<T as FromStr>::Err: Display,
T: Default,
{
let val = match opts.as_ref().and_then(|map| map.get(name)) {
Some(val) => match val.parse::<T>() {
Expand All @@ -64,10 +62,10 @@ where
)));
}
},
// if no option is set return the default value
None => default,
// if no option is set return None
None => return Ok(None),
};
Ok(val)
Ok(Some(val))
}

pub fn get_ipam_addresses<'a>(
Expand Down Expand Up @@ -232,29 +230,29 @@ impl CoreUtils {
Ok(result)
}

pub fn get_macvlan_mode_from_string(mode: &str) -> NetavarkResult<u32> {
pub fn get_macvlan_mode_from_string(mode: Option<&str>) -> NetavarkResult<u32> {
match mode {
// default to bridge when unset
"" | "bridge" => Ok(MACVLAN_MODE_BRIDGE),
"private" => Ok(MACVLAN_MODE_PRIVATE),
"vepa" => Ok(MACVLAN_MODE_VEPA),
"passthru" => Ok(MACVLAN_MODE_PASSTHRU),
"source" => Ok(MACVLAN_MODE_SOURCE),
None | Some("") | Some("bridge") => Ok(MACVLAN_MODE_BRIDGE),
Some("private") => Ok(MACVLAN_MODE_PRIVATE),
Some("vepa") => Ok(MACVLAN_MODE_VEPA),
Some("passthru") => Ok(MACVLAN_MODE_PASSTHRU),
Some("source") => Ok(MACVLAN_MODE_SOURCE),
// default to bridge
name => Err(NetavarkError::msg(format!(
Some(name) => Err(NetavarkError::msg(format!(
"invalid macvlan mode \"{}\"",
name
))),
}
}

pub fn get_ipvlan_mode_from_string(mode: &str) -> NetavarkResult<u16> {
pub fn get_ipvlan_mode_from_string(mode: Option<&str>) -> NetavarkResult<u16> {
match mode {
// default to l2 when unset
"" | "l2" => Ok(IPVLAN_MODE_L2),
"l3" => Ok(IPVLAN_MODE_L3),
"l3s" => Ok(IPVLAN_MODE_L3S),
name => Err(NetavarkError::msg(format!(
None | Some("") | Some("l2") => Ok(IPVLAN_MODE_L2),
Some("l3") => Ok(IPVLAN_MODE_L3),
Some("l3s") => Ok(IPVLAN_MODE_L3S),
Some(name) => Err(NetavarkError::msg(format!(
"invalid ipvlan mode \"{}\"",
name
))),
Expand Down
12 changes: 6 additions & 6 deletions src/network/vlan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ impl driver::NetworkDriver for Vlan<'_> {
return Err(NetavarkError::msg(NO_CONTAINER_INTERFACE_ERROR));
}

let mode = parse_option(&self.info.network.options, OPTION_MODE, String::default())?;
let mode: Option<String> = parse_option(&self.info.network.options, OPTION_MODE)?;

let mut ipam = get_ipam_addresses(self.info.per_network_opts, self.info.network)?;

let mtu = parse_option(&self.info.network.options, OPTION_MTU, 0)?;
let metric = parse_option(&self.info.network.options, OPTION_METRIC, 100)?;
let mtu = parse_option(&self.info.network.options, OPTION_MTU)?.unwrap_or(0);
let metric = parse_option(&self.info.network.options, OPTION_METRIC)?.unwrap_or(100);
let no_default_route: bool =
parse_option(&self.info.network.options, OPTION_NO_DEFAULT_ROUTE, false)?;
parse_option(&self.info.network.options, OPTION_NO_DEFAULT_ROUTE)?.unwrap_or(false);

// Remove gateways when marked as internal network
if self.info.network.internal {
Expand All @@ -115,10 +115,10 @@ impl driver::NetworkDriver for Vlan<'_> {
metric: Some(metric),
kind: match self.info.network.driver.as_str() {
super::constants::DRIVER_IPVLAN => KindData::IpVlan {
mode: CoreUtils::get_ipvlan_mode_from_string(&mode)?,
mode: CoreUtils::get_ipvlan_mode_from_string(mode.as_deref())?,
},
super::constants::DRIVER_MACVLAN => KindData::MacVlan {
mode: CoreUtils::get_macvlan_mode_from_string(&mode)?,
mode: CoreUtils::get_macvlan_mode_from_string(mode.as_deref())?,
mac_address: match &self.info.per_network_opts.static_mac {
Some(mac) => Some(CoreUtils::decode_address_from_hex(mac)?),
None => None,
Expand Down

0 comments on commit 3d3563b

Please sign in to comment.