Skip to content

Commit

Permalink
Merge pull request #228 from gabrielmougard/feat/specify-dns-server-f…
Browse files Browse the repository at this point in the history
…or-uplink

feat: Give option to specify custom DNS addresses for OVN uplink
  • Loading branch information
tomponline authored Dec 5, 2023
2 parents 848687d + 1a4a00b commit b1171ec
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 43 deletions.
44 changes: 24 additions & 20 deletions microcloud/cmd/microcloud/ask.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
cli "github.com/canonical/lxd/shared/cmd"
"github.com/canonical/lxd/shared/logger"
"github.com/canonical/lxd/shared/units"
"github.com/canonical/lxd/shared/validate"
cephTypes "github.com/canonical/microceph/microceph/api/types"

"github.com/canonical/microcloud/microcloud/api/types"
Expand Down Expand Up @@ -643,7 +644,8 @@ func (c *CmdControl) askNetwork(sh *service.Handler, systems map[string]InitSyst
}

// Prepare the configuration.
config := map[string]string{}
ipConfig := map[string]string{}
dnsConfig := map[string]string{}
if bootstrap {
for _, ip := range []string{"IPv4", "IPv6"} {
validator := func(s string) error {
Expand Down Expand Up @@ -674,37 +676,34 @@ func (c *CmdControl) askNetwork(sh *service.Handler, systems map[string]InitSyst
}

if gateway != "" {
validator := func(s string) error {
addr := net.ParseIP(s)
if addr == nil {
return fmt.Errorf("Invalid IP address %q", s)
if ip == "IPv4" {
rangeStart, err := c.asker.AskString(fmt.Sprintf("Specify the first %s address in the range to use with LXD: ", ip), "", validate.Required(validate.IsNetworkAddressV4))
if err != nil {
return err
}

if addr.To4() == nil && ip == "IPv4" {
return fmt.Errorf("Not a valid IPv4")
rangeEnd, err := c.asker.AskString(fmt.Sprintf("Specify the last %s address in the range to use with LXD: ", ip), "", validate.Required(validate.IsNetworkAddressV4))
if err != nil {
return err
}

if addr.To4() != nil && ip == "IPv6" {
return fmt.Errorf("Not a valid IPv6")
}
ipConfig[gateway] = fmt.Sprintf("%s-%s", rangeStart, rangeEnd)

return nil
}

if ip == "IPv4" {
rangeStart, err := c.asker.AskString(fmt.Sprintf("Specify the first %s address in the range to use with LXD: ", ip), "", validator)
gatewayAddr, _, err := net.ParseCIDR(gateway)
if err != nil {
return err
}

rangeEnd, err := c.asker.AskString(fmt.Sprintf("Specify the last %s address in the range to use with LXD: ", ip), "", validator)
dnsAddresses, err := c.asker.AskString(fmt.Sprintf("Specify the DNS addresses (comma-separated IPv4 / IPv6 addresses) for the distributed network (default: %s): ", gatewayAddr.String()), gatewayAddr.String(), validate.Optional(validate.IsListOf(validate.IsNetworkAddress)))
if err != nil {
return err
}

config[gateway] = fmt.Sprintf("%s-%s", rangeStart, rangeEnd)
if dnsAddresses != "" {
dnsConfig[gateway] = dnsAddresses
}
} else {
config[gateway] = ""
ipConfig[gateway] = ""
}
}
}
Expand Down Expand Up @@ -742,7 +741,7 @@ func (c *CmdControl) askNetwork(sh *service.Handler, systems map[string]InitSyst
var ipv4Gateway string
var ipv4Ranges string
var ipv6Gateway string
for gateway, ipRange := range config {
for gateway, ipRange := range ipConfig {
ip, _, err := net.ParseCIDR(gateway)
if err != nil {
return err
Expand All @@ -756,7 +755,12 @@ func (c *CmdControl) askNetwork(sh *service.Handler, systems map[string]InitSyst
}
}

uplink, ovn := lxd.DefaultOVNNetwork(ipv4Gateway, ipv4Ranges, ipv6Gateway)
var allDNSServers string
for _, dnsAddr := range dnsConfig {
allDNSServers = dnsAddr
}

uplink, ovn := lxd.DefaultOVNNetwork(ipv4Gateway, ipv4Ranges, ipv6Gateway, allDNSServers)
bootstrapSystem.Networks = []api.NetworksPost{uplink, ovn}
systems[sh.Name] = bootstrapSystem
}
Expand Down
3 changes: 2 additions & 1 deletion microcloud/cmd/microcloud/main_init_preseed.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type InitNetwork struct {
IPv4Gateway string `yaml:"ipv4_gateway"`
IPv4Range string `yaml:"ipv4_range"`
IPv6Gateway string `yaml:"ipv6_gateway"`
DNSServers string `yaml:"dns_servers"`
}

// StorageFilter separates the filters used for local and ceph disks.
Expand Down Expand Up @@ -430,7 +431,7 @@ func (p *Preseed) Parse(s *service.Handler, bootstrap bool) (map[string]InitSyst
if bootstrap {
system.TargetNetworks = append(system.TargetNetworks, lxd.DefaultPendingOVNNetwork(iface))
if s.Name == peer {
uplink, ovn := lxd.DefaultOVNNetwork(p.OVN.IPv4Gateway, p.OVN.IPv4Range, p.OVN.IPv6Gateway)
uplink, ovn := lxd.DefaultOVNNetwork(p.OVN.IPv4Gateway, p.OVN.IPv4Range, p.OVN.IPv6Gateway, p.OVN.DNSServers)
system.Networks = append(system.Networks, uplink, ovn)
}
} else {
Expand Down
6 changes: 5 additions & 1 deletion microcloud/service/lxd_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (s LXDService) DefaultOVNNetworkJoinConfig(parent string) api.ClusterMember
// creating the finalized network.
// Returns both the finalized uplink configuration as the first argument,
// and the default OVN network configuration as the second argument.
func (s LXDService) DefaultOVNNetwork(ipv4Gateway string, ipv4Range string, ipv6Gateway string) (api.NetworksPost, api.NetworksPost) {
func (s LXDService) DefaultOVNNetwork(ipv4Gateway string, ipv4Range string, ipv6Gateway string, dnsServers string) (api.NetworksPost, api.NetworksPost) {
finalUplinkCfg := api.NetworksPost{
NetworkPut: api.NetworkPut{
Config: map[string]string{},
Expand All @@ -85,6 +85,10 @@ func (s LXDService) DefaultOVNNetwork(ipv4Gateway string, ipv4Range string, ipv6
finalUplinkCfg.Config["ipv6.gateway"] = ipv6Gateway
}

if dnsServers != "" {
finalUplinkCfg.Config["dns.nameservers"] = dnsServers
}

ovnNetwork := api.NetworksPost{
NetworkPut: api.NetworkPut{Config: map[string]string{"network": "UPLINK"}, Description: "Default OVN network"},
Name: "default",
Expand Down
53 changes: 33 additions & 20 deletions microcloud/test/includes/microcloud.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,33 @@ unset_interactive_vars() {
unset LOOKUP_IFACE LIMIT_SUBNET SKIP_SERVICE EXPECT_PEERS \
SETUP_ZFS ZFS_FILTER ZFS_WIPE \
SETUP_CEPH CEPH_WARNING CEPH_FILTER CEPH_WIPE \
SETUP_OVN OVN_WARNING OVN_FILTER IPV4_SUBNET IPV4_START IPV4_END IPV6_SUBNET
SETUP_OVN OVN_WARNING OVN_FILTER IPV4_SUBNET IPV4_START IPV4_END CUSTOM_DNS_ADDRESSES IPV6_SUBNET
}

# microcloud_interactive: outputs text that can be passed to `TEST_CONSOLE=1 microcloud init`
# to simulate terminal input to the interactive CLI.
# The lines that are output are based on the values passed to the listed environment variables.
# Any unset variables will be omitted.
microcloud_interactive() {
LOOKUP_IFACE=${LOOKUP_IFACE:-} # filter string for the lookup interface table.
LIMIT_SUBNET=${LIMIT_SUBNET:-} # (yes/no) input for limiting lookup of systems to the above subnet.
SKIP_SERVICE=${SKIP_SERVICE:-} # (yes/no) input to skip any missing services. Should be unset if all services are installed.
EXPECT_PEERS=${EXPECT_PEERS:-} # wait for this number of systems to be available to join the cluster.
SETUP_ZFS=${SETUP_ZFS:-} # (yes/no) input for initiating ZFS storage pool setup.
ZFS_FILTER=${ZFS_FILTER:-} # filter string for ZFS disks.
ZFS_WIPE=${ZFS_WIPE:-} # (yes/no) to wipe all disks.
SETUP_CEPH=${SETUP_CEPH:-} # (yes/no) input for initiating CEPH storage pool setup.
CEPH_WARNING=${CEPH_WARNING:-} # (yes/no) input for warning about eligible disk detection.
CEPH_FILTER=${CEPH_FILTER:-} # filter string for CEPH disks.
CEPH_WIPE=${CEPH_WIPE:-} # (yes/no) to wipe all disks.
SETUP_OVN=${SETUP_OVN:-} # (yes/no) input for initiating OVN network setup.
OVN_WARNING=${OVN_WARNING:-} # (yes/no) input for warning about eligible interface detection.
OVN_FILTER=${OVN_FILTER:-} # filter string for OVN interfaces.
IPV4_SUBNET=${IPV4_SUBNET:-} # OVN ipv4 gateway subnet.
IPV4_START=${IPV4_START:-} # OVN ipv4 range start.
IPV4_END=${IPV4_END:-} # OVN ipv4 range end.
IPV6_SUBNET=${IPV6_SUBNET:-} # OVN ipv6 range.
LOOKUP_IFACE=${LOOKUP_IFACE:-} # filter string for the lookup interface table.
LIMIT_SUBNET=${LIMIT_SUBNET:-} # (yes/no) input for limiting lookup of systems to the above subnet.
SKIP_SERVICE=${SKIP_SERVICE:-} # (yes/no) input to skip any missing services. Should be unset if all services are installed.
EXPECT_PEERS=${EXPECT_PEERS:-} # wait for this number of systems to be available to join the cluster.
SETUP_ZFS=${SETUP_ZFS:-} # (yes/no) input for initiating ZFS storage pool setup.
ZFS_FILTER=${ZFS_FILTER:-} # filter string for ZFS disks.
ZFS_WIPE=${ZFS_WIPE:-} # (yes/no) to wipe all disks.
SETUP_CEPH=${SETUP_CEPH:-} # (yes/no) input for initiating CEPH storage pool setup.
CEPH_WARNING=${CEPH_WARNING:-} # (yes/no) input for warning about eligible disk detection.
CEPH_FILTER=${CEPH_FILTER:-} # filter string for CEPH disks.
CEPH_WIPE=${CEPH_WIPE:-} # (yes/no) to wipe all disks.
SETUP_OVN=${SETUP_OVN:-} # (yes/no) input for initiating OVN network setup.
OVN_WARNING=${OVN_WARNING:-} # (yes/no) input for warning about eligible interface detection.
OVN_FILTER=${OVN_FILTER:-} # filter string for OVN interfaces.
IPV4_SUBNET=${IPV4_SUBNET:-} # OVN ipv4 gateway subnet.
IPV4_START=${IPV4_START:-} # OVN ipv4 range start.
IPV4_END=${IPV4_END:-} # OVN ipv4 range end.
CUSTOM_DNS_ADDRESSES=${CUSTOM_DNS_ADDRESSES:-} # OVN custom DNS addresses.
IPV6_SUBNET=${IPV6_SUBNET:-} # OVN ipv6 range.

setup=$(cat << EOF
${LOOKUP_IFACE} # filter the lookup interface
Expand Down Expand Up @@ -84,6 +85,7 @@ $([ "${SETUP_OVN}" = "yes" ] && printf -- "---")
${IPV4_SUBNET} # setup ipv4/ipv6 gateways and ranges
${IPV4_START}
${IPV4_END}
${CUSTOM_DNS_ADDRESSES}
${IPV6_SUBNET}
EOF
)
Expand Down Expand Up @@ -202,6 +204,7 @@ validate_system_lxd_ovn() {
ipv4_gateway=${4:-}
ipv4_ranges=${5:-}
ipv6_gateway=${6:-}
dns_namesersers=${7:-}

echo " ${name} Validating OVN network"
addr=$(lxc exec local:"${name}" -- lxc config get cluster.https_address)
Expand All @@ -217,6 +220,15 @@ validate_system_lxd_ovn() {
! lxc config get "network.ovn.northbound_connection" --target "${name}" | sed -e 's/,/\n/g' | grep -q '^ssl:$' || false
! lxc config get "network.ovn.northbound_connection" --target "${name}" | sed -e 's/,/\n/g' | grep -q '^ssl::' || false

# Check that the created UPLINK network has the right DNS servers.
if [ -n "${dns_namesersers}" ] ; then
dns_addresses=$(lxc exec ${name} -- sh -c "lxc network get UPLINK dns.nameservers")
if [ "${dns_addresses}" != "${dns_namesersers}" ] ; then
echo "ERROR: UPLINK network has wrong DNS server addresses: ${dns_addresses}"
return 1
fi
fi

cfg=$(lxc network show UPLINK)
echo "${cfg}" | grep -q "status: Created"
echo "${cfg}" | grep -q "type: physical"
Expand Down Expand Up @@ -261,6 +273,7 @@ validate_system_lxd() {
ipv4_gateway=${6:-}
ipv4_ranges=${7:-}
ipv6_gateway=${8:-}
dns_namesersers=${9:-}

echo "==> ${name} Validating LXD with ${num_peers} peers"
echo " ${name} Local Disk: {${local_disk}}, Remote Disks: {${remote_disks}}, OVN Iface: {${ovn_interface}}"
Expand Down Expand Up @@ -289,7 +302,7 @@ validate_system_lxd() {
} > /dev/null 2>&1

if [ "${has_microovn}" = 1 ] && [ -n "${ovn_interface}" ] ; then
validate_system_lxd_ovn "${name}" "${num_peers}" "${ovn_interface}" "${ipv4_gateway}" "${ipv4_ranges}" "${ipv6_gateway}"
validate_system_lxd_ovn "${name}" "${num_peers}" "${ovn_interface}" "${ipv4_gateway}" "${ipv4_ranges}" "${ipv6_gateway}" "${dns_namesersers}"
else
validate_system_lxd_fan "${name}"
fi
Expand Down
1 change: 1 addition & 0 deletions microcloud/test/suites/add.sh
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ test_add_interactive() {
export IPV4_SUBNET="10.1.123.1/24"
export IPV4_START="10.1.123.100"
export IPV4_END="10.1.123.254"
export CUSTOM_DNS_ADDRESSES="10.1.123.1,8.8.8.8" # comma-separated list of custom DNS addresses to be set for the OVN uplink.
export IPV6_SUBNET="fd42:1:1234:1234::1/64"
microcloud_interactive | lxc exec micro01 -- sh -c "microcloud init > out"
lxc exec micro01 -- tail -1 out | grep "MicroCloud is ready" -q
Expand Down
3 changes: 2 additions & 1 deletion microcloud/test/suites/preseed.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ ovn:
ipv4_gateway: 10.1.123.1/24
ipv4_range: 10.1.123.100-10.1.123.254
ipv6_gateway: fd42:1:1234:1234::1/64
dns_servers: 10.1.123.1,8.8.8.8,fd42:1:1234:1234::1
storage:
local:
Expand All @@ -51,7 +52,7 @@ EOF
lxc exec micro01 -- sh -c "cat /root/preseed.yaml | TEST_CONSOLE=0 microcloud init --preseed"

for m in micro01 micro03 ; do
validate_system_lxd ${m} 3 disk1 2 enp6s0 10.1.123.1/24 10.1.123.100-10.1.123.254 fd42:1:1234:1234::1/64
validate_system_lxd ${m} 3 disk1 2 enp6s0 10.1.123.1/24 10.1.123.100-10.1.123.254 fd42:1:1234:1234::1/64 10.1.123.1,8.8.8.8,fd42:1:1234:1234::1
validate_system_microceph ${m} disk2 disk3
validate_system_microovn ${m}
done
Expand Down

0 comments on commit b1171ec

Please sign in to comment.