mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(ec2): give back the VPC network leases of a NAT gateway's addresses (#7581)
An address the subnet picks for a NAT gateway takes a lease on the VPC network, and DeleteNatGateway never gave it back, so once the gateway was gone automatic allocation skipped that address. Secondary addresses leaked the same way, on delete and when an update dropped them. DeleteNatGateway now gives back the lease of every address the gateway holds. It reads the gateway once, under the allocation lock, and gives nothing back for a gateway stored as deleted, whose addresses an earlier delete already gave back. A secondary-address update gives back the lease of each dropped secondary, except one the same update names again in a new slot. Every place in Ec2Service that gives leases back now goes through one releasePrivateIps helper. In Ec2ServiceTest, a MockedNetworkFixture replaces the setup the mocked-network tests repeated, and the older tests that built that setup by hand use it too.
This commit is contained in:
1 parent
56eedbece5
commit
1a44e7215d
2 files changed
+244
-123
No files matched your search
@@ -4534,11 +4534,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
addresses.add(assignPrivateIp(region, subnetId, addresses));
|
||||
}
|
||||
} catch (AwsException e) {
|
||||
if (vpcNetworkManager != null) {
|
||||
for (String address : addresses) {
|
||||
vpcNetworkManager.releasePrivateIp(region, subnetId, address);
|
||||
}
|
||||
}
|
||||
releasePrivateIps(region, subnetId, addresses);
|
||||
throw e;
|
||||
}
|
||||
return addresses;
|
||||
@@ -4548,6 +4544,19 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
return assignPrivateIp(region, subnetId, List.of());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gives back the VPC network's lease on each address, so automatic allocation can offer it
|
||||
* again. Callers must hold {@link #privateIpAllocationLock}.
|
||||
*/
|
||||
private void releasePrivateIps(String region, String subnetId, Collection<String> addresses) {
|
||||
if (vpcNetworkManager == null) {
|
||||
return;
|
||||
}
|
||||
for (String address : addresses) {
|
||||
vpcNetworkManager.releasePrivateIp(region, subnetId, address);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Callers must hold {@link #privateIpAllocationLock}, so an address found free here cannot be
|
||||
* taken before the resource it is handed to is stored.
|
||||
@@ -4575,11 +4584,9 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
allocated = vpcNetworkManager.allocatePrivateIp(region, subnetId);
|
||||
}
|
||||
// Given back only once the walk is over, or the network would offer them again. Their
|
||||
// holders took no lease, and deleting an interface or NAT gateway returns none, so a
|
||||
// lease kept here would outlive the holder.
|
||||
for (String address : skipped) {
|
||||
vpcNetworkManager.releasePrivateIp(region, subnetId, address);
|
||||
}
|
||||
// holders took no lease, and deleting an interface returns none, so a lease kept here
|
||||
// would outlive the holder.
|
||||
releasePrivateIps(region, subnetId, skipped);
|
||||
if (allocated.isPresent()) {
|
||||
return allocated.get();
|
||||
}
|
||||
@@ -4663,10 +4670,10 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
* lease to show for it: an interface created with its own address never takes one, and a
|
||||
* restart re-leases only instances. Any other subnet id belongs to one account, so for it the
|
||||
* wider scan finds nothing more. A lease with nothing in any account behind it does not fail
|
||||
* the launch: it is stale (DeleteNetworkInterface and DeleteNatGateway never return theirs),
|
||||
* or it belongs to a launch that failed and has not given the address back yet, which it does
|
||||
* once its container is detached. Either way the instance keeps the address without a lease of
|
||||
* its own, and {@link #assignPrivateIp} still skips it, because the stored instance holds it.
|
||||
* the launch: it is stale (DeleteNetworkInterface never returns its lease), or it belongs to
|
||||
* a launch that failed and has not given the address back yet, which it does once its
|
||||
* container is detached. Either way the instance keeps the address without a lease of its
|
||||
* own, and {@link #assignPrivateIp} still skips it, because the stored instance holds it.
|
||||
* An address outside a substituted subnet's effective range is never leased at all, in which
|
||||
* case the instance keeps it the way it keeps a synthesised address.
|
||||
*/
|
||||
@@ -4679,10 +4686,10 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
/**
|
||||
* The in-use half of {@link #claimRequestedPrivateIp}, without the lease. CreateNetworkInterface
|
||||
* and CreateNatGateway use it alone: neither an interface nor a NAT gateway has a container to
|
||||
* hold the address on the VPC network, and DeleteNetworkInterface and DeleteNatGateway return
|
||||
* no lease, so one taken here would keep the address from automatic allocation after its
|
||||
* holder is gone. {@link #assignPrivateIp} already steps around addresses stored interfaces and
|
||||
* NAT gateways hold. Callers must hold {@link #privateIpAllocationLock}.
|
||||
* hold the address on the VPC network, and {@link #assignPrivateIp} already steps around
|
||||
* addresses stored interfaces and NAT gateways hold. DeleteNetworkInterface returns no lease,
|
||||
* so one taken here would keep the address from automatic allocation after the interface is
|
||||
* gone. Callers must hold {@link #privateIpAllocationLock}.
|
||||
*
|
||||
* @return whether the address lies in a Docker-backed range, where a lease could be taken
|
||||
*/
|
||||
@@ -12599,9 +12606,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
slots[i].setPrimary(false);
|
||||
}
|
||||
} catch (AwsException e) {
|
||||
if (vpcNetworkManager != null) {
|
||||
assigned.forEach(ip -> vpcNetworkManager.releasePrivateIp(region, subnetId, ip));
|
||||
}
|
||||
releasePrivateIps(region, subnetId, assigned);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
@@ -12917,9 +12922,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
InstanceConnectEndpoint endpoint = withTags(requireInstanceConnectEndpoint(region, id));
|
||||
synchronized (privateIpAllocationLock) {
|
||||
instanceConnectEndpoints.delete(key(region, id));
|
||||
if (vpcNetworkManager != null) {
|
||||
vpcNetworkManager.releasePrivateIp(region, endpoint.getSubnetId(), endpoint.getPrivateIpAddress());
|
||||
}
|
||||
releasePrivateIps(region, endpoint.getSubnetId(),
|
||||
Collections.singletonList(endpoint.getPrivateIpAddress()));
|
||||
}
|
||||
removeTags(region, id);
|
||||
endpoint.getNetworkInterfaceIds().forEach(eniId -> removeTags(region, eniId));
|
||||
@@ -12979,27 +12983,38 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
|
||||
public NatGateway deleteNatGateway(String region, String natGatewayId) {
|
||||
ensureDefaultResources(region);
|
||||
NatGateway natGateway = getRequiredNatGateway(region, natGatewayId);
|
||||
// Hand the Elastic IP back, but only if the association is still the gateway's own. Under
|
||||
// the lock createNatGateway takes, so a concurrent create cannot have its association wiped.
|
||||
// Read under the lock createNatGateway and updateNatGatewaySecondaryAddresses take, so what
|
||||
// is given back is what the gateway holds now. The Elastic IP is handed back only if the
|
||||
// association is still the gateway's own, so a concurrent create cannot have it wiped.
|
||||
synchronized (privateIpAllocationLock) {
|
||||
NatGateway natGateway = getRequiredNatGateway(region, natGatewayId);
|
||||
// A CreateTags write that read the gateway before an earlier delete can store it back
|
||||
// marked deleted. That delete gave its addresses back, and the network may have leased
|
||||
// them to something else since. A requested address took no lease of its own. The one it
|
||||
// can carry is either a stale lease DeleteNetworkInterface left behind, which should go,
|
||||
// or a failed launch's, given back once its container is detached; giving that back
|
||||
// sooner matters only if the allocator wraps around to it in between.
|
||||
if (!"deleted".equals(natGateway.getState())) {
|
||||
releasePrivateIps(region, natGateway.getSubnetId(), natGateway.getNatGatewayAddresses().stream()
|
||||
.map(NatGatewayAddress::getPrivateIp).toList());
|
||||
}
|
||||
natGateway.setState("deleted");
|
||||
natGateway.getNatGatewayAddresses().forEach(a -> releaseNatGatewayEip(region, a));
|
||||
natGateways.delete(key(region, natGatewayId));
|
||||
removeTags(region, natGatewayId);
|
||||
natGateway.getNatGatewayAddresses().forEach(a -> removeTags(region, a.getNetworkInterfaceId()));
|
||||
return natGateway;
|
||||
}
|
||||
return natGateway;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a gateway's secondary addresses to the ones given, as AssociateNatGatewayAddress,
|
||||
* DisassociateNatGatewayAddress, AssignPrivateNatGatewayAddress and
|
||||
* UnassignPrivateNatGatewayAddress together would, keeping the gateway and its id: a secondary
|
||||
* the request still names is kept, one it no longer names is dropped and its Elastic IP handed
|
||||
* back, and a new one is checked as {@link #createNatGateway} checks it. Every check runs before
|
||||
* anything changes, so a refused request leaves the gateway as it was. The arguments mean what
|
||||
* they mean to {@link #createNatGateway}.
|
||||
* the request still names is kept, one it no longer names is dropped and its Elastic IP and
|
||||
* private address handed back, and a new one is checked as {@link #createNatGateway} checks it.
|
||||
* Every check runs before anything changes, so a refused request leaves the gateway as it was.
|
||||
* The arguments mean what they mean to {@link #createNatGateway}.
|
||||
*/
|
||||
public NatGateway updateNatGatewaySecondaryAddresses(String region, String natGatewayId,
|
||||
List<String> secondaryAllocationIds,
|
||||
@@ -13084,6 +13099,12 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
.filter(NatGatewayAddress::isPrimary).toList());
|
||||
addresses.addAll(List.of(result));
|
||||
addresses.forEach(a -> holdNatGatewayEip(region, a));
|
||||
// A dropped address that a new slot names again is still the gateway's, so it keeps
|
||||
// its lease.
|
||||
Set<String> heldIps = addresses.stream().map(NatGatewayAddress::getPrivateIp)
|
||||
.collect(Collectors.toSet());
|
||||
releasePrivateIps(region, subnetId, dropped.stream().map(NatGatewayAddress::getPrivateIp)
|
||||
.filter(ip -> !heldIps.contains(ip)).toList());
|
||||
natGateway.setNatGatewayAddresses(addresses);
|
||||
natGateways.put(key(region, natGatewayId), natGateway);
|
||||
return natGateway;
|
||||
|
||||
@@ -96,6 +96,7 @@ import java.util.Base64;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -142,6 +143,7 @@ import static org.mockito.ArgumentMatchers.argThat;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.CALLS_REAL_METHODS;
|
||||
import static org.mockito.Mockito.RETURNS_SELF;
|
||||
import static org.mockito.Mockito.atLeast;
|
||||
import static org.mockito.Mockito.atLeastOnce;
|
||||
import static org.mockito.Mockito.clearInvocations;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
@@ -2740,37 +2742,161 @@ class Ec2ServiceTest {
|
||||
*/
|
||||
@Test
|
||||
void aNatGatewayCreateThatRunsOutOfAddressesGivesBackTheLeaseItTook() {
|
||||
VpcNetworkManager vpcNetworks = mock(VpcNetworkManager.class);
|
||||
Ec2Service service = mockedVpcNetworkService(vpcNetworks);
|
||||
String subnetId = privateSubnet(service, "us-east-1", "10.60.0.0/28");
|
||||
when(vpcNetworks.allocatePrivateIp("us-east-1", subnetId))
|
||||
.thenReturn(Optional.of("10.60.0.5"), Optional.empty());
|
||||
MockedNetworkFixture fixture = mockedNetworkFixture("10.60.0.0/28", "10.60.0.5");
|
||||
|
||||
AwsException e = assertThrows(AwsException.class, () -> service.createNatGateway("us-east-1", subnetId,
|
||||
null, "private", null, List.of(), List.of(), "31", List.of()));
|
||||
AwsException e = assertThrows(AwsException.class,
|
||||
() -> privateNatGateway(fixture.service(), fixture.subnetId(), "31"));
|
||||
assertEquals("InsufficientFreeAddressesInSubnet", e.getErrorCode());
|
||||
verify(vpcNetworks).releasePrivateIp("us-east-1", subnetId, "10.60.0.5");
|
||||
assertTrue(service.describeNatGateways("us-east-1", List.of(), Map.of()).isEmpty());
|
||||
verify(fixture.vpcNetworks()).releasePrivateIp("us-east-1", fixture.subnetId(), "10.60.0.5");
|
||||
assertTrue(fixture.service().describeNatGateways("us-east-1", List.of(), Map.of()).isEmpty());
|
||||
}
|
||||
|
||||
/** The same on a secondary-address update, which also leaves the gateway as it was. */
|
||||
@Test
|
||||
void aNatGatewayUpdateThatRunsOutOfAddressesGivesBackTheLeaseItTook() {
|
||||
VpcNetworkManager vpcNetworks = mock(VpcNetworkManager.class);
|
||||
Ec2Service service = mockedVpcNetworkService(vpcNetworks);
|
||||
String subnetId = privateSubnet(service, "us-east-1", "10.60.0.0/28");
|
||||
when(vpcNetworks.allocatePrivateIp("us-east-1", subnetId))
|
||||
.thenReturn(Optional.of("10.60.0.6"), Optional.of("10.60.0.7"), Optional.empty());
|
||||
NatGateway nat = service.createNatGateway("us-east-1", subnetId, null, "private", List.of());
|
||||
MockedNetworkFixture fixture = mockedNetworkFixture("10.60.0.0/28", "10.60.0.6", "10.60.0.7");
|
||||
NatGateway nat = privateNatGateway(fixture.service(), fixture.subnetId(), null);
|
||||
assertEquals("10.60.0.6", nat.getNatGatewayAddresses().getFirst().getPrivateIp());
|
||||
|
||||
AwsException e = assertThrows(AwsException.class, () -> service.updateNatGatewaySecondaryAddresses(
|
||||
AwsException e = assertThrows(AwsException.class, () -> fixture.service().updateNatGatewaySecondaryAddresses(
|
||||
"us-east-1", nat.getNatGatewayId(), List.of(), List.of(), "31"));
|
||||
assertEquals("InsufficientFreeAddressesInSubnet", e.getErrorCode());
|
||||
verify(vpcNetworks).releasePrivateIp("us-east-1", subnetId, "10.60.0.7");
|
||||
assertEquals(List.of("10.60.0.6"), service.describeNatGateways("us-east-1", List.of(nat.getNatGatewayId()),
|
||||
Map.of()).getFirst().getNatGatewayAddresses().stream().map(NatGatewayAddress::getPrivateIp).toList(),
|
||||
"the gateway keeps only its primary address");
|
||||
verify(fixture.vpcNetworks()).releasePrivateIp("us-east-1", fixture.subnetId(), "10.60.0.7");
|
||||
assertEquals(List.of("10.60.0.6"), natGatewayPrivateIps(fixture.service().describeNatGateways("us-east-1",
|
||||
List.of(nat.getNatGatewayId()), Map.of()).getFirst()), "the gateway keeps only its primary address");
|
||||
}
|
||||
|
||||
/** DeleteNatGateway gives back the lease of every address the subnet picked, primary and secondary. */
|
||||
@Test
|
||||
void deletingANatGatewayGivesBackTheLeasesOfItsAddresses() {
|
||||
MockedNetworkFixture fixture = mockedNetworkFixture("10.60.0.0/28", "10.60.0.6", "10.60.0.7");
|
||||
String natId = privateNatGateway(fixture.service(), fixture.subnetId(), "1").getNatGatewayId();
|
||||
assertEquals(List.of(), fixture.released());
|
||||
|
||||
fixture.service().deleteNatGateway("us-east-1", natId);
|
||||
|
||||
assertEquals(List.of("10.60.0.6", "10.60.0.7"), fixture.released());
|
||||
}
|
||||
|
||||
/**
|
||||
* The same against the real allocator, which sweeps forward and comes back for a given-back
|
||||
* address only once it has walked the whole range. A /28 has five addresses to hand out, .10 to
|
||||
* .14, and the gateway takes all of them. The launch in between finds the network dry and gets
|
||||
* a synthesised address, which moves the fallback past .10, so the launch after the delete gets
|
||||
* .10 only if the network offers it again.
|
||||
*/
|
||||
@Test
|
||||
void anAddressADeletedNatGatewayHeldIsHandedOutAgainOnceTheWalkWrapsAround() {
|
||||
Ec2Service service = realVpcNetworkService(new RequestContext());
|
||||
String subnetId = privateSubnet(service, "us-east-1", "10.60.0.0/28");
|
||||
NatGateway nat = privateNatGateway(service, subnetId, "4");
|
||||
assertEquals(List.of("10.60.0.10", "10.60.0.11", "10.60.0.12", "10.60.0.13", "10.60.0.14"),
|
||||
natGatewayPrivateIps(nat));
|
||||
assertEquals(List.of("10.60.0.4"), launchAddresses(service, subnetId, 1));
|
||||
|
||||
service.deleteNatGateway("us-east-1", nat.getNatGatewayId());
|
||||
|
||||
assertEquals(List.of("10.60.0.10"), launchAddresses(service, subnetId, 1));
|
||||
}
|
||||
|
||||
/** A secondary an update drops gives back its lease; the addresses the gateway keeps do not. */
|
||||
@Test
|
||||
void aSecondaryAddressAnUpdateDropsGivesBackItsLease() {
|
||||
MockedNetworkFixture fixture = mockedNetworkFixture("10.60.0.0/28", "10.60.0.6", "10.60.0.7", "10.60.0.8");
|
||||
String natId = privateNatGateway(fixture.service(), fixture.subnetId(), "2").getNatGatewayId();
|
||||
|
||||
NatGateway updated = fixture.service().updateNatGatewaySecondaryAddresses("us-east-1", natId, List.of(),
|
||||
List.of(), "1");
|
||||
|
||||
assertEquals(List.of("10.60.0.6", "10.60.0.8"), natGatewayPrivateIps(updated));
|
||||
assertEquals(List.of("10.60.0.7"), fixture.released());
|
||||
}
|
||||
|
||||
/**
|
||||
* A secondary moved to another Elastic IP is dropped and built again, and when the new slot names
|
||||
* the same address the gateway still holds it, so its lease stays.
|
||||
*/
|
||||
@Test
|
||||
void aDroppedSecondaryAddressTheSameUpdateNamesAgainKeepsItsLease() {
|
||||
MockedNetworkFixture fixture = mockedNetworkFixture("10.60.0.0/28", "10.60.0.6", "10.60.0.7");
|
||||
String primary = fixture.service().allocateAddress("us-east-1").getAllocationId();
|
||||
String first = fixture.service().allocateAddress("us-east-1").getAllocationId();
|
||||
String second = fixture.service().allocateAddress("us-east-1").getAllocationId();
|
||||
String natId = fixture.service().createNatGateway("us-east-1", fixture.subnetId(), primary, "public", null,
|
||||
List.of(first), List.of(), null, List.of()).getNatGatewayId();
|
||||
|
||||
NatGateway updated = fixture.service().updateNatGatewaySecondaryAddresses("us-east-1", natId,
|
||||
List.of(second), List.of("10.60.0.7"), null);
|
||||
|
||||
NatGatewayAddress secondary = updated.getNatGatewayAddresses().get(1);
|
||||
assertEquals(Map.entry(second, "10.60.0.7"), Map.entry(secondary.getAllocationId(), secondary.getPrivateIp()));
|
||||
assertEquals(List.of(), fixture.released());
|
||||
}
|
||||
|
||||
/**
|
||||
* A tag write that read the gateway before a delete can store it back marked deleted. The delete
|
||||
* already gave its addresses back, and the network may have leased them to something else since,
|
||||
* so deleting that record again gives nothing back.
|
||||
*/
|
||||
@Test
|
||||
void deletingAGatewayStoredAsDeletedDoesNotGiveItsLeasesBackAgain() {
|
||||
AccountAwareStorageBackend<NatGateway> natGatewayStore = AccountAwareStorageBackend.inMemory("000000000000");
|
||||
MockedNetworkFixture fixture = mockedNetworkFixture(Map.of("ec2-nat-gateways.json", natGatewayStore),
|
||||
"10.60.0.0/28", "10.60.0.6");
|
||||
String natId = privateNatGateway(fixture.service(), fixture.subnetId(), null).getNatGatewayId();
|
||||
NatGateway deleted = fixture.service().deleteNatGateway("us-east-1", natId);
|
||||
natGatewayStore.putForAccount("000000000000", "us-east-1::" + natId, deleted);
|
||||
|
||||
fixture.service().deleteNatGateway("us-east-1", natId);
|
||||
|
||||
assertEquals(List.of("10.60.0.6"), fixture.released());
|
||||
}
|
||||
|
||||
/**
|
||||
* A subnet of {@code cidr} in mock mode, over a mocked VPC network that picks {@code picked} in
|
||||
* order and then runs dry, and that reports no Docker-backed range until told to.
|
||||
*/
|
||||
private static MockedNetworkFixture mockedNetworkFixture(String cidr, String... picked) {
|
||||
return mockedNetworkFixture(Map.of(), cidr, picked);
|
||||
}
|
||||
|
||||
/** {@link #mockedNetworkFixture(String, String...)}, with {@code stores} in place of the stores they name. */
|
||||
private static MockedNetworkFixture mockedNetworkFixture(Map<String, AccountAwareStorageBackend<?>> stores,
|
||||
String cidr, String... picked) {
|
||||
VpcNetworkManager vpcNetworks = mock(VpcNetworkManager.class);
|
||||
Ec2Service service = mockedVpcNetworkService(stores, vpcNetworks);
|
||||
String subnetId = privateSubnet(service, "us-east-1", cidr);
|
||||
Iterator<String> next = List.of(picked).iterator();
|
||||
when(vpcNetworks.allocatePrivateIp("us-east-1", subnetId))
|
||||
.thenAnswer(invocation -> next.hasNext() ? Optional.of(next.next()) : Optional.empty());
|
||||
return new MockedNetworkFixture(vpcNetworks, service, subnetId, cidr);
|
||||
}
|
||||
|
||||
private record MockedNetworkFixture(VpcNetworkManager vpcNetworks, Ec2Service service, String subnetId,
|
||||
String cidr) {
|
||||
|
||||
/** Has the network report the whole subnet as its Docker-backed range, where addresses are leased. */
|
||||
MockedNetworkFixture withDockerBackedRange() {
|
||||
when(vpcNetworks.effectiveSubnetCidr("us-east-1", subnetId)).thenReturn(Optional.of(cidr));
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Every address whose lease was given back to the VPC network, in the order it was. */
|
||||
List<String> released() {
|
||||
ArgumentCaptor<String> addresses = ArgumentCaptor.forClass(String.class);
|
||||
verify(vpcNetworks, atLeast(0)).releasePrivateIp(eq("us-east-1"), eq(subnetId), addresses.capture());
|
||||
return addresses.getAllValues();
|
||||
}
|
||||
}
|
||||
|
||||
/** A private NAT gateway with {@code secondaryCount} more addresses the subnet picks, or none when null. */
|
||||
private static NatGateway privateNatGateway(Ec2Service service, String subnetId, String secondaryCount) {
|
||||
return service.createNatGateway("us-east-1", subnetId, null, "private", null, List.of(), List.of(),
|
||||
secondaryCount, List.of());
|
||||
}
|
||||
|
||||
private static List<String> natGatewayPrivateIps(NatGateway natGateway) {
|
||||
return natGateway.getNatGatewayAddresses().stream().map(NatGatewayAddress::getPrivateIp).toList();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -4640,18 +4766,15 @@ class Ec2ServiceTest {
|
||||
|
||||
@Test
|
||||
void aRequestedPrivateIpIsLeasedOnTheVpcNetworkInsteadOfTheNextFreeAddress() {
|
||||
VpcNetworkManager vpcNetworks = mock(VpcNetworkManager.class);
|
||||
Ec2Service service = mockedVpcNetworkService(vpcNetworks);
|
||||
String subnetId = privateIpSubnet(service);
|
||||
when(vpcNetworks.effectiveSubnetCidr("us-east-1", subnetId)).thenReturn(Optional.of("10.50.1.0/24"));
|
||||
when(vpcNetworks.claimPrivateIp("us-east-1", subnetId, "10.50.1.50")).thenReturn(true);
|
||||
MockedNetworkFixture fixture = mockedNetworkFixture("10.50.1.0/24").withDockerBackedRange();
|
||||
when(fixture.vpcNetworks().claimPrivateIp("us-east-1", fixture.subnetId(), "10.50.1.50")).thenReturn(true);
|
||||
|
||||
assertEquals("10.50.1.50", launchWithPrivateIp(service, subnetId, 1, "10.50.1.50")
|
||||
assertEquals("10.50.1.50", launchWithPrivateIp(fixture.service(), fixture.subnetId(), 1, "10.50.1.50")
|
||||
.getInstances().getFirst().getPrivateIpAddress());
|
||||
|
||||
verify(vpcNetworks).claimPrivateIp("us-east-1", subnetId, "10.50.1.50");
|
||||
verify(vpcNetworks, never()).reservePrivateIp(anyString(), anyString(), anyString());
|
||||
verify(vpcNetworks, never()).allocatePrivateIp(anyString(), anyString());
|
||||
verify(fixture.vpcNetworks()).claimPrivateIp("us-east-1", fixture.subnetId(), "10.50.1.50");
|
||||
verify(fixture.vpcNetworks(), never()).reservePrivateIp(anyString(), anyString(), anyString());
|
||||
verify(fixture.vpcNetworks(), never()).allocatePrivateIp(anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -4792,18 +4915,16 @@ class Ec2ServiceTest {
|
||||
|
||||
@Test
|
||||
void aDryRunWithARequestedPrivateIpLeasesNothing() {
|
||||
VpcNetworkManager vpcNetworks = mock(VpcNetworkManager.class);
|
||||
Ec2Service service = mockedVpcNetworkService(vpcNetworks);
|
||||
String subnetId = privateIpSubnet(service);
|
||||
MockedNetworkFixture fixture = mockedNetworkFixture("10.50.1.0/24");
|
||||
|
||||
AwsException dryRun = assertThrows(AwsException.class,
|
||||
() -> service.runInstances("us-east-1", "ami-1234567890abcdef0", "t3.micro", 1, 1, null,
|
||||
List.of(), subnetId, null, List.of(), null, null, null, null, 0, null, null, null, null,
|
||||
true, null, null, null, false, "10.50.1.50"));
|
||||
() -> fixture.service().runInstances("us-east-1", "ami-1234567890abcdef0", "t3.micro", 1, 1,
|
||||
null, List.of(), fixture.subnetId(), null, List.of(), null, null, null, null, 0, null, null,
|
||||
null, null, true, null, null, null, false, "10.50.1.50"));
|
||||
|
||||
assertEquals("DryRunOperation", dryRun.getErrorCode());
|
||||
verify(vpcNetworks, never()).claimPrivateIp(anyString(), anyString(), anyString());
|
||||
assertTrue(instancesIn(service, subnetId).isEmpty());
|
||||
verify(fixture.vpcNetworks(), never()).claimPrivateIp(anyString(), anyString(), anyString());
|
||||
assertTrue(instancesIn(fixture.service(), fixture.subnetId()).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -4821,22 +4942,17 @@ class Ec2ServiceTest {
|
||||
|
||||
@Test
|
||||
void aNatGatewaysRequestedPrivateIpTakesNoLeaseAndIsStillNotHandedOut() {
|
||||
// DeleteNatGateway returns no lease, so one taken for the gateway's PrivateIpAddress would
|
||||
// keep the address from the network's allocator after the gateway is gone. The walk steps
|
||||
// around it while the stored gateway holds it.
|
||||
VpcNetworkManager vpcNetworks = mock(VpcNetworkManager.class);
|
||||
Ec2Service service = mockedVpcNetworkService(vpcNetworks);
|
||||
String subnetId = privateIpSubnet(service);
|
||||
when(vpcNetworks.effectiveSubnetCidr("us-east-1", subnetId)).thenReturn(Optional.of("10.50.1.0/24"));
|
||||
// A NAT gateway has no container to hold its PrivateIpAddress on the network, so it takes no
|
||||
// lease for it. The walk steps around it while the stored gateway holds it.
|
||||
MockedNetworkFixture fixture = mockedNetworkFixture("10.50.1.0/24", "10.50.1.10", "10.50.1.11")
|
||||
.withDockerBackedRange();
|
||||
|
||||
assertEquals("10.50.1.10", service.createNatGateway("us-east-1", subnetId, null, "private",
|
||||
"10.50.1.10", List.of()).getNatGatewayAddresses().getFirst().getPrivateIp());
|
||||
verify(vpcNetworks, never()).claimPrivateIp(anyString(), anyString(), anyString());
|
||||
verify(vpcNetworks, never()).reservePrivateIp(anyString(), anyString(), anyString());
|
||||
assertEquals("10.50.1.10", fixture.service().createNatGateway("us-east-1", fixture.subnetId(), null,
|
||||
"private", "10.50.1.10", List.of()).getNatGatewayAddresses().getFirst().getPrivateIp());
|
||||
verify(fixture.vpcNetworks(), never()).claimPrivateIp(anyString(), anyString(), anyString());
|
||||
verify(fixture.vpcNetworks(), never()).reservePrivateIp(anyString(), anyString(), anyString());
|
||||
|
||||
when(vpcNetworks.allocatePrivateIp("us-east-1", subnetId))
|
||||
.thenReturn(Optional.of("10.50.1.10"), Optional.of("10.50.1.11"), Optional.empty());
|
||||
assertEquals(List.of("10.50.1.11"), launchAddresses(service, subnetId, 1));
|
||||
assertEquals(List.of("10.50.1.11"), launchAddresses(fixture.service(), fixture.subnetId(), 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -4858,33 +4974,26 @@ class Ec2ServiceTest {
|
||||
|
||||
@Test
|
||||
void anAddressTheWalkSkipsIsGivenBackToTheVpcNetwork() {
|
||||
// Its holder took no lease, and deleting an interface or NAT gateway returns none, so a
|
||||
// lease the walk kept would outlive the holder.
|
||||
VpcNetworkManager vpcNetworks = mock(VpcNetworkManager.class);
|
||||
Ec2Service service = mockedVpcNetworkService(vpcNetworks);
|
||||
String subnetId = privateIpSubnet(service);
|
||||
service.createNetworkInterface("us-east-1", subnetId, "holder", "10.50.1.10",
|
||||
// Its holder took no lease, and deleting an interface returns none, so a lease the walk
|
||||
// kept would outlive the holder.
|
||||
MockedNetworkFixture fixture = mockedNetworkFixture("10.50.1.0/24", "10.50.1.10", "10.50.1.11");
|
||||
fixture.service().createNetworkInterface("us-east-1", fixture.subnetId(), "holder", "10.50.1.10",
|
||||
List.of(), List.of(), List.of());
|
||||
when(vpcNetworks.allocatePrivateIp("us-east-1", subnetId))
|
||||
.thenReturn(Optional.of("10.50.1.10"), Optional.of("10.50.1.11"), Optional.empty());
|
||||
|
||||
assertEquals(List.of("10.50.1.11"), launchAddresses(service, subnetId, 1));
|
||||
verify(vpcNetworks).releasePrivateIp("us-east-1", subnetId, "10.50.1.10");
|
||||
verify(vpcNetworks, never()).releasePrivateIp("us-east-1", subnetId, "10.50.1.11");
|
||||
assertEquals(List.of("10.50.1.11"), launchAddresses(fixture.service(), fixture.subnetId(), 1));
|
||||
assertEquals(List.of("10.50.1.10"), fixture.released());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anInterfaceRefusedForAHeldSecondaryLeasesNoPrimaryOnTheVpcNetwork() {
|
||||
VpcNetworkManager vpcNetworks = mock(VpcNetworkManager.class);
|
||||
Ec2Service service = mockedVpcNetworkService(vpcNetworks);
|
||||
String subnetId = privateIpSubnet(service);
|
||||
service.createNetworkInterface("us-east-1", subnetId, "holder", "10.50.1.70",
|
||||
MockedNetworkFixture fixture = mockedNetworkFixture("10.50.1.0/24", "10.50.1.10");
|
||||
fixture.service().createNetworkInterface("us-east-1", fixture.subnetId(), "holder", "10.50.1.70",
|
||||
List.of(), List.of(), List.of());
|
||||
when(vpcNetworks.allocatePrivateIp("us-east-1", subnetId)).thenReturn(Optional.of("10.50.1.10"));
|
||||
|
||||
assertEquals("InvalidIPAddress.InUse", assertThrows(AwsException.class, () -> service.createNetworkInterface(
|
||||
"us-east-1", subnetId, "clash", null, List.of("10.50.1.70"), List.of(), List.of())).getErrorCode());
|
||||
verify(vpcNetworks, never()).allocatePrivateIp(anyString(), anyString());
|
||||
assertEquals("InvalidIPAddress.InUse", assertThrows(AwsException.class, () -> fixture.service()
|
||||
.createNetworkInterface("us-east-1", fixture.subnetId(), "clash", null, List.of("10.50.1.70"),
|
||||
List.of(), List.of())).getErrorCode());
|
||||
verify(fixture.vpcNetworks(), never()).allocatePrivateIp(anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -4912,15 +5021,12 @@ class Ec2ServiceTest {
|
||||
void anAutoAssignedPrimaryEqualToARequestedSecondaryIsListedOnceAsPrimary() {
|
||||
// The secondary is free and not stored yet, so the network may hand the same address out
|
||||
// as the primary; the interface must not then list it a second time as a secondary.
|
||||
VpcNetworkManager vpcNetworks = mock(VpcNetworkManager.class);
|
||||
Ec2Service service = mockedVpcNetworkService(vpcNetworks);
|
||||
String subnetId = privateIpSubnet(service);
|
||||
when(vpcNetworks.allocatePrivateIp("us-east-1", subnetId)).thenReturn(Optional.of("10.50.1.10"));
|
||||
MockedNetworkFixture fixture = mockedNetworkFixture("10.50.1.0/24", "10.50.1.10");
|
||||
|
||||
String eniId = service.createNetworkInterface("us-east-1", subnetId, "overlap", null,
|
||||
String eniId = fixture.service().createNetworkInterface("us-east-1", fixture.subnetId(), "overlap", null,
|
||||
List.of("10.50.1.10"), List.of(), List.of()).getNetworkInterfaceId();
|
||||
|
||||
NetworkInterface stored = service.describeNetworkInterfaces("us-east-1", List.of(eniId), Map.of())
|
||||
NetworkInterface stored = fixture.service().describeNetworkInterfaces("us-east-1", List.of(eniId), Map.of())
|
||||
.networkInterfaces().getFirst();
|
||||
assertEquals("10.50.1.10", stored.getPrivateIpAddress());
|
||||
assertEquals(List.of(Map.entry("10.50.1.10", true)), stored.getPrivateIpAddresses().stream()
|
||||
@@ -4953,16 +5059,12 @@ class Ec2ServiceTest {
|
||||
// back. A launch that names the address in between finds nothing holding it and keeps it
|
||||
// although the lease is refused, and the late release then leaves the network offering an
|
||||
// address that launch's instance holds.
|
||||
VpcNetworkManager vpcNetworks = mock(VpcNetworkManager.class);
|
||||
Ec2Service service = mockedVpcNetworkService(vpcNetworks);
|
||||
String subnetId = privateIpSubnet(service);
|
||||
when(vpcNetworks.effectiveSubnetCidr("us-east-1", subnetId)).thenReturn(Optional.of("10.50.1.0/24"));
|
||||
when(vpcNetworks.claimPrivateIp("us-east-1", subnetId, "10.50.1.50")).thenReturn(false);
|
||||
launchWithPrivateIp(service, subnetId, 1, "10.50.1.50");
|
||||
when(vpcNetworks.allocatePrivateIp("us-east-1", subnetId))
|
||||
.thenReturn(Optional.of("10.50.1.50"), Optional.of("10.50.1.51"), Optional.empty());
|
||||
MockedNetworkFixture fixture = mockedNetworkFixture("10.50.1.0/24", "10.50.1.50", "10.50.1.51")
|
||||
.withDockerBackedRange();
|
||||
when(fixture.vpcNetworks().claimPrivateIp("us-east-1", fixture.subnetId(), "10.50.1.50")).thenReturn(false);
|
||||
launchWithPrivateIp(fixture.service(), fixture.subnetId(), 1, "10.50.1.50");
|
||||
|
||||
assertEquals(List.of("10.50.1.51"), launchAddresses(service, subnetId, 1));
|
||||
assertEquals(List.of("10.50.1.51"), launchAddresses(fixture.service(), fixture.subnetId(), 1));
|
||||
}
|
||||
|
||||
private static String privateIpSubnet(Ec2Service service) {
|
||||
@@ -5045,12 +5147,10 @@ class Ec2ServiceTest {
|
||||
contexts, new VpcNetworkManager(config, docker, null), null, null, null);
|
||||
}
|
||||
|
||||
/** Ec2Service in mock mode over {@code vpcNetworks}, called as the default account. */
|
||||
private static Ec2Service mockedVpcNetworkService(VpcNetworkManager vpcNetworks) {
|
||||
return mockedVpcNetworkService(Map.of(), vpcNetworks);
|
||||
}
|
||||
|
||||
/** {@link #mockedVpcNetworkService(VpcNetworkManager)}, with {@code stores} in place of the stores they name. */
|
||||
/**
|
||||
* Ec2Service in mock mode over {@code vpcNetworks}, called as the default account, with
|
||||
* {@code stores} in place of the stores they name.
|
||||
*/
|
||||
private static Ec2Service mockedVpcNetworkService(Map<String, AccountAwareStorageBackend<?>> stores,
|
||||
VpcNetworkManager vpcNetworks) {
|
||||
return mockedVpcNetworkService(new RequestContext(), stores, vpcNetworks);
|
||||
|
||||
Reference in new issue
Block a user