Skip to content

Commit 39d5db0

Browse files
committed
feat(config): unify address conversions
Some configuration objects related to addresses contain a mask length which is unnecessary. Add methods to attempt to convert those anyway, ignoring the mask. Signed-off-by: Fredi Raspall <[email protected]>
1 parent dd068ce commit 39d5db0

File tree

5 files changed

+48
-20
lines changed

5 files changed

+48
-20
lines changed

config/src/converters/grpc/gwgroups.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// Copyright Open Network Fabric Authors
33

4+
use crate::converters::strings::parse_address;
45
use crate::external::gwgroup::{GwGroup, GwGroupMember};
56
use gateway_config::config as gateway_config;
6-
use std::net::IpAddr;
7-
use std::str::FromStr;
87

98
impl TryFrom<&gateway_config::GatewayGroupMember> for GwGroupMember {
109
type Error = String;
1110

1211
fn try_from(value: &gateway_config::GatewayGroupMember) -> Result<Self, Self::Error> {
13-
let ipaddress = IpAddr::from_str(&value.ipaddress)
14-
.map_err(|e| format!("Bad ip address '{}': {e}", &value.ipaddress))?;
15-
Ok(GwGroupMember::new(&value.name, value.priority, ipaddress))
12+
let address = parse_address(&value.ipaddress)
13+
.map_err(|e| format!("Bad ip address '{}': {e}", value.ipaddress))?;
14+
Ok(GwGroupMember::new(&value.name, value.priority, address))
1615
}
1716
}
1817
impl TryFrom<&GwGroupMember> for gateway_config::GatewayGroupMember {

config/src/converters/k8s/config/gwgroups.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Copyright Open Network Fabric Authors
33

44
use crate::converters::k8s::FromK8sConversionError;
5+
use crate::converters::strings::parse_address;
56
use k8s_intf::gateway_agent_crd::{GatewayAgentGroupsMembers, GatewayAgentSpec};
6-
use std::net::IpAddr;
77

88
use crate::external::gwgroup::{GwGroup, GwGroupMember, GwGroupTable};
99

@@ -22,13 +22,11 @@ impl TryFrom<&GatewayAgentGroupsMembers> for GwGroupMember {
2222
.try_into()
2323
.map_err(|e| Self::Error::ParseError(format!("Bad priority value: {e}")))?;
2424

25-
let ipaddress = value.vtep_ip.as_ref().ok_or_else(|| {
25+
let address = value.vtep_ip.as_ref().ok_or_else(|| {
2626
Self::Error::MissingData("Gateway group member ip address".to_string())
2727
})?;
28-
29-
let ipaddress = ipaddress
30-
.parse::<IpAddr>()
31-
.map_err(|e| Self::Error::ParseError(format!("Invalid ip address {ipaddress}: {e}")))?;
28+
let ipaddress = parse_address(address)
29+
.map_err(|e| Self::Error::ParseError(format!("Invalid ip address {address}: {e}")))?;
3230

3331
Ok(Self {
3432
name: name.clone(),

config/src/converters/k8s/config/underlay.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// Copyright Open Network Fabric Authors
33

4+
use crate::converters::strings::parse_address_v4;
45
use std::net::IpAddr;
56

6-
use ipnet::Ipv4Net;
77
use k8s_intf::gateway_agent_crd::GatewayAgentGateway;
88
use lpm::prefix::{Prefix, PrefixString};
99
use net::eth::mac::SourceMac;
@@ -98,21 +98,17 @@ fn add_bgp_config(
9898
"Gateway protocol IP not specified".to_string(),
9999
))?;
100100

101-
let router_id = protocol_ip
102-
.parse::<Ipv4Net>()
103-
.map_err(|e| {
104-
FromK8sConversionError::ParseError(format!(
105-
"Invalid IPv4 protocol IP {protocol_ip}: {e}"
106-
))
107-
})?
108-
.addr();
101+
let router_id = parse_address_v4(protocol_ip).map_err(|e| {
102+
FromK8sConversionError::ParseError(format!("Invalid IPv4 protocol IP {protocol_ip}: {e}"))
103+
})?;
109104

110105
let vtep_ip_raw = gateway
111106
.vtep_ip
112107
.as_ref()
113108
.ok_or(FromK8sConversionError::MissingData(
114109
"Gateway VTEP IP not specified".to_string(),
115110
))?;
111+
116112
let vtep_prefix = Prefix::try_from(PrefixString(vtep_ip_raw)).map_err(|e| {
117113
FromK8sConversionError::ParseError(format!("Invalid VTEP IP {vtep_ip_raw}: {e}"))
118114
})?;

config/src/converters/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
66
pub mod grpc;
77
pub mod k8s;
8+
pub mod strings;

config/src/converters/strings.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// Copyright Open Network Fabric Authors
3+
4+
//! Commonly used conversions from strings
5+
6+
use ipnet::{AddrParseError, IpNet, Ipv4Net};
7+
use std::net::{IpAddr, Ipv4Addr};
8+
9+
/// Parse a string containing an IP address. If the string contains a mask
10+
/// length, ignore it. On Success, returns an `IpAddr`.
11+
///
12+
/// # Errors
13+
/// This function returns `AddrParseError` if the address could not be parsed.
14+
pub fn parse_address(input: &str) -> Result<IpAddr, AddrParseError> {
15+
match input.parse::<IpAddr>() {
16+
Ok(address) => Ok(address),
17+
Err(_) => match input.parse::<IpNet>()? {
18+
IpNet::V4(a) => Ok(a.addr().into()),
19+
IpNet::V6(a) => Ok(a.addr().into()),
20+
},
21+
}
22+
}
23+
24+
/// Parse a string containing an IPv4 address. If the string contains a mask
25+
/// length, ignore it. On Success, returns an `Ipv4Addr`.
26+
///
27+
/// # Errors
28+
/// This function returns `AddrParseError` if the address could not be parsed.
29+
pub fn parse_address_v4(input: &str) -> Result<Ipv4Addr, AddrParseError> {
30+
match input.parse::<Ipv4Addr>() {
31+
Ok(address) => Ok(address),
32+
Err(_) => Ok(input.parse::<Ipv4Net>()?.addr()),
33+
}
34+
}

0 commit comments

Comments
 (0)