mirror of
https://git.proxmox.com/git/pve-installer
synced 2025-04-28 04:44:59 +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,
|
||||
setup_info: &SetupInfo,
|
||||
) -> 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");
|
||||
|
||||
|
@ -384,10 +384,18 @@ pub struct NetworkOptions {
|
||||
impl NetworkOptions {
|
||||
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 {
|
||||
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.
|
||||
address: CidrAddress::new(Ipv4Addr::UNSPECIFIED, 0).unwrap(),
|
||||
gateway: Ipv4Addr::UNSPECIFIED.into(),
|
||||
@ -430,14 +438,23 @@ impl NetworkOptions {
|
||||
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 domain = network
|
||||
.dns
|
||||
.domain
|
||||
.as_deref()
|
||||
.unwrap_or(Self::DEFAULT_DOMAIN);
|
||||
// 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
|
||||
.domain
|
||||
.as_deref()
|
||||
.unwrap_or(Self::DEFAULT_DOMAIN)
|
||||
});
|
||||
|
||||
Fqdn::from(&format!("{hostname}.{domain}")).unwrap_or_else(|_| {
|
||||
// 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::net::{IpAddr, Ipv4Addr};
|
||||
|
||||
#[test]
|
||||
fn network_options_from_setup_network_info() {
|
||||
let setup = SetupInfo::mocked();
|
||||
|
||||
fn mock_setup_network() -> (SetupInfo, NetworkInfo) {
|
||||
let mut interfaces = BTreeMap::new();
|
||||
interfaces.insert(
|
||||
"eth0".to_owned(),
|
||||
@ -498,7 +512,7 @@ mod tests {
|
||||
},
|
||||
);
|
||||
|
||||
let mut info = NetworkInfo {
|
||||
let info = NetworkInfo {
|
||||
dns: Dns {
|
||||
domain: Some("bar.com".to_owned()),
|
||||
dns: Vec::new(),
|
||||
@ -514,8 +528,15 @@ mod tests {
|
||||
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!(
|
||||
NetworkOptions::defaults_from(&setup, &info),
|
||||
NetworkOptions::defaults_from(&setup, &info, None),
|
||||
NetworkOptions {
|
||||
ifname: "eth0".to_owned(),
|
||||
fqdn: Fqdn::from("foo.bar.com").unwrap(),
|
||||
@ -527,7 +548,7 @@ mod tests {
|
||||
|
||||
info.hostname = None;
|
||||
pretty_assertions::assert_eq!(
|
||||
NetworkOptions::defaults_from(&setup, &info),
|
||||
NetworkOptions::defaults_from(&setup, &info, None),
|
||||
NetworkOptions {
|
||||
ifname: "eth0".to_owned(),
|
||||
fqdn: Fqdn::from("pve.bar.com").unwrap(),
|
||||
@ -539,7 +560,7 @@ mod tests {
|
||||
|
||||
info.dns.domain = None;
|
||||
pretty_assertions::assert_eq!(
|
||||
NetworkOptions::defaults_from(&setup, &info),
|
||||
NetworkOptions::defaults_from(&setup, &info, None),
|
||||
NetworkOptions {
|
||||
ifname: "eth0".to_owned(),
|
||||
fqdn: Fqdn::from("pve.example.invalid").unwrap(),
|
||||
@ -551,7 +572,7 @@ mod tests {
|
||||
|
||||
info.hostname = Some("foo".to_owned());
|
||||
pretty_assertions::assert_eq!(
|
||||
NetworkOptions::defaults_from(&setup, &info),
|
||||
NetworkOptions::defaults_from(&setup, &info, None),
|
||||
NetworkOptions {
|
||||
ifname: "eth0".to_owned(),
|
||||
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]),
|
||||
timezone: TimezoneOptions::defaults_from(&runtime_info, &locales),
|
||||
password: Default::default(),
|
||||
network: NetworkOptions::defaults_from(&setup_info, &runtime_info.network),
|
||||
network: NetworkOptions::defaults_from(&setup_info, &runtime_info.network, None),
|
||||
autoreboot: true,
|
||||
},
|
||||
setup_info,
|
||||
|
Loading…
Reference in New Issue
Block a user