From e63cf69026c12435043608662c7e4e739409cc3e Mon Sep 17 00:00:00 2001 From: Wei Zhou Date: Thu, 4 Apr 2024 12:58:04 +0200 Subject: [PATCH] server: fix haproxy misconfiguration after VPC VR start --- .../VirtualNetworkApplianceManagerImpl.java | 49 ++++++++++++------- ...VpcVirtualNetworkApplianceManagerImpl.java | 26 ++++++++++ 2 files changed, 56 insertions(+), 19 deletions(-) diff --git a/server/src/main/java/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java b/server/src/main/java/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java index d208021e8334..3093c56f03b2 100644 --- a/server/src/main/java/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java +++ b/server/src/main/java/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java @@ -2578,25 +2578,7 @@ protected void finalizeNetworkRulesForNetwork(final Commands cmds, final DomainR } } - final List lbs = _loadBalancerDao.listByNetworkIdAndScheme(guestNetworkId, Scheme.Public); - final List lbRules = new ArrayList(); - if (_networkModel.isProviderSupportServiceInNetwork(guestNetworkId, Service.Lb, provider)) { - // Re-apply load balancing rules - for (final LoadBalancerVO lb : lbs) { - final List dstList = _lbMgr.getExistingDestinations(lb.getId()); - final List policyList = _lbMgr.getStickinessPolicies(lb.getId()); - final List hcPolicyList = _lbMgr.getHealthCheckPolicies(lb.getId()); - final Ip sourceIp = _networkModel.getPublicIpAddress(lb.getSourceIpAddressId()).getAddress(); - final LbSslCert sslCert = _lbMgr.getLbSslCert(lb.getId()); - final LoadBalancingRule loadBalancing = new LoadBalancingRule(lb, dstList, policyList, hcPolicyList, sourceIp, sslCert, lb.getLbProtocol()); - lbRules.add(loadBalancing); - } - } - - s_logger.debug("Found " + lbRules.size() + " load balancing rule(s) to apply as a part of domR " + router + " start."); - if (!lbRules.isEmpty()) { - _commandSetupHelper.createApplyLoadBalancingRulesCommands(lbRules, router, cmds, guestNetworkId); - } + createApplyLoadBalancingRulesCommands(cmds, router, provider, guestNetworkId); } // Reapply dhcp and dns configuration. final Network guestNetwork = _networkDao.findById(guestNetworkId); @@ -2623,6 +2605,35 @@ protected void finalizeNetworkRulesForNetwork(final Commands cmds, final DomainR } } + private void createApplyLoadBalancingRulesCommands(final Commands cmds, final DomainRouterVO router, final Provider provider, final Long guestNetworkId) { + if (router.getVpcId() != null) { + return; + } + final List lbs = _loadBalancerDao.listByNetworkIdAndScheme(guestNetworkId, Scheme.Public); + final List lbRules = new ArrayList(); + if (_networkModel.isProviderSupportServiceInNetwork(guestNetworkId, Service.Lb, provider)) { + // Re-apply load balancing rules + createLoadBalancingRulesList(lbRules, lbs); + } + + s_logger.debug("Found " + lbRules.size() + " load balancing rule(s) to apply as a part of domR " + router + " start."); + if (!lbRules.isEmpty()) { + _commandSetupHelper.createApplyLoadBalancingRulesCommands(lbRules, router, cmds, guestNetworkId); + } + } + + protected void createLoadBalancingRulesList(List lbRules, final List lbs) { + for (final LoadBalancerVO lb : lbs) { + final List dstList = _lbMgr.getExistingDestinations(lb.getId()); + final List policyList = _lbMgr.getStickinessPolicies(lb.getId()); + final List hcPolicyList = _lbMgr.getHealthCheckPolicies(lb.getId()); + final Ip sourceIp = _networkModel.getPublicIpAddress(lb.getSourceIpAddressId()).getAddress(); + final LbSslCert sslCert = _lbMgr.getLbSslCert(lb.getId()); + final LoadBalancingRule loadBalancing = new LoadBalancingRule(lb, dstList, policyList, hcPolicyList, sourceIp, sslCert, lb.getLbProtocol()); + lbRules.add(loadBalancing); + } + } + private void createDefaultEgressFirewallRule(final List rules, final long networkId) { final NetworkVO network = _networkDao.findById(networkId); final NetworkOfferingVO offering = _networkOfferingDao.findById(network.getNetworkOfferingId()); diff --git a/server/src/main/java/com/cloud/network/router/VpcVirtualNetworkApplianceManagerImpl.java b/server/src/main/java/com/cloud/network/router/VpcVirtualNetworkApplianceManagerImpl.java index 74b0dddfd45f..1c1dc568b2c9 100644 --- a/server/src/main/java/com/cloud/network/router/VpcVirtualNetworkApplianceManagerImpl.java +++ b/server/src/main/java/com/cloud/network/router/VpcVirtualNetworkApplianceManagerImpl.java @@ -64,10 +64,14 @@ import com.cloud.network.VirtualRouterProvider; import com.cloud.network.addr.PublicIp; import com.cloud.network.dao.IPAddressVO; +import com.cloud.network.dao.LoadBalancerDao; +import com.cloud.network.dao.LoadBalancerVO; import com.cloud.network.dao.MonitoringServiceVO; import com.cloud.network.dao.NetworkVO; import com.cloud.network.dao.RemoteAccessVpnVO; import com.cloud.network.dao.Site2SiteVpnConnectionVO; +import com.cloud.network.lb.LoadBalancingRule; +import com.cloud.network.rules.LoadBalancerContainer.Scheme; import com.cloud.network.vpc.NetworkACLItemDao; import com.cloud.network.vpc.NetworkACLItemVO; import com.cloud.network.vpc.NetworkACLManager; @@ -129,6 +133,8 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian private EntityManager _entityMgr; @Inject protected HypervisorGuruManager _hvGuruMgr; + @Inject + private LoadBalancerDao loadBalancerDao; @Override public boolean configure(final String name, final Map params) throws ConfigurationException { @@ -522,12 +528,32 @@ public boolean finalizeCommandsOnStart(final Commands cmds, final VirtualMachine cmds.addCommand(finishCmd); } + createApplyLoadBalancingRulesCommandsForVpc(cmds, domainRouterVO, provider, guestNics); + // Add network usage commands cmds.addCommands(usageCmds); } return true; } + private void createApplyLoadBalancingRulesCommandsForVpc(final Commands cmds, DomainRouterVO domainRouterVO, Provider provider, + List> guestNics) { + final List lbs = loadBalancerDao.listByVpcIdAndScheme(domainRouterVO.getVpcId(), Scheme.Public); + final List lbRules = new ArrayList<>(); + createLoadBalancingRulesList(lbRules, lbs); + s_logger.debug("Found " + lbRules.size() + " load balancing rule(s) to apply as a part of VPC VR " + domainRouterVO + " start."); + if (!lbRules.isEmpty()) { + for (final Pair nicNtwk : guestNics) { + final Nic guestNic = nicNtwk.first(); + final long guestNetworkId = guestNic.getNetworkId(); + if (_networkModel.isProviderSupportServiceInNetwork(guestNetworkId, Service.Lb, provider)) { + _commandSetupHelper.createApplyLoadBalancingRulesCommands(lbRules, domainRouterVO, cmds, guestNetworkId); + break; + } + } + } + } + @Override protected List getDefaultServicesToMonitor(NetworkVO network) { if (network.getTrafficType() == TrafficType.Public) {