mirror of
https://git.proxmox.com/git/pve-installer
synced 2025-08-11 11:19:16 +00:00
common: options: allow user-supplied domain for network options
Add an optional parameter to allow specifying a domain, which will take precedence over both the DHCP-supplied domain (if any) and the hardcoded default domain. Signed-off-by: Christoph Heiss <c.heiss@proxmox.com>
This commit is contained in:
parent
a51f65e731
commit
b9c06cb119
@ -24,7 +24,7 @@ fn get_network_settings(
|
|||||||
runtime_info: &RuntimeInfo,
|
runtime_info: &RuntimeInfo,
|
||||||
setup_info: &SetupInfo,
|
setup_info: &SetupInfo,
|
||||||
) -> Result<NetworkOptions> {
|
) -> Result<NetworkOptions> {
|
||||||
let mut network_options = NetworkOptions::defaults_from(setup_info, &runtime_info.network);
|
let mut network_options = NetworkOptions::defaults_from(setup_info, &runtime_info.network, None);
|
||||||
|
|
||||||
info!("Setting network configuration");
|
info!("Setting network configuration");
|
||||||
|
|
||||||
|
@ -384,10 +384,18 @@ pub struct NetworkOptions {
|
|||||||
impl NetworkOptions {
|
impl NetworkOptions {
|
||||||
const DEFAULT_DOMAIN: &'static str = "example.invalid";
|
const DEFAULT_DOMAIN: &'static str = "example.invalid";
|
||||||
|
|
||||||
pub fn defaults_from(setup: &SetupInfo, network: &NetworkInfo) -> Self {
|
pub fn defaults_from(
|
||||||
|
setup: &SetupInfo,
|
||||||
|
network: &NetworkInfo,
|
||||||
|
default_domain: Option<&str>,
|
||||||
|
) -> Self {
|
||||||
let mut this = Self {
|
let mut this = Self {
|
||||||
ifname: String::new(),
|
ifname: String::new(),
|
||||||
fqdn: Self::construct_fqdn(network, setup.config.product.default_hostname()),
|
fqdn: Self::construct_fqdn(
|
||||||
|
network,
|
||||||
|
setup.config.product.default_hostname(),
|
||||||
|
default_domain,
|
||||||
|
),
|
||||||
// Safety: The provided mask will always be valid.
|
// Safety: The provided mask will always be valid.
|
||||||
address: CidrAddress::new(Ipv4Addr::UNSPECIFIED, 0).unwrap(),
|
address: CidrAddress::new(Ipv4Addr::UNSPECIFIED, 0).unwrap(),
|
||||||
gateway: Ipv4Addr::UNSPECIFIED.into(),
|
gateway: Ipv4Addr::UNSPECIFIED.into(),
|
||||||
@ -430,14 +438,23 @@ impl NetworkOptions {
|
|||||||
this
|
this
|
||||||
}
|
}
|
||||||
|
|
||||||
fn construct_fqdn(network: &NetworkInfo, default_hostname: &str) -> Fqdn {
|
pub fn construct_fqdn(
|
||||||
|
network: &NetworkInfo,
|
||||||
|
default_hostname: &str,
|
||||||
|
default_domain: Option<&str>,
|
||||||
|
) -> Fqdn {
|
||||||
let hostname = network.hostname.as_deref().unwrap_or(default_hostname);
|
let hostname = network.hostname.as_deref().unwrap_or(default_hostname);
|
||||||
|
|
||||||
let domain = network
|
// First, use the provided default domain if provided. If that is unset,
|
||||||
|
// use the one from the host network configuration, i.e. as and if provided by DHCP.
|
||||||
|
// As last fallback, use [`Self::DEFAULT_DOMAIN`].
|
||||||
|
let domain = default_domain.unwrap_or_else(|| {
|
||||||
|
network
|
||||||
.dns
|
.dns
|
||||||
.domain
|
.domain
|
||||||
.as_deref()
|
.as_deref()
|
||||||
.unwrap_or(Self::DEFAULT_DOMAIN);
|
.unwrap_or(Self::DEFAULT_DOMAIN)
|
||||||
|
});
|
||||||
|
|
||||||
Fqdn::from(&format!("{hostname}.{domain}")).unwrap_or_else(|_| {
|
Fqdn::from(&format!("{hostname}.{domain}")).unwrap_or_else(|_| {
|
||||||
// Safety: This will always result in a valid FQDN, as we control & know
|
// Safety: This will always result in a valid FQDN, as we control & know
|
||||||
@ -480,10 +497,7 @@ mod tests {
|
|||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
use std::net::{IpAddr, Ipv4Addr};
|
use std::net::{IpAddr, Ipv4Addr};
|
||||||
|
|
||||||
#[test]
|
fn mock_setup_network() -> (SetupInfo, NetworkInfo) {
|
||||||
fn network_options_from_setup_network_info() {
|
|
||||||
let setup = SetupInfo::mocked();
|
|
||||||
|
|
||||||
let mut interfaces = BTreeMap::new();
|
let mut interfaces = BTreeMap::new();
|
||||||
interfaces.insert(
|
interfaces.insert(
|
||||||
"eth0".to_owned(),
|
"eth0".to_owned(),
|
||||||
@ -498,7 +512,7 @@ mod tests {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut info = NetworkInfo {
|
let info = NetworkInfo {
|
||||||
dns: Dns {
|
dns: Dns {
|
||||||
domain: Some("bar.com".to_owned()),
|
domain: Some("bar.com".to_owned()),
|
||||||
dns: Vec::new(),
|
dns: Vec::new(),
|
||||||
@ -514,8 +528,15 @@ mod tests {
|
|||||||
hostname: Some("foo".to_owned()),
|
hostname: Some("foo".to_owned()),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
(SetupInfo::mocked(), info)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn network_options_from_setup_network_info() {
|
||||||
|
let (setup, mut info) = mock_setup_network();
|
||||||
|
|
||||||
pretty_assertions::assert_eq!(
|
pretty_assertions::assert_eq!(
|
||||||
NetworkOptions::defaults_from(&setup, &info),
|
NetworkOptions::defaults_from(&setup, &info, None),
|
||||||
NetworkOptions {
|
NetworkOptions {
|
||||||
ifname: "eth0".to_owned(),
|
ifname: "eth0".to_owned(),
|
||||||
fqdn: Fqdn::from("foo.bar.com").unwrap(),
|
fqdn: Fqdn::from("foo.bar.com").unwrap(),
|
||||||
@ -527,7 +548,7 @@ mod tests {
|
|||||||
|
|
||||||
info.hostname = None;
|
info.hostname = None;
|
||||||
pretty_assertions::assert_eq!(
|
pretty_assertions::assert_eq!(
|
||||||
NetworkOptions::defaults_from(&setup, &info),
|
NetworkOptions::defaults_from(&setup, &info, None),
|
||||||
NetworkOptions {
|
NetworkOptions {
|
||||||
ifname: "eth0".to_owned(),
|
ifname: "eth0".to_owned(),
|
||||||
fqdn: Fqdn::from("pve.bar.com").unwrap(),
|
fqdn: Fqdn::from("pve.bar.com").unwrap(),
|
||||||
@ -539,7 +560,7 @@ mod tests {
|
|||||||
|
|
||||||
info.dns.domain = None;
|
info.dns.domain = None;
|
||||||
pretty_assertions::assert_eq!(
|
pretty_assertions::assert_eq!(
|
||||||
NetworkOptions::defaults_from(&setup, &info),
|
NetworkOptions::defaults_from(&setup, &info, None),
|
||||||
NetworkOptions {
|
NetworkOptions {
|
||||||
ifname: "eth0".to_owned(),
|
ifname: "eth0".to_owned(),
|
||||||
fqdn: Fqdn::from("pve.example.invalid").unwrap(),
|
fqdn: Fqdn::from("pve.example.invalid").unwrap(),
|
||||||
@ -551,7 +572,7 @@ mod tests {
|
|||||||
|
|
||||||
info.hostname = Some("foo".to_owned());
|
info.hostname = Some("foo".to_owned());
|
||||||
pretty_assertions::assert_eq!(
|
pretty_assertions::assert_eq!(
|
||||||
NetworkOptions::defaults_from(&setup, &info),
|
NetworkOptions::defaults_from(&setup, &info, None),
|
||||||
NetworkOptions {
|
NetworkOptions {
|
||||||
ifname: "eth0".to_owned(),
|
ifname: "eth0".to_owned(),
|
||||||
fqdn: Fqdn::from("foo.example.invalid").unwrap(),
|
fqdn: Fqdn::from("foo.example.invalid").unwrap(),
|
||||||
@ -561,4 +582,44 @@ mod tests {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn network_options_correctly_handles_user_supplied_default_domain() {
|
||||||
|
let (setup, mut info) = mock_setup_network();
|
||||||
|
|
||||||
|
pretty_assertions::assert_eq!(
|
||||||
|
NetworkOptions::defaults_from(&setup, &info, None),
|
||||||
|
NetworkOptions {
|
||||||
|
ifname: "eth0".to_owned(),
|
||||||
|
fqdn: Fqdn::from("foo.bar.com").unwrap(),
|
||||||
|
address: CidrAddress::new(Ipv4Addr::new(192, 168, 0, 2), 24).unwrap(),
|
||||||
|
gateway: IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1)),
|
||||||
|
dns_server: Ipv4Addr::UNSPECIFIED.into(),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
info.dns.domain = None;
|
||||||
|
pretty_assertions::assert_eq!(
|
||||||
|
NetworkOptions::defaults_from(&setup, &info, Some("custom.local")),
|
||||||
|
NetworkOptions {
|
||||||
|
ifname: "eth0".to_owned(),
|
||||||
|
fqdn: Fqdn::from("foo.custom.local").unwrap(),
|
||||||
|
address: CidrAddress::new(Ipv4Addr::new(192, 168, 0, 2), 24).unwrap(),
|
||||||
|
gateway: IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1)),
|
||||||
|
dns_server: Ipv4Addr::UNSPECIFIED.into(),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
info.dns.domain = Some("some.domain.local".to_owned());
|
||||||
|
pretty_assertions::assert_eq!(
|
||||||
|
NetworkOptions::defaults_from(&setup, &info, Some("custom.local")),
|
||||||
|
NetworkOptions {
|
||||||
|
ifname: "eth0".to_owned(),
|
||||||
|
fqdn: Fqdn::from("foo.custom.local").unwrap(),
|
||||||
|
address: CidrAddress::new(Ipv4Addr::new(192, 168, 0, 2), 24).unwrap(),
|
||||||
|
gateway: IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1)),
|
||||||
|
dns_server: Ipv4Addr::UNSPECIFIED.into(),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -169,7 +169,7 @@ fn main() {
|
|||||||
bootdisk: BootdiskOptions::defaults_from(&runtime_info.disks[0]),
|
bootdisk: BootdiskOptions::defaults_from(&runtime_info.disks[0]),
|
||||||
timezone: TimezoneOptions::defaults_from(&runtime_info, &locales),
|
timezone: TimezoneOptions::defaults_from(&runtime_info, &locales),
|
||||||
password: Default::default(),
|
password: Default::default(),
|
||||||
network: NetworkOptions::defaults_from(&setup_info, &runtime_info.network),
|
network: NetworkOptions::defaults_from(&setup_info, &runtime_info.network, None),
|
||||||
autoreboot: true,
|
autoreboot: true,
|
||||||
},
|
},
|
||||||
setup_info,
|
setup_info,
|
||||||
|
Loading…
Reference in New Issue
Block a user