Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

microcloud: Subnet sharing warning should check interfaces not IPs #522

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 57 additions & 15 deletions cmd/microcloud/ask.go
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,22 @@ func (c *initConfig) askRemotePool(sh *service.Handler) error {
return nil
}

// Network is a helper struct to store an IP address, its subnet and
// its corresponding network interface name.
type Network struct {
// Interface is the name of the network interface. An example
// of why this is useful in MicroCloud is when we want to check that different network types (OVN, Ceph, etc)
// are on different network interfaces.
Interface net.Interface
// IP is the IP address of the network. An example of why this is useful in MicroCloud is when we want to store
// a member OVN underlay network IP address.
IP net.IP
// Subnet is the subnet of the network. An example of why this is useful in MicroCloud is when we want to check
// that we don't have subnet collisions between different network types (OVN, Ceph, etc) in a cluster.
// For example, we don't want a member using 'subnet A' for OVN and an other member using 'subnet A' for Ceph.
Subnet *net.IPNet
}

func (c *initConfig) askOVNNetwork(sh *service.Handler) error {
if sh.Services[types.MicroOVN] == nil {
return nil
Expand Down Expand Up @@ -1133,7 +1149,7 @@ func (c *initConfig) askOVNNetwork(sh *service.Handler) error {
}
}

var ovnUnderlaySelectedIPs map[string]string
var ovnUnderlaySelectedNets map[string]*Network
ovnUnderlayData := [][]string{}
for peer, system := range c.systems {
// skip any systems that have already been clustered, but are available for other configuration.
Expand All @@ -1159,28 +1175,29 @@ func (c *initConfig) askOVNNetwork(sh *service.Handler) error {

if wantsDedicatedUnderlay {
header = []string{"LOCATION", "IFACE", "TYPE", "IP ADDRESS (CIDR)"}
ovnUnderlaySelectedIPs = map[string]string{}
err = c.askRetry("Retry selecting underlay network interfaces?", func() error {
table := tui.NewSelectableTable(header, ovnUnderlayData)
answers, err := table.Render(context.Background(), c.asker, "Select exactly one network interface from each cluster member:")
if err != nil {
return err
}

ovnUnderlaySelectedIPs = map[string]string{}
ovnUnderlaySelectedNets = make(map[string]*Network)
for _, answer := range answers {
target := answer["LOCATION"]
ipAddr := answer["IP ADDRESS (CIDR)"]
if ovnUnderlaySelectedIPs[target] != "" {
ifaceName := answer["IFACE"]

if ovnUnderlaySelectedNets[target] != nil {
return fmt.Errorf("Failed to configure OVN underlay traffic: Selected more than one interface for target %q", target)
}

ip, _, err := net.ParseCIDR(ipAddr)
ip, ipNet, err := net.ParseCIDR(ipAddr)
if err != nil {
return err
}

ovnUnderlaySelectedIPs[target] = ip.String()
ovnUnderlaySelectedNets[target] = &Network{Interface: net.Interface{Name: ifaceName}, IP: ip, Subnet: ipNet}
}

return nil
Expand All @@ -1191,11 +1208,11 @@ func (c *initConfig) askOVNNetwork(sh *service.Handler) error {
}
}

if len(ovnUnderlaySelectedIPs) > 0 {
if len(ovnUnderlaySelectedNets) > 0 {
for peer := range askSystems {
underlayIP, ok := ovnUnderlaySelectedIPs[peer]
underlayNetwork, ok := ovnUnderlaySelectedNets[peer]
if ok {
fmt.Printf(" Using %q for OVN underlay traffic on %q\n", underlayIP, peer)
fmt.Printf(" Using %q for OVN underlay traffic on %q\n", underlayNetwork.IP.String(), peer)
}
}

Expand Down Expand Up @@ -1232,10 +1249,10 @@ func (c *initConfig) askOVNNetwork(sh *service.Handler) error {
system.Networks = append(system.Networks, finalConfigs...)
}

if ovnUnderlaySelectedIPs != nil {
ovnUnderlayIpAddr, ok := ovnUnderlaySelectedIPs[peer]
if ovnUnderlaySelectedNets != nil {
ovnUnderlayNet, ok := ovnUnderlaySelectedNets[peer]
if ok {
system.OVNGeneveAddr = ovnUnderlayIpAddr
system.OVNGeneveNetwork = ovnUnderlayNet
}
}

Expand Down Expand Up @@ -1393,14 +1410,24 @@ func (c *initConfig) askCephNetwork(sh *service.Handler) error {
return err
}

internalCephNetworkInterface, err := lxd.FindInterfaceForSubnet(internalCephSubnet)
if err != nil {
return fmt.Errorf("Failed to find interface for subnet %q: %w", internalCephSubnet, err)
}

if internalCephSubnet != microCloudInternalNetworkAddrCIDR {
err = c.validateCephInterfacesForSubnet(lxd, availableCephNetworkInterfaces, internalCephSubnet)
if err != nil {
return err
}

internalCephIP, internalCephNet, err := net.ParseCIDR(internalCephSubnet)
if err != nil {
return fmt.Errorf("Failed to parse the internal Ceph network: %w", err)
}

bootstrapSystem := c.systems[sh.Name]
bootstrapSystem.MicroCephInternalNetworkSubnet = internalCephSubnet
bootstrapSystem.MicroCephInternalNetwork = &Network{Interface: *internalCephNetworkInterface, Subnet: internalCephNet, IP: internalCephIP}
c.systems[sh.Name] = bootstrapSystem
}

Expand All @@ -1409,6 +1436,11 @@ func (c *initConfig) askCephNetwork(sh *service.Handler) error {
return err
}

publicCephNetworkInterface, err := lxd.FindInterfaceForSubnet(publicCephSubnet)
if err != nil {
return fmt.Errorf("Failed to find interface for subnet %q: %w", publicCephSubnet, err)
}

if publicCephSubnet != internalCephSubnet {
err = c.validateCephInterfacesForSubnet(lxd, availableCephNetworkInterfaces, publicCephSubnet)
if err != nil {
Expand All @@ -1417,15 +1449,25 @@ func (c *initConfig) askCephNetwork(sh *service.Handler) error {
}

if publicCephSubnet != microCloudInternalNetworkAddrCIDR {
publicCephIP, publicCephNet, err := net.ParseCIDR(publicCephSubnet)
if err != nil {
return fmt.Errorf("Failed to parse the public Ceph network: %w", err)
}

bootstrapSystem := c.systems[sh.Name]
bootstrapSystem.MicroCephPublicNetworkSubnet = publicCephSubnet
bootstrapSystem.MicroCephPublicNetwork = &Network{Interface: *publicCephNetworkInterface, Subnet: publicCephNet, IP: publicCephIP}
c.systems[sh.Name] = bootstrapSystem

// This is to avoid the situation where the internal network for Ceph has been skipped, but the public network has been set.
// Ceph will automatically set the internal network to the public Ceph network if the internal network is not set, which is not what we want.
// Instead, we still want to keep the internal Ceph network to use the MicroCloud internal network as a default.
if internalCephSubnet == microCloudInternalNetworkAddrCIDR {
bootstrapSystem.MicroCephInternalNetworkSubnet = microCloudInternalNetworkAddrCIDR
microcloudInternalIP, microcloudNet, err := net.ParseCIDR(microCloudInternalNetworkAddrCIDR)
if err != nil {
return fmt.Errorf("Failed to parse the internal MicroCloud network: %w", err)
}

bootstrapSystem.MicroCephInternalNetwork = &Network{Interface: bootstrapSystem.MicroCloudInternalNetwork.Interface, Subnet: microcloudNet, IP: microcloudInternalIP}
c.systems[sh.Name] = bootstrapSystem
}
}
Expand Down
Loading
Loading