From 4059b07e92637815221130993c0baf6815ebeb9d Mon Sep 17 00:00:00 2001 From: Wei Zhou Date: Wed, 21 Aug 2024 12:47:48 +0200 Subject: [PATCH] Dynamic: connect to ALL (or ALL dedicated) BGP peers if no BGP peer mapping for the network/vpc --- .../cloudstack/network/RoutedIpv4Manager.java | 2 +- .../cloudstack/network/dao/BgpPeerDaoImpl.java | 4 ++-- .../main/java/com/cloud/bgp/BGPServiceImpl.java | 8 ++++++++ .../com/cloud/network/NetworkServiceImpl.java | 16 +++++++--------- .../com/cloud/network/vpc/VpcManagerImpl.java | 10 ++++------ .../network/RoutedIpv4ManagerImpl.java | 2 +- 6 files changed, 23 insertions(+), 19 deletions(-) diff --git a/api/src/main/java/org/apache/cloudstack/network/RoutedIpv4Manager.java b/api/src/main/java/org/apache/cloudstack/network/RoutedIpv4Manager.java index 4a33d7236f65..f90b01fe3070 100644 --- a/api/src/main/java/org/apache/cloudstack/network/RoutedIpv4Manager.java +++ b/api/src/main/java/org/apache/cloudstack/network/RoutedIpv4Manager.java @@ -187,5 +187,5 @@ public interface RoutedIpv4Manager extends PluggableService, Configurable { Vpc changeBgpPeersForVpc(ChangeBgpPeersForVpcCmd changeBgpPeersForVpcCmd); - List getBgpPeersForAccount(Account owner, long zoneIdd); + List getBgpPeerIdsForAccount(Account owner, long zoneIdd); } diff --git a/engine/schema/src/main/java/org/apache/cloudstack/network/dao/BgpPeerDaoImpl.java b/engine/schema/src/main/java/org/apache/cloudstack/network/dao/BgpPeerDaoImpl.java index 06d601a96c20..367e7af2d12a 100644 --- a/engine/schema/src/main/java/org/apache/cloudstack/network/dao/BgpPeerDaoImpl.java +++ b/engine/schema/src/main/java/org/apache/cloudstack/network/dao/BgpPeerDaoImpl.java @@ -47,12 +47,12 @@ public class BgpPeerDaoImpl extends GenericDaoBase implements B protected SearchBuilder VpcIdSearch; protected SearchBuilder AllFieldsSearch; - private static final String LIST_ALL_BGP_PEERS_IDS_FOR_ACCOUNT = "SELECT id FROM `cloud`.`bgp_peers` WHERE data_center_id = ? " + + private static final String LIST_ALL_BGP_PEERS_IDS_FOR_ACCOUNT = "SELECT id FROM `cloud`.`bgp_peers` WHERE removed IS NULL AND data_center_id = ? " + "AND ((domain_id IS NULL AND account_id IS NULL) " + "OR (domain_id = ? AND account_id IS NULL) " + "OR (domain_id = ? AND account_id = ?))"; - private static final String LIST_DEDICATED_BGP_PEERS_IDS_FOR_ACCOUNT = "SELECT id FROM `cloud`.`bgp_peers` WHERE data_center_id = ? " + + private static final String LIST_DEDICATED_BGP_PEERS_IDS_FOR_ACCOUNT = "SELECT id FROM `cloud`.`bgp_peers` WHERE removed IS NULL AND data_center_id = ? " + "AND ((domain_id = ? AND account_id IS NULL) " + "OR (domain_id = ? AND account_id = ?))"; diff --git a/server/src/main/java/com/cloud/bgp/BGPServiceImpl.java b/server/src/main/java/com/cloud/bgp/BGPServiceImpl.java index d95a052cb180..4236ca52b401 100644 --- a/server/src/main/java/com/cloud/bgp/BGPServiceImpl.java +++ b/server/src/main/java/com/cloud/bgp/BGPServiceImpl.java @@ -68,6 +68,7 @@ import java.util.Date; import java.util.List; import java.util.Objects; +import java.util.stream.Collectors; public class BGPServiceImpl implements BGPService { @@ -392,6 +393,13 @@ public boolean applyBgpPeers(Network network, boolean continueOnError) throws Re } else { bgpPeers = bgpPeerDao.listNonRevokeByNetworkId(network.getId()); } + if (CollectionUtils.isEmpty(bgpPeers)) { + Account owner = accountDao.findByIdIncludingRemoved(network.getAccountId()); + List bgpPeerIds = routedIpv4Manager.getBgpPeerIdsForAccount(owner, network.getDataCenterId()); + bgpPeers = bgpPeerIds.stream() + .map(bgpPeerId -> bgpPeerDao.findById(bgpPeerId)) + .collect(Collectors.toList()); + } LOGGER.debug(String.format("Applying BPG Peers for network [%s]: [%s]", network, bgpPeers)); return ((BgpServiceProvider) provider).applyBgpPeers(network, bgpPeers); } diff --git a/server/src/main/java/com/cloud/network/NetworkServiceImpl.java b/server/src/main/java/com/cloud/network/NetworkServiceImpl.java index 72a37078b24f..9e3dedaf8c70 100644 --- a/server/src/main/java/com/cloud/network/NetworkServiceImpl.java +++ b/server/src/main/java/com/cloud/network/NetworkServiceImpl.java @@ -1707,15 +1707,13 @@ public Network createGuestNetwork(CreateNetworkCmd cmd) throws InsufficientCapac } // Validate BGP peers - if (vpcId != null && CollectionUtils.isNotEmpty(bgpPeerIds)) { - throw new InvalidParameterValueException("The BGP peers of VPC tiers will inherit from the VPC, do not add separately."); - } - if (CollectionUtils.isNotEmpty(bgpPeerIds) && !routedIpv4Manager.isDynamicRoutedNetwork(ntwkOff)) { - throw new InvalidParameterValueException("The network offering does not support Dynamic routing"); - } - if (CollectionUtils.isEmpty(bgpPeerIds)) { - bgpPeerIds = routedIpv4Manager.getBgpPeersForAccount(owner, zone.getId()); - } else { + if (CollectionUtils.isNotEmpty(bgpPeerIds)) { + if (vpcId != null) { + throw new InvalidParameterValueException("The BGP peers of VPC tiers will inherit from the VPC, do not add separately."); + } + if (!routedIpv4Manager.isDynamicRoutedNetwork(ntwkOff)) { + throw new InvalidParameterValueException("The network offering does not support Dynamic routing"); + } routedIpv4Manager.validateBgpPeers(owner, zone.getId(), bgpPeerIds); } diff --git a/server/src/main/java/com/cloud/network/vpc/VpcManagerImpl.java b/server/src/main/java/com/cloud/network/vpc/VpcManagerImpl.java index 77d01aad4436..33c6f5006c5e 100644 --- a/server/src/main/java/com/cloud/network/vpc/VpcManagerImpl.java +++ b/server/src/main/java/com/cloud/network/vpc/VpcManagerImpl.java @@ -1155,12 +1155,10 @@ public Vpc createVpc(final long zoneId, final long vpcOffId, final long vpcOwner validateVpcCidrSize(caller, owner.getAccountId(), vpcOff, cidr, cidrSize); // Validate BGP peers - if (CollectionUtils.isNotEmpty(bgpPeerIds) && !routedIpv4Manager.isDynamicRoutedVpc(vpcOff)) { - throw new InvalidParameterValueException("The VPC offering does not support Dynamic routing"); - } - if (CollectionUtils.isEmpty(bgpPeerIds)) { - bgpPeerIds = routedIpv4Manager.getBgpPeersForAccount(owner, zone.getId()); - } else { + if (CollectionUtils.isNotEmpty(bgpPeerIds)) { + if (!routedIpv4Manager.isDynamicRoutedVpc(vpcOff)) { + throw new InvalidParameterValueException("The VPC offering does not support Dynamic routing"); + } routedIpv4Manager.validateBgpPeers(owner, zone.getId(), bgpPeerIds); } diff --git a/server/src/main/java/org/apache/cloudstack/network/RoutedIpv4ManagerImpl.java b/server/src/main/java/org/apache/cloudstack/network/RoutedIpv4ManagerImpl.java index e5638bc0d2b4..8f7a4393cef4 100644 --- a/server/src/main/java/org/apache/cloudstack/network/RoutedIpv4ManagerImpl.java +++ b/server/src/main/java/org/apache/cloudstack/network/RoutedIpv4ManagerImpl.java @@ -1536,7 +1536,7 @@ public Vpc changeBgpPeersForVpc(ChangeBgpPeersForVpcCmd changeBgpPeersForVpcCmd) } @Override - public List getBgpPeersForAccount(Account owner, long zoneId) { + public List getBgpPeerIdsForAccount(Account owner, long zoneId) { return bgpPeerDao.listAvailableBgpPeerIdsForAccount(zoneId, owner.getDomainId(), owner.getId(), UseSystemBgpPeers.valueIn(owner.getId())); }