Skip to content

Commit 78eda30

Browse files
committed
extension: apply VPC source NAT in implement(network) for VPC tiers
implementVpc() is called from startVpc()/restartVpc() which may run before any tier network exists (e.g. at initial VPC creation time). In that case it cannot find an anchor network and skips source NAT assignment. Fix: also apply the VPC-level source NAT IP inside implement(network) when the network belongs to a VPC. The script is idempotent so calling assign-ip multiple times (once per tier implement, and again from implementVpc on restart) is harmless.
1 parent 0dc7e32 commit 78eda30

1 file changed

Lines changed: 24 additions & 10 deletions

File tree

framework/extensions/src/main/java/org/apache/cloudstack/framework/extensions/network/NetworkExtensionElement.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -452,17 +452,31 @@ public boolean implement(Network network, NetworkOffering offering, DeployDestin
452452
return false;
453453
}
454454

455-
// Step 3: Configure source NAT for non-VPC networks.
456-
// VPC source NAT is managed at implementVpc().
457-
if (network.getVpcId() == null && canHandle(network, Service.SourceNat)) {
458-
try {
459-
Account owner = accountService.getAccount(network.getAccountId());
460-
PublicIpAddress existingIp = networkModel.getSourceNatIpAddressForGuestNetwork(owner, network);
461-
if (existingIp != null) {
462-
applyIps(network, List.of(existingIp), Set.of(Service.SourceNat));
455+
// Step 3: Configure source NAT.
456+
if (canHandle(network, Service.SourceNat)) {
457+
if (network.getVpcId() == null) {
458+
// Isolated network: apply the network's own source NAT IP.
459+
try {
460+
Account owner = accountService.getAccount(network.getAccountId());
461+
PublicIpAddress existingIp = networkModel.getSourceNatIpAddressForGuestNetwork(owner, network);
462+
if (existingIp != null) {
463+
applyIps(network, List.of(existingIp), Set.of(Service.SourceNat));
464+
}
465+
} catch (Exception e) {
466+
logger.warn("Failed to configure source NAT IP for network {}: {}", network.getId(), e.getMessage(), e);
467+
}
468+
} else {
469+
// VPC tier: apply the VPC-level source NAT IP.
470+
// implementVpc() may have been called before any tier existed (no-op then),
471+
// so we eagerly apply it here on every tier implement; the script is idempotent.
472+
try {
473+
final PublicIpAddress vpcSourceNatIp = getVpcSourceNatIp(network.getVpcId());
474+
if (vpcSourceNatIp != null) {
475+
applyIps(network, List.of(vpcSourceNatIp), Set.of(Service.SourceNat));
476+
}
477+
} catch (Exception e) {
478+
logger.warn("Failed to configure VPC source NAT IP for VPC tier network {}: {}", network.getId(), e.getMessage(), e);
463479
}
464-
} catch (Exception e) {
465-
logger.warn("Failed to configure source NAT IP for network {}: {}", network.getId(), e.getMessage(), e);
466480
}
467481
}
468482

0 commit comments

Comments
 (0)