fix(ec2): allocate requested secondary addresses when creating ENIs (#8576)

This commit is contained in:
luiseterc authored and GitHub committed 2026-11-16 11:30:50 -04:00
1 parent 3e52c94e39
commit 48be04cc21
6 files changed
+221 -4

No files matched your search

@@ -5561,7 +5561,8 @@ public class Ec2QueryHandler {
Integer ipv6AddressCount = parseOptionalInt(p.getFirst("Ipv6AddressCount"), "Ipv6AddressCount");
NetworkInterface ni = service.createNetworkInterface(region, subnetId, description,
privateIpAddress, privateIpAddresses, ipv6Addresses, ipv6AddressCount, securityGroupIds, tagList);
privateIpAddress, privateIpAddresses, ipv6Addresses, ipv6AddressCount, securityGroupIds, tagList,
intParam(p, "SecondaryPrivateIpAddressCount"));
XmlBuilder xml = new XmlBuilder()
.start("CreateNetworkInterfaceResponse", AwsNamespaces.EC2)
@@ -16130,7 +16130,23 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
String privateIpAddress, List<String> privateIpAddresses,
List<String> securityGroupIds, List<Tag> tagList) {
return createNetworkInterface(region, subnetId, description, privateIpAddress, privateIpAddresses,
List.of(), null, securityGroupIds, tagList);
List.of(), null, securityGroupIds, tagList, null);
}
public NetworkInterface createNetworkInterface(String region, String subnetId, String description,
String privateIpAddress, List<String> privateIpAddresses,
List<String> securityGroupIds, List<Tag> tagList,
Integer secondaryCount) {
return createNetworkInterface(region, subnetId, description, privateIpAddress, privateIpAddresses,
List.of(), null, securityGroupIds, tagList, secondaryCount);
}
public NetworkInterface createNetworkInterface(String region, String subnetId, String description,
String privateIpAddress, List<String> privateIpAddresses,
List<String> ipv6Addresses, Integer ipv6AddressCount,
List<String> securityGroupIds, List<Tag> tagList) {
return createNetworkInterface(region, subnetId, description, privateIpAddress, privateIpAddresses,
ipv6Addresses, ipv6AddressCount, securityGroupIds, tagList, null);
}
/**
@@ -16140,7 +16156,15 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
public NetworkInterface createNetworkInterface(String region, String subnetId, String description,
String privateIpAddress, List<String> privateIpAddresses,
List<String> ipv6Addresses, Integer ipv6AddressCount,
List<String> securityGroupIds, List<Tag> tagList) {
List<String> securityGroupIds, List<Tag> tagList,
Integer secondaryCount) {
if (secondaryCount != null && secondaryCount < 0) {
throw new AwsException("InvalidParameterValue", "SecondaryPrivateIpAddressCount must not be negative", 400);
}
if (secondaryCount != null && privateIpAddresses != null && !privateIpAddresses.isEmpty()) {
throw new AwsException("InvalidParameterCombination",
"SecondaryPrivateIpAddressCount cannot be combined with explicit secondary addresses", 400);
}
if (subnetId == null || subnetId.isBlank()) {
throw new AwsException("MissingParameter",
"The request must contain the parameter SubnetId", 400);
@@ -16190,6 +16214,23 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
}
List<String> ipv6 = ipv6AddressesForNewInterface(region, subnet, ipv6Addresses, ipv6AddressCount);
String primaryIp = requestedIp != null ? requestedIp : assignPrivateIp(region, subnetId);
List<String> allocated = new ArrayList<>();
if (requestedIp == null) {
allocated.add(primaryIp);
}
Set<String> pending = new LinkedHashSet<>(secondaries);
pending.add(primaryIp);
try {
for (int i = 0; i < (secondaryCount == null ? 0 : secondaryCount); i++) {
String extra = assignPrivateIp(region, subnetId, pending);
allocated.add(extra);
pending.add(extra);
secondaries.add(extra);
}
} catch (AwsException e) {
releasePrivateIps(region, subnetId, allocated);
throw e;
}
String primaryDns = AwsRegions.ec2PrivateIpDnsName(primaryIp, region);
NetworkInterface ni = new NetworkInterface();
@@ -16900,7 +16941,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
releaseInterfaceAddresses(region, networkInterfaceId, true);
networkInterfaces.delete(key(region, networkInterfaceId));
removeTags(region, networkInterfaceId);
releasePrivateIps(region, ni.getSubnetId(), List.of(ni.getPrivateIpAddress()));
releasePrivateIps(region, ni.getSubnetId(), ni.getPrivateIpAddresses().stream()
.map(NetworkInterfacePrivateIpAddress::getPrivateIpAddress).toList());
}
}