diff --git a/internal/cluster/cluster.go b/internal/cluster/cluster.go index 04f86adfca4..9ff388df83e 100644 --- a/internal/cluster/cluster.go +++ b/internal/cluster/cluster.go @@ -1399,12 +1399,8 @@ func (m *Manager) setConnectivityMajorityGroupsForClusterInternal(cluster *commo majorityGroups[cidr] = majorityGroup } - primaryMachineCIDR := "" - if network.IsMachineCidrAvailable(cluster) { - primaryMachineCIDR = network.GetMachineCidrById(cluster, 0) - } for _, family := range []network.AddressFamily{network.IPv4, network.IPv6} { - majorityGroup, err := network.CreateL3MajorityGroup(hosts, family, primaryMachineCIDR) + majorityGroup, err := network.CreateL3MajorityGroup(hosts, family, network.GetMachineNetworkCidrs(cluster)) if err != nil { m.log.WithError(err).Warnf("Create L3 majority group for cluster %s failed", cluster.ID.String()) } else { diff --git a/internal/network/connectivity_groups.go b/internal/network/connectivity_groups.go index 11fac004f1f..f3710129396 100644 --- a/internal/network/connectivity_groups.go +++ b/internal/network/connectivity_groups.go @@ -416,9 +416,10 @@ func newL2QueryFactory(cidr string) (hostQueryFactory, error) { } type l3Query struct { - current int - connectivityReport models.ConnectivityReport - nodesAddresses map[strfmt.UUID]map[string]bool + current int + connectivityReport models.ConnectivityReport + nodesAddresses map[strfmt.UUID]map[string]bool + machineNetworkCidrs []string } func (l *l3Query) next() strfmt.UUID { @@ -431,12 +432,14 @@ func (l *l3Query) next() strfmt.UUID { } foundAddresses := make(map[string]bool) for _, l3 := range rh.L3Connectivity { - _, foundAddress := addresses[l3.RemoteIPAddress] - if foundAddress && l3.Successful { - foundAddresses[l3.RemoteIPAddress] = true + if isInCidr := IpInCidrs(l3.RemoteIPAddress, l.machineNetworkCidrs); isInCidr { + _, foundAddress := addresses[l3.RemoteIPAddress] + if foundAddress && l3.Successful { + foundAddresses[l3.RemoteIPAddress] = true + } } } - if len(addresses) == len(foundAddresses) { + if len(foundAddresses) > 0 && len(addresses) == len(foundAddresses) { return rh.HostID } } @@ -444,12 +447,14 @@ func (l *l3Query) next() strfmt.UUID { } type l3QueryFactory struct { - nodesAddresses map[strfmt.UUID]map[string]bool + nodesAddresses map[strfmt.UUID]map[string]bool + machineNetworkCidrs []string } func (l *l3QueryFactory) create(h *models.Host) (hostQuery, error) { ret := l3Query{ - nodesAddresses: l.nodesAddresses, + nodesAddresses: l.nodesAddresses, + machineNetworkCidrs: l.machineNetworkCidrs, } err := json.Unmarshal([]byte(h.Connectivity), &ret.connectivityReport) if err != nil { @@ -458,7 +463,7 @@ func (l *l3QueryFactory) create(h *models.Host) (hostQuery, error) { return &ret, nil } -func newL3QueryFactory(hosts []*models.Host, family AddressFamily, primaryMachineCIDR string) (hostQueryFactory, error) { +func newL3QueryFactory(hosts []*models.Host, family AddressFamily, machineNetworkCidrs []string) (hostQueryFactory, error) { nodesAddresses := make(map[strfmt.UUID]map[string]bool) for _, h := range hosts { if h.Inventory == "" { @@ -482,11 +487,8 @@ func newL3QueryFactory(hosts []*models.Host, family AddressFamily, primaryMachin if err != nil { return nil, err } - ipInCidr, err := IpInCidr(ip.String(), primaryMachineCIDR) - if err != nil { - return nil, err - } - if ipInCidr { + + if ipInCidr := IpInCidrs(ip.String(), machineNetworkCidrs); ipInCidr { value[ip.String()] = true } } @@ -494,7 +496,8 @@ func newL3QueryFactory(hosts []*models.Host, family AddressFamily, primaryMachin nodesAddresses[*h.ID] = value } return &l3QueryFactory{ - nodesAddresses: nodesAddresses, + nodesAddresses: nodesAddresses, + machineNetworkCidrs: machineNetworkCidrs, }, nil } @@ -577,11 +580,11 @@ func CreateL2MajorityGroup(cidr string, hosts []*models.Host) ([]strfmt.UUID, er * It is done by taking a sorted connectivity group list according to the group size, and from this group take the * largest one */ -func CreateL3MajorityGroup(hosts []*models.Host, family AddressFamily, primaryMachineCIDR string) ([]strfmt.UUID, error) { +func CreateL3MajorityGroup(hosts []*models.Host, family AddressFamily, machineCidrs []string) ([]strfmt.UUID, error) { if !funk.Contains([]AddressFamily{IPv4, IPv6}, family) { return nil, errors.Errorf("Unexpected address family %+v", family) } - factory, err := newL3QueryFactory(hosts, family, primaryMachineCIDR) + factory, err := newL3QueryFactory(hosts, family, machineCidrs) if err != nil { return nil, err } diff --git a/internal/network/machine_network_cidr.go b/internal/network/machine_network_cidr.go index 0c4285ff264..92e4e20962f 100644 --- a/internal/network/machine_network_cidr.go +++ b/internal/network/machine_network_cidr.go @@ -281,6 +281,15 @@ func IpInCidr(ipAddr, cidr string) (bool, error) { return ipNet.Contains(ip), nil } +func IpInCidrs(ipAddr string, cidrs []string) bool { + for _, cidr := range cidrs { + if isInCidr, _ := IpInCidr(ipAddr, cidr); isInCidr { + return true + } + } + return false +} + func belongsToNetwork(log logrus.FieldLogger, h *models.Host, machineIpnet *net.IPNet) bool { var inventory models.Inventory err := json.Unmarshal([]byte(h.Inventory), &inventory)