mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ec2): give each account's VPC its own Docker network (#8598)
VpcNetworkManager keyed its VPC bindings, subnets and Docker networks by Region and id alone, and every account's default VPC and subnets share one id (vpc-default-<region>), so two accounts' default VPCs shared one binding, one Docker network and one lease table: EC2 instances, Lambda functions, ECS tasks, EKS nodes and the MWAA, CodeBuild and Batch containers of two accounts landed on the same bridge. Ec2Service worked around the shared lease table by counting every account's resources as holding the subnet's addresses. - VpcNetworkManager takes the account on every call and keys bindings and subnets by account, Region and id. A network carries the account in its labels and, for any account but the default one, in its name; the default account's keeps the name it always had, so its live networks outlive the upgrade. A surviving network of another account's VPC of the same id is a collision, as another Region's is. - Startup reconciliation asks which accounts declare a VPC of an id in a Region (DeclaredVpcs). A labelled network is kept while its account declares the VPC; one an older version labelled with no account belongs to the account declaring its VPC, the default account first, and a non-default account's VPC keeps using that account-less network, so its live containers stay attached across the upgrade. - Ec2Service passes the caller's account, and the restore each stored VPC's, subnet's and instance's own. The helpers other services call (allocate, reserve, release, attach, detach) take the owning account and run as it, as a caller may run outside the owner's request: a provisioned or cross-account Lambda launch, a container removed in the background. Lambda, ECS, EKS and VpcContainerLeases (MWAA, CodeBuild, Batch) pass the account they already hold; Lambda's and EKS's leases carry it to their release. - An address is held by the caller's account's resources alone, as its network and lease table are its own: the every-account holding check goes, and the container lease map is keyed by account. VpcNetworkManagerTest covers two accounts' default VPCs getting a network each (the second's identical CIDR substituted), another account's surviving network as a collision, a labelled network kept only while its account declares the VPC, and an older version's unlabelled network kept and adopted for the account declaring it. Ec2ServiceTest's cross-account tests now pin the per-account holding. ec2.md says so.
This commit is contained in:
1 parent
06a5ce4219
commit
bb6b308fdb
20 files changed
+776
-649
No files matched your search
@@ -287,8 +287,8 @@ class BatchDockerRunnerTest {
|
||||
subnet.setSubnetId("subnet-a");
|
||||
subnet.setVpcId("vpc-1");
|
||||
when(ec2.describeSubnets("us-east-1", List.of("subnet-a"), Map.of())).thenReturn(List.of(subnet));
|
||||
when(ec2.allocateVpcPrivateIp("us-east-1", "subnet-a")).thenReturn(Optional.of("10.0.1.30"));
|
||||
when(ec2.attachContainerToVpc("us-east-1", "vpc-1", "subnet-a", "container-id", "10.0.1.30"))
|
||||
when(ec2.allocateVpcPrivateIp("000000000000", "us-east-1", "subnet-a")).thenReturn(Optional.of("10.0.1.30"));
|
||||
when(ec2.attachContainerToVpc("000000000000", "us-east-1", "vpc-1", "subnet-a", "container-id", "10.0.1.30"))
|
||||
.thenReturn(Optional.of("10.0.1.30"));
|
||||
RegionResolver regionResolver = mock(RegionResolver.class);
|
||||
when(regionResolver.getAccountId()).thenReturn("000000000000");
|
||||
@@ -327,10 +327,10 @@ class BatchDockerRunnerTest {
|
||||
assertEquals(0, result.exitCode());
|
||||
// The job's command is the entrypoint, so the container is on the VPC network before it starts.
|
||||
InOrder order = inOrder(ec2, lifecycleManager);
|
||||
order.verify(ec2).attachContainerToVpc("us-east-1", "vpc-1", "subnet-a", "container-id", "10.0.1.30");
|
||||
order.verify(ec2).attachContainerToVpc("000000000000", "us-east-1", "vpc-1", "subnet-a", "container-id", "10.0.1.30");
|
||||
order.verify(lifecycleManager).startCreated(eq("container-id"), any());
|
||||
assertTrue(heldWhileRunning.get(), "the job's address holds its subnet while it runs");
|
||||
verify(ec2).releaseVpcPrivateIp("us-east-1", "subnet-a", "10.0.1.30");
|
||||
verify(ec2).releaseVpcPrivateIp("000000000000", "us-east-1", "subnet-a", "10.0.1.30");
|
||||
assertFalse(runner.hasSubnetDependency("us-east-1", "subnet-a"));
|
||||
}
|
||||
|
||||
|
||||
@@ -95,8 +95,8 @@ class CodeBuildRunnerSourceFailureTest {
|
||||
subnet.setSubnetId("subnet-1");
|
||||
subnet.setVpcId("vpc-1");
|
||||
when(ec2.describeSubnets("us-east-1", List.of("subnet-1"), Map.of())).thenReturn(List.of(subnet));
|
||||
when(ec2.allocateVpcPrivateIp("us-east-1", "subnet-1")).thenReturn(Optional.of("10.0.1.20"));
|
||||
when(ec2.attachContainerToVpc("us-east-1", "vpc-1", "subnet-1", "container", "10.0.1.20"))
|
||||
when(ec2.allocateVpcPrivateIp("000000000000", "us-east-1", "subnet-1")).thenReturn(Optional.of("10.0.1.20"));
|
||||
when(ec2.attachContainerToVpc("000000000000", "us-east-1", "vpc-1", "subnet-1", "container", "10.0.1.20"))
|
||||
.thenReturn(Optional.of("10.0.1.20"));
|
||||
Fixture fixture = fixture(false, ec2);
|
||||
// StartBuild copied the project's vpcConfig onto the build; the project has since been updated.
|
||||
@@ -115,7 +115,7 @@ class CodeBuildRunnerSourceFailureTest {
|
||||
|
||||
assertTrue(heldDuringCopy.get(), "the build's address holds its subnet and VPC while it runs");
|
||||
// The address is given back right after the container is removed, on the build's thread.
|
||||
verify(ec2, timeout(5_000)).releaseVpcPrivateIp("us-east-1", "subnet-1", "10.0.1.20");
|
||||
verify(ec2, timeout(5_000)).releaseVpcPrivateIp("000000000000", "us-east-1", "subnet-1", "10.0.1.20");
|
||||
assertFalse(fixture.runner().hasSubnetDependency("us-east-1", "subnet-1"));
|
||||
}
|
||||
|
||||
|
||||
@@ -1609,7 +1609,7 @@ class Ec2ContainerManagerTest {
|
||||
|
||||
awaitUntil(() -> "terminated".equals(instance.getState().getName()), Duration.ofSeconds(2));
|
||||
verify(harness.vpcNetworkManager, timeout(2000).atLeastOnce())
|
||||
.releasePrivateIp("us-west-2", "subnet-lease", "10.0.1.10");
|
||||
.releasePrivateIp(null, "us-west-2", "subnet-lease", "10.0.1.10");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -1624,9 +1624,9 @@ class Ec2ContainerManagerTest {
|
||||
awaitUntil(() -> "terminated".equals(instance.getState().getName()), Duration.ofSeconds(2));
|
||||
// Detach before release: Docker holds the address while the endpoint stands, so handing it
|
||||
// to the next launch before disconnecting would have the daemon refuse it.
|
||||
verify(harness.vpcNetworkManager, timeout(2000)).detach("us-west-2", "vpc-lease", null);
|
||||
verify(harness.vpcNetworkManager, timeout(2000)).detach(null, "us-west-2", "vpc-lease", null);
|
||||
verify(harness.vpcNetworkManager, timeout(2000))
|
||||
.releasePrivateIp("us-west-2", "subnet-lease", "10.0.1.10");
|
||||
.releasePrivateIp(null, "us-west-2", "subnet-lease", "10.0.1.10");
|
||||
}
|
||||
|
||||
private static Instance leasedInstance(String instanceId) {
|
||||
|
||||
@@ -113,7 +113,6 @@ import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.BiPredicate;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
@@ -2881,7 +2880,7 @@ class Ec2ServiceTest {
|
||||
AwsException e = assertThrows(AwsException.class,
|
||||
() -> privateNatGateway(fixture.service(), fixture.subnetId(), "31"));
|
||||
assertEquals("InsufficientFreeAddressesInSubnet", e.getErrorCode());
|
||||
verify(fixture.vpcNetworks()).releasePrivateIp("us-east-1", fixture.subnetId(), "10.60.0.5");
|
||||
verify(fixture.vpcNetworks()).releasePrivateIp("000000000000", "us-east-1", fixture.subnetId(), "10.60.0.5");
|
||||
assertTrue(fixture.service().describeNatGateways("us-east-1", List.of(), Map.of()).isEmpty());
|
||||
}
|
||||
|
||||
@@ -2895,7 +2894,7 @@ class Ec2ServiceTest {
|
||||
AwsException e = assertThrows(AwsException.class, () -> fixture.service().updateNatGatewaySecondaryAddresses(
|
||||
"us-east-1", nat.getNatGatewayId(), List.of(), List.of(), "31"));
|
||||
assertEquals("InsufficientFreeAddressesInSubnet", e.getErrorCode());
|
||||
verify(fixture.vpcNetworks()).releasePrivateIp("us-east-1", fixture.subnetId(), "10.60.0.7");
|
||||
verify(fixture.vpcNetworks()).releasePrivateIp("000000000000", "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");
|
||||
}
|
||||
@@ -3001,7 +3000,7 @@ class Ec2ServiceTest {
|
||||
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))
|
||||
when(vpcNetworks.allocatePrivateIp("000000000000", "us-east-1", subnetId))
|
||||
.thenAnswer(invocation -> next.hasNext() ? Optional.of(next.next()) : Optional.empty());
|
||||
return new MockedNetworkFixture(vpcNetworks, service, subnetId, cidr);
|
||||
}
|
||||
@@ -3011,14 +3010,14 @@ class Ec2ServiceTest {
|
||||
|
||||
/** 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));
|
||||
when(vpcNetworks.effectiveSubnetCidr("000000000000", "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());
|
||||
verify(vpcNetworks, atLeast(0)).releasePrivateIp(eq("000000000000"), eq("us-east-1"), eq(subnetId), addresses.capture());
|
||||
return addresses.getAllValues();
|
||||
}
|
||||
}
|
||||
@@ -5078,14 +5077,14 @@ class Ec2ServiceTest {
|
||||
@Test
|
||||
void aRequestedPrivateIpIsLeasedOnTheVpcNetworkInsteadOfTheNextFreeAddress() {
|
||||
MockedNetworkFixture fixture = mockedNetworkFixture("10.50.1.0/24").withDockerBackedRange();
|
||||
when(fixture.vpcNetworks().claimPrivateIp("us-east-1", fixture.subnetId(), "10.50.1.50")).thenReturn(true);
|
||||
when(fixture.vpcNetworks().claimPrivateIp("000000000000", "us-east-1", fixture.subnetId(), "10.50.1.50")).thenReturn(true);
|
||||
|
||||
assertEquals("10.50.1.50", launchWithPrivateIp(fixture.service(), fixture.subnetId(), 1, "10.50.1.50")
|
||||
.getInstances().getFirst().getPrivateIpAddress());
|
||||
|
||||
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());
|
||||
verify(fixture.vpcNetworks()).claimPrivateIp("000000000000", "us-east-1", fixture.subnetId(), "10.50.1.50");
|
||||
verify(fixture.vpcNetworks(), never()).reservePrivateIp(anyString(), anyString(), anyString(), anyString());
|
||||
verify(fixture.vpcNetworks(), never()).allocatePrivateIp(anyString(), anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -5099,9 +5098,7 @@ class Ec2ServiceTest {
|
||||
String subnetId = Ec2Service.defaultSubnetId("us-east-1", "a");
|
||||
context.setAccountId("111122223333");
|
||||
service.describeSubnets("us-east-1", List.of(), Map.of());
|
||||
String stale = fixture.vpcNetworks().allocatePrivateIp("us-east-1", subnetId).orElseThrow();
|
||||
|
||||
context.setAccountId("000000000000");
|
||||
String stale = fixture.vpcNetworks().allocatePrivateIp("111122223333", "us-east-1", subnetId).orElseThrow();
|
||||
|
||||
assertEquals(stale, launchWithPrivateIp(service, subnetId, 1, stale)
|
||||
.getInstances().getFirst().getPrivateIpAddress());
|
||||
@@ -5109,10 +5106,10 @@ class Ec2ServiceTest {
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"instance", "network-interface", "nat-gateway"})
|
||||
void aRequestedPrivateIpAnotherAccountHoldsOnASharedVpcNetworkIsInUse(String holder) {
|
||||
// Default VPC and subnet ids are the same in every account, so their Docker network and
|
||||
// lease table are shared while the stored-resource check sees only the caller's account.
|
||||
// Claiming the address anyway would give the other account's lease away.
|
||||
void aRequestedPrivateIpAnotherAccountHoldsOnItsOwnDefaultVpcIsFreeInThisOne(String holder) {
|
||||
// Default VPC and subnet ids are the same in every account, but each account's VPC has its own
|
||||
// Docker network and lease table, as each account's VPC is its own on AWS: what the other
|
||||
// account holds on its network does not hold this account's address.
|
||||
RequestContext context = new RequestContext();
|
||||
Ec2Service service = realVpcNetworkService(context);
|
||||
String subnetId = Ec2Service.defaultSubnetId("us-east-1", "a");
|
||||
@@ -5125,21 +5122,17 @@ class Ec2ServiceTest {
|
||||
default -> service.createNatGateway("us-east-1", subnetId, null, "private", List.of())
|
||||
.getNatGatewayAddresses().getFirst().getPrivateIp();
|
||||
};
|
||||
assertEquals("172.31.0.10", held, "the shared network's first address, leased by the other account");
|
||||
assertEquals("172.31.0.10", held, "the first address of the other account's network");
|
||||
|
||||
context.setAccountId("000000000000");
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> launchWithPrivateIp(service, subnetId, 1, held));
|
||||
|
||||
assertEquals("InvalidIPAddress.InUse", error.getErrorCode());
|
||||
assertEquals("Address " + held + " is in use.", error.getMessage());
|
||||
assertTrue(instancesIn(service, subnetId).isEmpty(), "a rejected launch must store nothing");
|
||||
assertEquals(held, launchWithPrivateIp(service, subnetId, 1, held)
|
||||
.getInstances().getFirst().getPrivateIpAddress());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anAddressAnotherAccountsInterfaceWasCreatedWithIsInUseOnASharedVpcNetwork() {
|
||||
// CreateNetworkInterface with its own address takes no lease, so only the stored
|
||||
// interface says the shared network's address is held.
|
||||
void anAddressAnotherAccountsInterfaceWasCreatedWithIsFreeInThisAccountsDefaultVpc() {
|
||||
// The other account's interface holds the address on that account's network alone.
|
||||
RequestContext context = new RequestContext();
|
||||
Ec2Service service = realVpcNetworkService(context);
|
||||
String subnetId = Ec2Service.defaultSubnetId("us-east-1", "a");
|
||||
@@ -5149,17 +5142,15 @@ class Ec2ServiceTest {
|
||||
List.of(), List.of(), List.of());
|
||||
|
||||
context.setAccountId("000000000000");
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> launchWithPrivateIp(service, subnetId, 1, "172.31.0.50"));
|
||||
|
||||
assertEquals("InvalidIPAddress.InUse", error.getErrorCode());
|
||||
assertTrue(instancesIn(service, subnetId).isEmpty(), "a rejected launch must store nothing");
|
||||
assertEquals("172.31.0.50", launchWithPrivateIp(service, subnetId, 1, "172.31.0.50")
|
||||
.getInstances().getFirst().getPrivateIpAddress());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anotherAccountsNatGatewayStillHoldsItsAddressAfterARestartEmptiesTheLeaseTable() {
|
||||
// A restart re-leases only instances' addresses, so a persisted NAT gateway's address is
|
||||
// held by the stored gateway alone.
|
||||
void anotherAccountsNatGatewayDoesNotHoldThisAccountsAddressAfterARestart() {
|
||||
// The other account's persisted NAT gateway holds its address in that account's default VPC,
|
||||
// whose network is its own.
|
||||
String subnetId = Ec2Service.defaultSubnetId("us-east-1", "a");
|
||||
NatGatewayAddress address = new NatGatewayAddress();
|
||||
address.setPrivateIp("172.31.0.10");
|
||||
@@ -5174,11 +5165,8 @@ class Ec2ServiceTest {
|
||||
context.setAccountId("000000000000");
|
||||
Ec2Service service = realVpcNetworkService(context, Map.of("ec2-nat-gateways.json", natGatewayStore));
|
||||
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> launchWithPrivateIp(service, subnetId, 1, "172.31.0.10"));
|
||||
|
||||
assertEquals("InvalidIPAddress.InUse", error.getErrorCode());
|
||||
assertTrue(instancesIn(service, subnetId).isEmpty(), "a rejected launch must store nothing");
|
||||
assertEquals("172.31.0.10", launchWithPrivateIp(service, subnetId, 1, "172.31.0.10")
|
||||
.getInstances().getFirst().getPrivateIpAddress());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -5190,7 +5178,7 @@ class Ec2ServiceTest {
|
||||
instanceStore.putForAccount("111122223333", "us-east-1::i-other",
|
||||
defaultSubnetInstance("i-other", "172.31.0.50", InstanceState.running()));
|
||||
VpcNetworkManager vpcNetworks = mock(VpcNetworkManager.class);
|
||||
when(vpcNetworks.effectiveSubnetCidr("us-east-1", subnetId)).thenReturn(Optional.of("10.240.0.0/20"));
|
||||
when(vpcNetworks.effectiveSubnetCidr("000000000000", "us-east-1", subnetId)).thenReturn(Optional.of("10.240.0.0/20"));
|
||||
Ec2Service service = mockedVpcNetworkService(Map.of("ec2-instances.json", instanceStore), vpcNetworks);
|
||||
|
||||
assertEquals("172.31.0.50", launchWithPrivateIp(service, subnetId, 1, "172.31.0.50")
|
||||
@@ -5232,7 +5220,7 @@ class Ec2ServiceTest {
|
||||
null, null, true, null, null, null, false, "10.50.1.50"));
|
||||
|
||||
assertEquals("DryRunOperation", dryRun.getErrorCode());
|
||||
verify(fixture.vpcNetworks(), never()).claimPrivateIp(anyString(), anyString(), anyString());
|
||||
verify(fixture.vpcNetworks(), never()).claimPrivateIp(anyString(), anyString(), anyString(), anyString());
|
||||
assertTrue(instancesIn(fixture.service(), fixture.subnetId()).isEmpty());
|
||||
}
|
||||
|
||||
@@ -5258,16 +5246,16 @@ class Ec2ServiceTest {
|
||||
|
||||
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());
|
||||
verify(fixture.vpcNetworks(), never()).claimPrivateIp(anyString(), anyString(), anyString(), anyString());
|
||||
verify(fixture.vpcNetworks(), never()).reservePrivateIp(anyString(), anyString(), anyString(), anyString());
|
||||
|
||||
assertEquals(List.of("10.50.1.11"), launchAddresses(fixture.service(), fixture.subnetId(), 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anAddressAnotherAccountsInterfaceWasCreatedWithIsNotHandedOutOnASharedVpcNetwork() {
|
||||
// Default subnet ids are the same in every account, so their Docker network is shared, and
|
||||
// only the other account's stored interface says its address is held.
|
||||
void anAddressAnotherAccountsInterfaceWasCreatedWithIsHandedOutInThisAccountsDefaultVpc() {
|
||||
// The other account's interface holds its address on that account's network, so this
|
||||
// account's own default VPC network hands the same address out.
|
||||
RequestContext context = new RequestContext();
|
||||
Ec2Service service = realVpcNetworkService(context);
|
||||
String subnetId = Ec2Service.defaultSubnetId("us-east-1", "a");
|
||||
@@ -5278,7 +5266,7 @@ class Ec2ServiceTest {
|
||||
|
||||
context.setAccountId("000000000000");
|
||||
|
||||
assertEquals(List.of("172.31.0.11"), launchAddresses(service, subnetId, 1));
|
||||
assertEquals(List.of("172.31.0.10"), launchAddresses(service, subnetId, 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -5349,7 +5337,7 @@ class Ec2ServiceTest {
|
||||
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());
|
||||
verify(fixture.vpcNetworks(), never()).allocatePrivateIp(anyString(), anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -5390,9 +5378,8 @@ class Ec2ServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void anAddressAnotherAccountHoldsIsNotHandedOutWhenTheWalkRunsTheNetworkDry() {
|
||||
// The walk skips the only address the shared network still offers, which another account's
|
||||
// interface holds, and the synthesised fallback's own scan sees only the caller's account.
|
||||
void anAddressAnotherAccountHoldsIsHandedOutOnThisAccountsOwnNetwork() {
|
||||
// The other account's interface holds the address on that account's network, not on this one's.
|
||||
RequestContext context = new RequestContext();
|
||||
VpcNetworkManager vpcNetworks = mock(VpcNetworkManager.class);
|
||||
Ec2Service service = mockedVpcNetworkService(context, Map.of(), vpcNetworks);
|
||||
@@ -5401,12 +5388,12 @@ class Ec2ServiceTest {
|
||||
service.describeSubnets("us-east-1", List.of(), Map.of());
|
||||
service.createNetworkInterface("us-east-1", subnetId, "holder", "172.31.0.10",
|
||||
List.of(), List.of(), List.of());
|
||||
when(vpcNetworks.allocatePrivateIp("us-east-1", subnetId))
|
||||
when(vpcNetworks.allocatePrivateIp("000000000000", "us-east-1", subnetId))
|
||||
.thenReturn(Optional.of("172.31.0.10"), Optional.empty());
|
||||
|
||||
context.setAccountId("000000000000");
|
||||
|
||||
assertEquals(List.of("172.31.0.11"), launchAddresses(service, subnetId, 1));
|
||||
assertEquals(List.of("172.31.0.10"), launchAddresses(service, subnetId, 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -5418,11 +5405,11 @@ class Ec2ServiceTest {
|
||||
service.describeSubnets("us-east-1", List.of(), Map.of());
|
||||
service.createNetworkInterface("us-east-1", subnetId, "holder", "172.31.0.10",
|
||||
List.of(), List.of(), List.of());
|
||||
when(vpcNetworks.allocatePrivateIp("us-east-1", subnetId))
|
||||
when(vpcNetworks.allocatePrivateIp("000000000000", "us-east-1", subnetId))
|
||||
.thenReturn(Optional.of("172.31.0.10"), Optional.of("172.31.0.11"));
|
||||
|
||||
assertEquals(Optional.of("172.31.0.11"), service.allocateVpcPrivateIp("us-east-1", subnetId));
|
||||
verify(vpcNetworks).releasePrivateIp("us-east-1", subnetId, "172.31.0.10");
|
||||
assertEquals(Optional.of("172.31.0.11"), service.allocateVpcPrivateIp("000000000000", "us-east-1", subnetId));
|
||||
verify(vpcNetworks).releasePrivateIp("000000000000", "us-east-1", subnetId, "172.31.0.10");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -5431,9 +5418,9 @@ class Ec2ServiceTest {
|
||||
Ec2Service service = mockedVpcNetworkService(new RequestContext(), Map.of(), vpcNetworks);
|
||||
String subnetId = Ec2Service.defaultSubnetId("us-east-1", "a");
|
||||
service.describeSubnets("us-east-1", List.of(), Map.of());
|
||||
when(vpcNetworks.allocatePrivateIp("us-east-1", subnetId))
|
||||
when(vpcNetworks.allocatePrivateIp("000000000000", "us-east-1", subnetId))
|
||||
.thenReturn(Optional.of("172.31.0.10"), Optional.empty());
|
||||
service.allocateVpcPrivateIp("us-east-1", subnetId);
|
||||
service.allocateVpcPrivateIp("000000000000", "us-east-1", subnetId);
|
||||
|
||||
AwsException inUse = assertThrows(AwsException.class, () -> service.createNetworkInterface("us-east-1",
|
||||
subnetId, "named", "172.31.0.10", List.of(), List.of(), List.of()));
|
||||
@@ -5442,7 +5429,7 @@ class Ec2ServiceTest {
|
||||
assertEquals("172.31.0.11", service.createNetworkInterface("us-east-1", subnetId, "automatic", null,
|
||||
List.of(), List.of(), List.of()).getPrivateIpAddress());
|
||||
|
||||
service.releaseVpcPrivateIp("us-east-1", subnetId, "172.31.0.10");
|
||||
service.releaseVpcPrivateIp("000000000000", "us-east-1", subnetId, "172.31.0.10");
|
||||
assertEquals("172.31.0.10", service.createNetworkInterface("us-east-1", subnetId, "named", "172.31.0.10",
|
||||
List.of(), List.of(), List.of()).getPrivateIpAddress());
|
||||
}
|
||||
@@ -5458,7 +5445,7 @@ class Ec2ServiceTest {
|
||||
MockedNetworkFixture fixture = mockedNetworkFixture("10.50.1.0/28", leased.toArray(String[]::new));
|
||||
Ec2Service service = fixture.service();
|
||||
for (int i = 0; i < leased.size(); i++) {
|
||||
service.allocateVpcPrivateIp("us-east-1", fixture.subnetId());
|
||||
service.allocateVpcPrivateIp("000000000000", "us-east-1", fixture.subnetId());
|
||||
}
|
||||
String vpcId = service.describeSubnets("us-east-1", List.of(fixture.subnetId()), Map.of())
|
||||
.getFirst().getVpcId();
|
||||
@@ -5480,7 +5467,7 @@ class Ec2ServiceTest {
|
||||
// address that launch's instance holds.
|
||||
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);
|
||||
when(fixture.vpcNetworks().claimPrivateIp("000000000000", "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(fixture.service(), fixture.subnetId(), 1));
|
||||
@@ -6840,8 +6827,8 @@ class Ec2ServiceTest {
|
||||
|
||||
service.restoreMetadataRegistrations();
|
||||
|
||||
verify(vpcNetworks).reservePrivateIp("us-east-1", "subnet-a", "10.0.1.10");
|
||||
verify(vpcNetworks, never()).reservePrivateIp("us-east-1", "subnet-a", "10.0.1.11");
|
||||
verify(vpcNetworks).reservePrivateIp("000000000000", "us-east-1", "subnet-a", "10.0.1.10");
|
||||
verify(vpcNetworks, never()).reservePrivateIp("000000000000", "us-east-1", "subnet-a", "10.0.1.11");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -6875,14 +6862,14 @@ class Ec2ServiceTest {
|
||||
|
||||
service.restoreMetadataRegistrations();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<BiPredicate<String, String>> stillDeclared = ArgumentCaptor.forClass(BiPredicate.class);
|
||||
ArgumentCaptor<VpcNetworkManager.DeclaredVpcs> stillDeclared =
|
||||
ArgumentCaptor.forClass(VpcNetworkManager.DeclaredVpcs.class);
|
||||
verify(vpcNetworks).reconcileOrphans(stillDeclared.capture());
|
||||
assertTrue(stillDeclared.getValue().test("us-east-1", "vpc-a"));
|
||||
assertFalse(stillDeclared.getValue().test("us-east-1", "vpc-gone"));
|
||||
verify(vpcNetworks).declareVpc("us-east-1", "vpc-a", "10.20.0.0/16");
|
||||
verify(vpcNetworks).declareSubnet("us-east-1", "vpc-a", "subnet-a", "10.20.1.0/24");
|
||||
verify(vpcNetworks).reservePrivateIp("us-east-1", "subnet-a", "10.20.1.10");
|
||||
assertEquals(Set.of("111122223333"), stillDeclared.getValue().accountsDeclaring("us-east-1", "vpc-a"));
|
||||
assertEquals(Set.of(), stillDeclared.getValue().accountsDeclaring("us-east-1", "vpc-gone"));
|
||||
verify(vpcNetworks).declareVpc("111122223333", "us-east-1", "vpc-a", "10.20.0.0/16");
|
||||
verify(vpcNetworks).declareSubnet("111122223333", "us-east-1", "vpc-a", "subnet-a", "10.20.1.0/24");
|
||||
verify(vpcNetworks).reservePrivateIp("111122223333", "us-east-1", "subnet-a", "10.20.1.10");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -6908,9 +6895,9 @@ class Ec2ServiceTest {
|
||||
service.restoreMetadataRegistrations();
|
||||
|
||||
InOrder declared = inOrder(vpcNetworks);
|
||||
declared.verify(vpcNetworks).declareVpc("us-east-1", "vpc-x", "10.0.0.0/16");
|
||||
declared.verify(vpcNetworks).declareVpc("us-east-1", "vpc-y", "10.0.0.0/16");
|
||||
declared.verify(vpcNetworks).declareVpc("us-east-1", "vpc-z", "10.0.0.0/16");
|
||||
declared.verify(vpcNetworks).declareVpc("555555555555", "us-east-1", "vpc-x", "10.0.0.0/16");
|
||||
declared.verify(vpcNetworks).declareVpc("111122223333", "us-east-1", "vpc-y", "10.0.0.0/16");
|
||||
declared.verify(vpcNetworks).declareVpc("999999999999", "us-east-1", "vpc-z", "10.0.0.0/16");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -7027,7 +7014,7 @@ class Ec2ServiceTest {
|
||||
VpcNetworkManager vpcNetworks = mock(VpcNetworkManager.class);
|
||||
when(vpcNetworks.enabled()).thenReturn(true);
|
||||
// Second claim on the same address is refused; startup must not abort over it.
|
||||
when(vpcNetworks.reservePrivateIp(anyString(), anyString(), anyString()))
|
||||
when(vpcNetworks.reservePrivateIp(anyString(), anyString(), anyString(), anyString()))
|
||||
.thenReturn(true).thenReturn(false);
|
||||
Ec2Service service = new Ec2Service(mockConfig(false), mock(Ec2ContainerManager.class),
|
||||
mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
|
||||
@@ -7035,7 +7022,7 @@ class Ec2ServiceTest {
|
||||
new InMemoryStorageFactory(Map.of("ec2-instances.json", instanceStore)), vpcNetworks);
|
||||
|
||||
assertDoesNotThrow(service::restoreMetadataRegistrations);
|
||||
verify(vpcNetworks, times(2)).reservePrivateIp("us-east-1", "subnet-a", "10.0.1.10");
|
||||
verify(vpcNetworks, times(2)).reservePrivateIp("000000000000", "us-east-1", "subnet-a", "10.0.1.10");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -48,18 +48,18 @@ class VpcContainerLeasesTest {
|
||||
subnet.setSubnetId("subnet-1");
|
||||
subnet.setVpcId("vpc-1");
|
||||
when(ec2.describeSubnets(REGION, List.of("subnet-1"), Map.of())).thenReturn(List.of(subnet));
|
||||
when(ec2.allocateVpcPrivateIp(REGION, "subnet-1")).thenReturn(Optional.of("10.0.1.20"));
|
||||
when(ec2.allocateVpcPrivateIp(ACCOUNT, REGION, "subnet-1")).thenReturn(Optional.of("10.0.1.20"));
|
||||
leases = new VpcContainerLeases(ec2, "Test resource", () -> docker);
|
||||
}
|
||||
|
||||
@Test
|
||||
void anAttachThatFailsGivesTheLeasedAddressBack() {
|
||||
when(ec2.attachContainerToVpc(REGION, "vpc-1", "subnet-1", "c1", "10.0.1.20"))
|
||||
when(ec2.attachContainerToVpc(ACCOUNT, REGION, "vpc-1", "subnet-1", "c1", "10.0.1.20"))
|
||||
.thenThrow(new IllegalStateException("docker refused"));
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> leases.join(ACCOUNT, REGION, null, "subnet-1", "c1", "r1"));
|
||||
|
||||
verify(ec2).releaseVpcPrivateIp(REGION, "subnet-1", "10.0.1.20");
|
||||
verify(ec2).releaseVpcPrivateIp(ACCOUNT, REGION, "subnet-1", "10.0.1.20");
|
||||
assertFalse(leases.holdsSubnet(ACCOUNT, REGION, "subnet-1"));
|
||||
}
|
||||
|
||||
@@ -67,12 +67,12 @@ class VpcContainerLeasesTest {
|
||||
void aSubnetOutsideTheVpcTheResourceNamesIsNotJoined() {
|
||||
assertTrue(leases.join(ACCOUNT, REGION, "vpc-other", "subnet-1", "c1", "r1").isEmpty());
|
||||
|
||||
verify(ec2, never()).allocateVpcPrivateIp(anyString(), anyString());
|
||||
verify(ec2, never()).allocateVpcPrivateIp(anyString(), anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anAddressDockerCouldNotConfirmFreeIsGivenBackOnceTheContainerIsGone() {
|
||||
when(ec2.attachContainerToVpc(REGION, "vpc-1", "subnet-1", "c1", "10.0.1.20"))
|
||||
when(ec2.attachContainerToVpc(ACCOUNT, REGION, "vpc-1", "subnet-1", "c1", "10.0.1.20"))
|
||||
.thenReturn(Optional.of("10.0.1.20"));
|
||||
assertEquals("10.0.1.20", leases.join(ACCOUNT, REGION, "vpc-1", "subnet-1", "c1", "r1")
|
||||
.orElseThrow().address());
|
||||
@@ -92,6 +92,6 @@ class VpcContainerLeasesTest {
|
||||
|
||||
inspect.set(new NotFoundException("gone"));
|
||||
assertFalse(leases.holdsSubnet(ACCOUNT, REGION, "subnet-1"));
|
||||
verify(ec2).releaseVpcPrivateIp(REGION, "subnet-1", "10.0.1.20");
|
||||
verify(ec2).releaseVpcPrivateIp(ACCOUNT, REGION, "subnet-1", "10.0.1.20");
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
+11
-11
@@ -87,17 +87,17 @@ class VpcNetworkTeardownDockerIntegrationTest {
|
||||
for (int round = 0; round < 2; round++) {
|
||||
String vpcId = "vpc-" + round;
|
||||
String subnetId = "subnet-" + round;
|
||||
manager.declareVpc(REGION, vpcId, cidr.toString());
|
||||
manager.declareSubnet(REGION, vpcId, subnetId, cidr.toString());
|
||||
String address = manager.allocatePrivateIp(REGION, subnetId).orElseThrow();
|
||||
manager.declareVpc("000000000000", REGION, vpcId, cidr.toString());
|
||||
manager.declareSubnet("000000000000", REGION, vpcId, subnetId, cidr.toString());
|
||||
String address = manager.allocatePrivateIp("000000000000", REGION, subnetId).orElseThrow();
|
||||
assertEquals(expectedAddress, address);
|
||||
assertFalse(manager.isSubstituted(REGION, vpcId));
|
||||
assertFalse(manager.isSubstituted("000000000000", REGION, vpcId));
|
||||
|
||||
String guest = startContainer(namespace + "-guest-" + round);
|
||||
containers.add(guest);
|
||||
String networkName = manager.networkName(REGION, vpcId);
|
||||
String networkName = manager.networkName("000000000000", REGION, vpcId);
|
||||
networks.add(networkName);
|
||||
assertTrue(manager.attach(REGION, vpcId, subnetId, guest, address).isPresent());
|
||||
assertTrue(manager.attach("000000000000", REGION, vpcId, subnetId, guest, address).isPresent());
|
||||
Network network = docker.inspectNetworkCmd().withNetworkId(networkName).exec();
|
||||
assertTrue(network.getContainers().containsKey(self));
|
||||
assertEquals(address + "/24", network.getContainers().get(guest).getIpv4Address());
|
||||
@@ -105,15 +105,15 @@ class VpcNetworkTeardownDockerIntegrationTest {
|
||||
if (restored) {
|
||||
manager.shutdown();
|
||||
manager = new VpcNetworkManager(config, docker, resolver);
|
||||
manager.declareVpc(REGION, vpcId, cidr.toString());
|
||||
manager.declareSubnet(REGION, vpcId, subnetId, cidr.toString());
|
||||
assertTrue(manager.attachContainer(REGION, vpcId, guest));
|
||||
manager.declareVpc("000000000000", REGION, vpcId, cidr.toString());
|
||||
manager.declareSubnet("000000000000", REGION, vpcId, subnetId, cidr.toString());
|
||||
assertTrue(manager.attachContainer("000000000000", REGION, vpcId, guest));
|
||||
}
|
||||
|
||||
docker.removeContainerCmd(guest).withForce(true).exec();
|
||||
containers.remove(guest);
|
||||
manager.forgetSubnet(REGION, subnetId);
|
||||
manager.deleteVpcNetwork(REGION, vpcId);
|
||||
manager.forgetSubnet("000000000000", REGION, subnetId);
|
||||
manager.deleteVpcNetwork("000000000000", REGION, vpcId);
|
||||
|
||||
await().pollInterval(Duration.ofMillis(25)).atMost(Duration.ofSeconds(12))
|
||||
.untilAsserted(() -> assertThrows(NotFoundException.class,
|
||||
|
||||
+2
-2
@@ -96,7 +96,7 @@ class EcsAwsvpcTaskAddressDockerIntegrationTest {
|
||||
assertEquals("RUNNING", described.getLastStatus(), "the task must start: " + described.getStoppedReason());
|
||||
String address = described.getPrivateIpAddress();
|
||||
assertNotNull(address, "an awsvpc task must report its ENI address");
|
||||
network = vpcNetworkManager.networkNameFor(REGION, vpcId).orElseThrow();
|
||||
network = vpcNetworkManager.networkNameFor("000000000000", REGION, vpcId).orElseThrow();
|
||||
String dockerId = described.getContainers().getFirst().getDockerId();
|
||||
ContainerNetwork onVpc = dockerClient.inspectContainerCmd(dockerId).exec()
|
||||
.getNetworkSettings().getNetworks().get(network);
|
||||
@@ -127,7 +127,7 @@ class EcsAwsvpcTaskAddressDockerIntegrationTest {
|
||||
}
|
||||
if (network != null) {
|
||||
// Also disconnects the endpoint Floci gives itself on the network when it runs in a container.
|
||||
vpcNetworkManager.deleteVpcNetwork(REGION, vpcId);
|
||||
vpcNetworkManager.deleteVpcNetwork("000000000000", REGION, vpcId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+4
-4
@@ -115,7 +115,7 @@ class EcsContainerManagerTaskAddressTest {
|
||||
container("sidecar", List.of()), container("web", List.of(new PortMapping(80, 80, "tcp")))),
|
||||
List.of(), REGION);
|
||||
|
||||
verify(ec2Service).attachContainerToVpc(REGION, "vpc-1", "subnet-1", "web-id", "10.0.0.10");
|
||||
verify(ec2Service).attachContainerToVpc("000000000000", REGION, "vpc-1", "subnet-1", "web-id", "10.0.0.10");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -123,7 +123,7 @@ class EcsContainerManagerTaskAddressTest {
|
||||
manager.startTask(taskWithEni(), taskDefinition(NetworkMode.awsvpc,
|
||||
container("sidecar", List.of()), container("web", List.of())), List.of(), REGION);
|
||||
|
||||
verify(ec2Service).attachContainerToVpc(REGION, "vpc-1", "subnet-1", "sidecar-id", "10.0.0.10");
|
||||
verify(ec2Service).attachContainerToVpc("000000000000", REGION, "vpc-1", "subnet-1", "sidecar-id", "10.0.0.10");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -134,7 +134,7 @@ class EcsContainerManagerTaskAddressTest {
|
||||
manager.startTask(task, taskDefinition(NetworkMode.bridge,
|
||||
container("web", List.of(new PortMapping(80, 80, "tcp")))), List.of(), REGION);
|
||||
|
||||
verify(ec2Service, never()).attachContainerToVpc(any(), any(), any(), any(), any());
|
||||
verify(ec2Service, never()).attachContainerToVpc(any(), any(), any(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -159,7 +159,7 @@ class EcsContainerManagerTaskAddressTest {
|
||||
container("web", List.of(new PortMapping(80, 80, "tcp")))), List.of(), REGION);
|
||||
|
||||
assertEquals("eni-1", task.getNetworkInterfaceId());
|
||||
verify(ec2Service, never()).attachContainerToVpc(any(), any(), any(), any(), any());
|
||||
verify(ec2Service, never()).attachContainerToVpc(any(), any(), any(), any(), any(), any());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
|
||||
@@ -4857,7 +4857,7 @@ class EksClusterManagerTest {
|
||||
"arn:aws:" + invocation.getArgument(0) + "::" + invocation.getArgument(1) + ":" + invocation.getArgument(2));
|
||||
|
||||
ec2Service = Mockito.mock(Ec2Service.class);
|
||||
when(ec2Service.attachContainerToVpc(anyString(), anyString(), anyString(), anyString(), anyString()))
|
||||
when(ec2Service.attachContainerToVpc(anyString(), anyString(), anyString(), anyString(), anyString(), anyString()))
|
||||
.thenReturn(Optional.of("floci-vpc-1"));
|
||||
manager = new EksClusterManager(containerBuilder, lifecycleManager,
|
||||
Mockito.mock(ContainerDetector.class), portAllocator,
|
||||
@@ -4903,9 +4903,9 @@ class EksClusterManagerTest {
|
||||
when(lifecycleManager.create(any())).thenReturn("cid-1");
|
||||
when(lifecycleManager.startCreated(eq("cid-1"), any()))
|
||||
.thenReturn(new ContainerInfo("cid-1", Map.of()));
|
||||
when(ec2Service.allocateVpcPrivateIp("us-east-1", "subnet-1"))
|
||||
when(ec2Service.allocateVpcPrivateIp("000000000000", "us-east-1", "subnet-1"))
|
||||
.thenReturn(Optional.of("10.0.1.25"));
|
||||
when(ec2Service.attachContainerToVpc("us-east-1", "vpc-1", "subnet-1", "cid-1", "10.0.1.25"))
|
||||
when(ec2Service.attachContainerToVpc("000000000000", "us-east-1", "vpc-1", "subnet-1", "cid-1", "10.0.1.25"))
|
||||
.thenReturn(Optional.of("floci-vpc-1"));
|
||||
|
||||
Cluster cluster = vpcCluster("vpc-cluster", "vpc-1", List.of("subnet-1", "subnet-2"));
|
||||
@@ -4919,9 +4919,9 @@ class EksClusterManagerTest {
|
||||
assertTrue(cmdCaptor.getValue().contains("--node-ip=10.0.1.25"));
|
||||
|
||||
InOrder order = Mockito.inOrder(ec2Service, lifecycleManager);
|
||||
order.verify(ec2Service).allocateVpcPrivateIp("us-east-1", "subnet-1");
|
||||
order.verify(ec2Service).allocateVpcPrivateIp("000000000000", "us-east-1", "subnet-1");
|
||||
order.verify(lifecycleManager).create(any());
|
||||
order.verify(ec2Service).attachContainerToVpc("us-east-1", "vpc-1", "subnet-1", "cid-1", "10.0.1.25");
|
||||
order.verify(ec2Service).attachContainerToVpc("000000000000", "us-east-1", "vpc-1", "subnet-1", "cid-1", "10.0.1.25");
|
||||
order.verify(lifecycleManager).startCreated(eq("cid-1"), any());
|
||||
|
||||
Instance nodeInstance = manager.synthesizeClusterNodeInstance(
|
||||
@@ -4930,7 +4930,7 @@ class EksClusterManagerTest {
|
||||
|
||||
manager.stopCluster(cluster);
|
||||
order.verify(lifecycleManager).stopAndRemoveStrict("cid-1", null, 5, true);
|
||||
order.verify(ec2Service).releaseVpcPrivateIp("us-east-1", "subnet-1", "10.0.1.25");
|
||||
order.verify(ec2Service).releaseVpcPrivateIp("000000000000", "us-east-1", "subnet-1", "10.0.1.25");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -4939,7 +4939,7 @@ class EksClusterManagerTest {
|
||||
when(lifecycleManager.create(any())).thenReturn("cid-no-ip");
|
||||
when(lifecycleManager.startCreated(eq("cid-no-ip"), any()))
|
||||
.thenReturn(new ContainerInfo("cid-no-ip", Map.of()));
|
||||
when(ec2Service.allocateVpcPrivateIp("us-east-1", "subnet-1"))
|
||||
when(ec2Service.allocateVpcPrivateIp("000000000000", "us-east-1", "subnet-1"))
|
||||
.thenReturn(Optional.empty());
|
||||
|
||||
Cluster cluster = vpcCluster("unbacked-cluster", "vpc-1", List.of("subnet-1"));
|
||||
@@ -4950,7 +4950,7 @@ class EksClusterManagerTest {
|
||||
ArgumentCaptor<List<String>> cmdCaptor = ArgumentCaptor.forClass(List.class);
|
||||
verify(builder).withCmd(cmdCaptor.capture());
|
||||
assertTrue(cmdCaptor.getValue().stream().noneMatch(a -> a.startsWith("--node-ip")));
|
||||
verify(ec2Service, never()).attachContainerToVpc(anyString(), anyString(), anyString(), anyString(), anyString());
|
||||
verify(ec2Service, never()).attachContainerToVpc(anyString(), anyString(), anyString(), anyString(), anyString(), anyString());
|
||||
|
||||
Instance fallbackNode = manager.synthesizeClusterNodeInstance(
|
||||
cluster, "172.17.0.2", "us-east-1", "000000000000");
|
||||
@@ -4964,7 +4964,7 @@ class EksClusterManagerTest {
|
||||
when(lifecycleManager.create(any())).thenReturn("cid-recreated");
|
||||
when(lifecycleManager.startCreated(eq("cid-recreated"), any()))
|
||||
.thenReturn(new ContainerInfo("cid-recreated", Map.of()));
|
||||
when(ec2Service.reserveVpcPrivateIp("us-east-1", "subnet-1", "10.0.1.25"))
|
||||
when(ec2Service.reserveVpcPrivateIp("000000000000", "us-east-1", "subnet-1", "10.0.1.25"))
|
||||
.thenReturn(true);
|
||||
|
||||
Cluster cluster = vpcCluster("recreated", "vpc-1", List.of("subnet-1"));
|
||||
@@ -4973,7 +4973,7 @@ class EksClusterManagerTest {
|
||||
manager.restoreCluster(cluster);
|
||||
|
||||
assertEquals("10.0.1.25", cluster.getNodePrivateIp());
|
||||
verify(ec2Service, never()).allocateVpcPrivateIp(anyString(), anyString());
|
||||
verify(ec2Service, never()).allocateVpcPrivateIp(anyString(), anyString(), anyString());
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<List<String>> cmdCaptor = ArgumentCaptor.forClass(List.class);
|
||||
@@ -4981,10 +4981,10 @@ class EksClusterManagerTest {
|
||||
assertTrue(cmdCaptor.getValue().contains("--node-ip=10.0.1.25"));
|
||||
|
||||
InOrder order = Mockito.inOrder(ec2Service, lifecycleManager);
|
||||
order.verify(ec2Service).reserveVpcPrivateIp("us-east-1", "subnet-1", "10.0.1.25");
|
||||
order.verify(ec2Service).reserveVpcPrivateIp("000000000000", "us-east-1", "subnet-1", "10.0.1.25");
|
||||
order.verify(lifecycleManager).create(any());
|
||||
order.verify(ec2Service).attachContainerToVpc(
|
||||
"us-east-1", "vpc-1", "subnet-1", "cid-recreated", "10.0.1.25");
|
||||
"000000000000", "us-east-1", "vpc-1", "subnet-1", "cid-recreated", "10.0.1.25");
|
||||
order.verify(lifecycleManager).startCreated(eq("cid-recreated"), any());
|
||||
}
|
||||
|
||||
@@ -4994,9 +4994,9 @@ class EksClusterManagerTest {
|
||||
when(lifecycleManager.create(any())).thenReturn("cid-vpc", "cid-bridge");
|
||||
when(lifecycleManager.startCreated(eq("cid-bridge"), any()))
|
||||
.thenReturn(new ContainerInfo("cid-bridge", Map.of()));
|
||||
when(ec2Service.allocateVpcPrivateIp("us-east-1", "subnet-1"))
|
||||
when(ec2Service.allocateVpcPrivateIp("000000000000", "us-east-1", "subnet-1"))
|
||||
.thenReturn(Optional.of("10.0.1.25"));
|
||||
when(ec2Service.attachContainerToVpc("us-east-1", "vpc-1", "subnet-1", "cid-vpc", "10.0.1.25"))
|
||||
when(ec2Service.attachContainerToVpc("000000000000", "us-east-1", "vpc-1", "subnet-1", "cid-vpc", "10.0.1.25"))
|
||||
.thenReturn(Optional.empty());
|
||||
|
||||
Cluster cluster = vpcCluster("first-launch-fallback", "vpc-1", List.of("subnet-1"));
|
||||
@@ -5004,7 +5004,7 @@ class EksClusterManagerTest {
|
||||
|
||||
assertNull(cluster.getNodePrivateIp());
|
||||
assertEquals("cid-bridge", cluster.getContainerId());
|
||||
verify(ec2Service).releaseVpcPrivateIp("us-east-1", "subnet-1", "10.0.1.25");
|
||||
verify(ec2Service).releaseVpcPrivateIp("000000000000", "us-east-1", "subnet-1", "10.0.1.25");
|
||||
verify(lifecycleManager).removeIfExistsStrict("floci-aws-eks-first-launch-fallback", true);
|
||||
verify(lifecycleManager, Mockito.times(2)).create(any());
|
||||
verify(lifecycleManager).startCreated(eq("cid-bridge"), any());
|
||||
@@ -5024,10 +5024,10 @@ class EksClusterManagerTest {
|
||||
when(lifecycleManager.findByName("floci-eks-restore-attach-fail")).thenReturn(Optional.empty());
|
||||
when(portAllocator.allocate(6440, 6499)).thenReturn(6440);
|
||||
when(lifecycleManager.create(any())).thenReturn("cid-recreated");
|
||||
when(ec2Service.reserveVpcPrivateIp("us-east-1", "subnet-1", "10.0.1.25"))
|
||||
when(ec2Service.reserveVpcPrivateIp("000000000000", "us-east-1", "subnet-1", "10.0.1.25"))
|
||||
.thenReturn(true);
|
||||
when(ec2Service.attachContainerToVpc(
|
||||
"us-east-1", "vpc-1", "subnet-1", "cid-recreated", "10.0.1.25"))
|
||||
"000000000000", "us-east-1", "vpc-1", "subnet-1", "cid-recreated", "10.0.1.25"))
|
||||
.thenReturn(Optional.empty());
|
||||
|
||||
Cluster cluster = vpcCluster("restore-attach-fail", "vpc-1", List.of("subnet-1"));
|
||||
@@ -5036,7 +5036,7 @@ class EksClusterManagerTest {
|
||||
assertThrows(IllegalStateException.class, () -> manager.restoreCluster(cluster));
|
||||
|
||||
assertEquals("10.0.1.25", cluster.getNodePrivateIp());
|
||||
verify(ec2Service, never()).releaseVpcPrivateIp(anyString(), anyString(), anyString());
|
||||
verify(ec2Service, never()).releaseVpcPrivateIp(anyString(), anyString(), anyString(), anyString());
|
||||
verify(lifecycleManager).removeIfExistsStrict("floci-aws-eks-restore-attach-fail", true);
|
||||
verify(lifecycleManager, Mockito.times(1)).create(any());
|
||||
verify(lifecycleManager, never()).startCreated(anyString(), any());
|
||||
@@ -5054,7 +5054,7 @@ class EksClusterManagerTest {
|
||||
|
||||
manager.restoreCluster(cluster);
|
||||
|
||||
verify(ec2Service).reserveVpcPrivateIp("us-east-1", "subnet-1", "10.0.1.25");
|
||||
verify(ec2Service).reserveVpcPrivateIp("000000000000", "us-east-1", "subnet-1", "10.0.1.25");
|
||||
verify(lifecycleManager, never()).create(any());
|
||||
}
|
||||
|
||||
@@ -5063,7 +5063,7 @@ class EksClusterManagerTest {
|
||||
when(lifecycleManager.create(any())).thenReturn("cid-new");
|
||||
when(lifecycleManager.startCreated(eq("cid-new"), any()))
|
||||
.thenReturn(new ContainerInfo("cid-new", Map.of()));
|
||||
when(ec2Service.reserveVpcPrivateIp("us-east-1", "subnet-1", "10.0.1.25"))
|
||||
when(ec2Service.reserveVpcPrivateIp("000000000000", "us-east-1", "subnet-1", "10.0.1.25"))
|
||||
.thenReturn(true);
|
||||
|
||||
Cluster cluster = vpcCluster("replace-cluster", "vpc-1", List.of("subnet-1"));
|
||||
@@ -5077,7 +5077,7 @@ class EksClusterManagerTest {
|
||||
|
||||
assertEquals("cid-new", cluster.getContainerId());
|
||||
assertEquals("10.0.1.25", cluster.getNodePrivateIp());
|
||||
verify(ec2Service, never()).allocateVpcPrivateIp(anyString(), anyString());
|
||||
verify(ec2Service, never()).allocateVpcPrivateIp(anyString(), anyString(), anyString());
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<List<String>> cmdCaptor = ArgumentCaptor.forClass(List.class);
|
||||
@@ -5086,10 +5086,10 @@ class EksClusterManagerTest {
|
||||
|
||||
InOrder order = Mockito.inOrder(dockerClient, ec2Service, lifecycleManager);
|
||||
order.verify(dockerClient).stopContainerCmd("cid-old");
|
||||
order.verify(ec2Service).detachContainerFromVpc("us-east-1", "vpc-1", "cid-old");
|
||||
order.verify(ec2Service).reserveVpcPrivateIp("us-east-1", "subnet-1", "10.0.1.25");
|
||||
order.verify(ec2Service).detachContainerFromVpc("000000000000", "us-east-1", "vpc-1", "cid-old");
|
||||
order.verify(ec2Service).reserveVpcPrivateIp("000000000000", "us-east-1", "subnet-1", "10.0.1.25");
|
||||
order.verify(lifecycleManager).create(any());
|
||||
order.verify(ec2Service).attachContainerToVpc("us-east-1", "vpc-1", "subnet-1", "cid-new", "10.0.1.25");
|
||||
order.verify(ec2Service).attachContainerToVpc("000000000000", "us-east-1", "vpc-1", "subnet-1", "cid-new", "10.0.1.25");
|
||||
order.verify(lifecycleManager).startCreated(eq("cid-new"), any());
|
||||
}
|
||||
|
||||
@@ -5100,7 +5100,7 @@ class EksClusterManagerTest {
|
||||
.thenThrow(new RuntimeException("replacement failed"));
|
||||
when(lifecycleManager.adopt(eq("cid-old"), eq(List.of(6443)), any()))
|
||||
.thenReturn(new ContainerInfo("cid-old", Map.of(), Map.of(6443, 6441)));
|
||||
when(ec2Service.reserveVpcPrivateIp("us-east-1", "subnet-1", "10.0.1.25"))
|
||||
when(ec2Service.reserveVpcPrivateIp("000000000000", "us-east-1", "subnet-1", "10.0.1.25"))
|
||||
.thenReturn(true);
|
||||
|
||||
Cluster cluster = vpcCluster("rollback-cluster", "vpc-1", List.of("subnet-1"));
|
||||
@@ -5114,8 +5114,8 @@ class EksClusterManagerTest {
|
||||
|
||||
assertEquals("cid-old", cluster.getContainerId());
|
||||
assertEquals("10.0.1.25", cluster.getNodePrivateIp());
|
||||
verify(ec2Service).detachContainerFromVpc("us-east-1", "vpc-1", "cid-old");
|
||||
verify(ec2Service).attachContainerToVpc("us-east-1", "vpc-1", "subnet-1", "cid-old", "10.0.1.25");
|
||||
verify(ec2Service).detachContainerFromVpc("000000000000", "us-east-1", "vpc-1", "cid-old");
|
||||
verify(ec2Service).attachContainerToVpc("000000000000", "us-east-1", "vpc-1", "subnet-1", "cid-old", "10.0.1.25");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -5180,24 +5180,24 @@ class EksClusterManagerTest {
|
||||
null,
|
||||
null);
|
||||
|
||||
when(vpcNetworkManager.reservePrivateIp("us-east-1", "subnet-1", "10.0.1.25"))
|
||||
when(vpcNetworkManager.reservePrivateIp("000000000000", "us-east-1", "subnet-1", "10.0.1.25"))
|
||||
.thenReturn(true);
|
||||
when(vpcNetworkManager.reservePrivateIp("us-east-1", "subnet-1", "10.0.1.99"))
|
||||
when(vpcNetworkManager.reservePrivateIp("000000000000", "us-east-1", "subnet-1", "10.0.1.99"))
|
||||
.thenReturn(false);
|
||||
|
||||
assertTrue(realEc2Service.reserveVpcPrivateIp("us-east-1", "subnet-1", "10.0.1.25"));
|
||||
assertFalse(realEc2Service.reserveVpcPrivateIp("us-east-1", "subnet-1", "10.0.1.99"));
|
||||
verify(vpcNetworkManager).reservePrivateIp("us-east-1", "subnet-1", "10.0.1.25");
|
||||
verify(vpcNetworkManager).reservePrivateIp("us-east-1", "subnet-1", "10.0.1.99");
|
||||
assertTrue(realEc2Service.reserveVpcPrivateIp("000000000000", "us-east-1", "subnet-1", "10.0.1.25"));
|
||||
assertFalse(realEc2Service.reserveVpcPrivateIp("000000000000", "us-east-1", "subnet-1", "10.0.1.99"));
|
||||
verify(vpcNetworkManager).reservePrivateIp("000000000000", "us-east-1", "subnet-1", "10.0.1.25");
|
||||
verify(vpcNetworkManager).reservePrivateIp("000000000000", "us-east-1", "subnet-1", "10.0.1.99");
|
||||
|
||||
// Reserved IP is recorded in containerPrivateIps so allocateVpcPrivateIp skips it until released.
|
||||
when(vpcNetworkManager.allocatePrivateIp("us-east-1", "subnet-1"))
|
||||
when(vpcNetworkManager.allocatePrivateIp("000000000000", "us-east-1", "subnet-1"))
|
||||
.thenReturn(Optional.of("10.0.1.25"), Optional.of("10.0.1.26"));
|
||||
assertEquals(Optional.of("10.0.1.26"), realEc2Service.allocateVpcPrivateIp("us-east-1", "subnet-1"));
|
||||
verify(vpcNetworkManager).releasePrivateIp("us-east-1", "subnet-1", "10.0.1.25");
|
||||
assertEquals(Optional.of("10.0.1.26"), realEc2Service.allocateVpcPrivateIp("000000000000", "us-east-1", "subnet-1"));
|
||||
verify(vpcNetworkManager).releasePrivateIp("000000000000", "us-east-1", "subnet-1", "10.0.1.25");
|
||||
|
||||
realEc2Service.detachContainerFromVpc("us-east-1", "vpc-1", "cid-1");
|
||||
verify(vpcNetworkManager).detach("us-east-1", "vpc-1", "cid-1");
|
||||
realEc2Service.detachContainerFromVpc("000000000000", "us-east-1", "vpc-1", "cid-1");
|
||||
verify(vpcNetworkManager).detach("000000000000", "us-east-1", "vpc-1", "cid-1");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,6 @@ import com.github.dockerjava.api.model.MountType;
|
||||
import com.github.dockerjava.api.model.StreamType;
|
||||
import com.github.dockerjava.api.model.VersionComponent;
|
||||
import io.floci.aws.config.EmulatorConfig;
|
||||
import io.floci.aws.core.common.RequestScopes;
|
||||
import io.floci.aws.core.common.dns.EmbeddedDnsServer;
|
||||
import io.floci.aws.core.common.docker.ContainerBuilder;
|
||||
import io.floci.aws.core.common.docker.ContainerEngineDetector;
|
||||
@@ -56,8 +55,6 @@ import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InOrder;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockMakers;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
@@ -79,7 +76,6 @@ import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static io.floci.aws.testing.DockerDaemonVersionFixtures.stubDaemonVersion;
|
||||
@@ -1558,45 +1554,45 @@ class ContainerLauncherTest {
|
||||
@Test
|
||||
void vpcFunctionJoinsItsVpcNetworkBeforeStartAndReturnsTheAddressAfterRemoval() throws Exception {
|
||||
LambdaFunction fn = vpcFunction("vpc-join-fn");
|
||||
when(ec2Service.allocateVpcPrivateIp("us-east-1", "subnet-a")).thenReturn(Optional.of("172.31.0.20"));
|
||||
when(ec2Service.attachContainerToVpc("us-east-1", "vpc-1", "subnet-a", "container-123", "172.31.0.20"))
|
||||
when(ec2Service.allocateVpcPrivateIp("000000000000", "us-east-1", "subnet-a")).thenReturn(Optional.of("172.31.0.20"));
|
||||
when(ec2Service.attachContainerToVpc("000000000000", "us-east-1", "vpc-1", "subnet-a", "container-123", "172.31.0.20"))
|
||||
.thenReturn(Optional.of("floci-vpc-1"));
|
||||
|
||||
ContainerHandle handle = launcher.launch(fn);
|
||||
verify(ec2Service, never()).releaseVpcPrivateIp(any(), any(), any());
|
||||
verify(ec2Service, never()).releaseVpcPrivateIp(any(), any(), any(), any());
|
||||
launcher.stop(handle);
|
||||
|
||||
InOrder order = inOrder(ec2Service, lifecycleManager);
|
||||
order.verify(ec2Service).allocateVpcPrivateIp("us-east-1", "subnet-a");
|
||||
order.verify(ec2Service).attachContainerToVpc("us-east-1", "vpc-1", "subnet-a", "container-123",
|
||||
order.verify(ec2Service).allocateVpcPrivateIp("000000000000", "us-east-1", "subnet-a");
|
||||
order.verify(ec2Service).attachContainerToVpc("000000000000", "us-east-1", "vpc-1", "subnet-a", "container-123",
|
||||
"172.31.0.20");
|
||||
order.verify(lifecycleManager).startCreated(eq("container-123"), any());
|
||||
order.verify(lifecycleManager).stopAndRemoveStrict("container-123", null, 1);
|
||||
order.verify(ec2Service).releaseVpcPrivateIp("us-east-1", "subnet-a", "172.31.0.20");
|
||||
order.verify(ec2Service).releaseVpcPrivateIp("000000000000", "us-east-1", "subnet-a", "172.31.0.20");
|
||||
}
|
||||
|
||||
@Test
|
||||
void vpcFunctionKeepsItsAddressUntilARetriedRemovalSucceeds() throws Exception {
|
||||
LambdaFunction fn = vpcFunction("vpc-retry-fn");
|
||||
when(ec2Service.allocateVpcPrivateIp("us-east-1", "subnet-a")).thenReturn(Optional.of("172.31.0.23"));
|
||||
when(ec2Service.attachContainerToVpc("us-east-1", "vpc-1", "subnet-a", "container-123", "172.31.0.23"))
|
||||
when(ec2Service.allocateVpcPrivateIp("000000000000", "us-east-1", "subnet-a")).thenReturn(Optional.of("172.31.0.23"));
|
||||
when(ec2Service.attachContainerToVpc("000000000000", "us-east-1", "vpc-1", "subnet-a", "container-123", "172.31.0.23"))
|
||||
.thenReturn(Optional.of("floci-vpc-1"));
|
||||
ContainerHandle handle = launcher.launch(fn);
|
||||
doThrow(new RuntimeException("removal failed")).doNothing()
|
||||
.when(lifecycleManager).stopAndRemoveStrict("container-123", null, 1);
|
||||
|
||||
assertThrows(RuntimeException.class, () -> launcher.stop(handle));
|
||||
verify(ec2Service, never()).releaseVpcPrivateIp(any(), any(), any());
|
||||
verify(ec2Service, never()).releaseVpcPrivateIp(any(), any(), any(), any());
|
||||
|
||||
launcher.stop(handle);
|
||||
verify(ec2Service).releaseVpcPrivateIp("us-east-1", "subnet-a", "172.31.0.23");
|
||||
verify(ec2Service).releaseVpcPrivateIp("000000000000", "us-east-1", "subnet-a", "172.31.0.23");
|
||||
}
|
||||
|
||||
@Test
|
||||
void vpcFunctionReturnsTheAddressOnceAFailedLaunchRemovesItsContainer() throws Exception {
|
||||
LambdaFunction fn = vpcFunction("vpc-launch-failure-fn");
|
||||
when(ec2Service.allocateVpcPrivateIp("us-east-1", "subnet-a")).thenReturn(Optional.of("172.31.0.21"));
|
||||
when(ec2Service.attachContainerToVpc("us-east-1", "vpc-1", "subnet-a", "container-123", "172.31.0.21"))
|
||||
when(ec2Service.allocateVpcPrivateIp("000000000000", "us-east-1", "subnet-a")).thenReturn(Optional.of("172.31.0.21"));
|
||||
when(ec2Service.attachContainerToVpc("000000000000", "us-east-1", "vpc-1", "subnet-a", "container-123", "172.31.0.21"))
|
||||
.thenReturn(Optional.of("floci-vpc-1"));
|
||||
doThrow(new RuntimeException("start failed")).when(lifecycleManager).startCreated(eq("container-123"), any());
|
||||
|
||||
@@ -1604,14 +1600,14 @@ class ContainerLauncherTest {
|
||||
|
||||
InOrder order = inOrder(lifecycleManager, ec2Service);
|
||||
order.verify(lifecycleManager).stopAndRemoveStrict("container-123", null);
|
||||
order.verify(ec2Service).releaseVpcPrivateIp("us-east-1", "subnet-a", "172.31.0.21");
|
||||
order.verify(ec2Service).releaseVpcPrivateIp("000000000000", "us-east-1", "subnet-a", "172.31.0.21");
|
||||
}
|
||||
|
||||
@Test
|
||||
void vpcFunctionKeepsTheAddressWhenAFailedLaunchCannotRemoveItsContainer() throws Exception {
|
||||
LambdaFunction fn = vpcFunction("vpc-launch-removal-failure-fn");
|
||||
when(ec2Service.allocateVpcPrivateIp("us-east-1", "subnet-a")).thenReturn(Optional.of("172.31.0.24"));
|
||||
when(ec2Service.attachContainerToVpc("us-east-1", "vpc-1", "subnet-a", "container-123", "172.31.0.24"))
|
||||
when(ec2Service.allocateVpcPrivateIp("000000000000", "us-east-1", "subnet-a")).thenReturn(Optional.of("172.31.0.24"));
|
||||
when(ec2Service.attachContainerToVpc("000000000000", "us-east-1", "vpc-1", "subnet-a", "container-123", "172.31.0.24"))
|
||||
.thenReturn(Optional.of("floci-vpc-1"));
|
||||
doThrow(new RuntimeException("start failed")).when(lifecycleManager).startCreated(eq("container-123"), any());
|
||||
doThrow(new IllegalStateException("removal failed"))
|
||||
@@ -1619,52 +1615,41 @@ class ContainerLauncherTest {
|
||||
|
||||
assertThrows(RuntimeException.class, () -> launcher.launch(fn));
|
||||
|
||||
verify(ec2Service, never()).releaseVpcPrivateIp(any(), any(), any());
|
||||
verify(ec2Service, never()).releaseVpcPrivateIp(any(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void vpcFunctionReturnsTheAddressAtOnceWhenItCannotJoinTheNetwork() throws Exception {
|
||||
LambdaFunction fn = vpcFunction("vpc-attach-failure-fn");
|
||||
when(ec2Service.allocateVpcPrivateIp("us-east-1", "subnet-a")).thenReturn(Optional.of("172.31.0.22"));
|
||||
when(ec2Service.attachContainerToVpc("us-east-1", "vpc-1", "subnet-a", "container-123", "172.31.0.22"))
|
||||
when(ec2Service.allocateVpcPrivateIp("000000000000", "us-east-1", "subnet-a")).thenReturn(Optional.of("172.31.0.22"));
|
||||
when(ec2Service.attachContainerToVpc("000000000000", "us-east-1", "vpc-1", "subnet-a", "container-123", "172.31.0.22"))
|
||||
.thenReturn(Optional.empty());
|
||||
|
||||
ContainerHandle handle = launcher.launch(fn);
|
||||
launcher.stop(handle);
|
||||
|
||||
InOrder order = inOrder(ec2Service, lifecycleManager);
|
||||
order.verify(ec2Service).releaseVpcPrivateIp("us-east-1", "subnet-a", "172.31.0.22");
|
||||
order.verify(ec2Service).releaseVpcPrivateIp("000000000000", "us-east-1", "subnet-a", "172.31.0.22");
|
||||
order.verify(lifecycleManager).startCreated(eq("container-123"), any());
|
||||
verify(ec2Service, times(1)).releaseVpcPrivateIp(any(), any(), any());
|
||||
verify(ec2Service, times(1)).releaseVpcPrivateIp(any(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void vpcFunctionLeasesItsAddressInTheFunctionsAccountOutsideARequest() throws Exception {
|
||||
// A provisioned or cross-account launch has no caller scope of the function's owner, and
|
||||
// the allocator reads the owner's endpoint interfaces from the ambient account.
|
||||
// A provisioned or cross-account launch has no caller scope of the function's owner, so the
|
||||
// owner's account is named on every call: its VPC network is the account's own.
|
||||
LambdaFunction fn = vpcFunction("vpc-account-fn");
|
||||
fn.setFunctionArn("arn:aws:lambda:eu-west-1:222233334444:function:vpc-account-fn");
|
||||
AtomicBoolean inOwnerScope = new AtomicBoolean();
|
||||
when(ec2Service.allocateVpcPrivateIp("eu-west-1", "subnet-a"))
|
||||
.thenAnswer(invocation -> inOwnerScope.get() ? Optional.of("172.31.0.25") : Optional.empty());
|
||||
when(ec2Service.attachContainerToVpc("eu-west-1", "vpc-1", "subnet-a", "container-123", "172.31.0.25"))
|
||||
.thenReturn(Optional.of("floci-vpc-1"));
|
||||
try (MockedStatic<RequestScopes> scopes =
|
||||
mockStatic(RequestScopes.class, withSettings().mockMaker(MockMakers.INLINE))) {
|
||||
scopes.when(() -> RequestScopes.callAs(eq("222233334444"), eq("eu-west-1"), any(Supplier.class)))
|
||||
.thenAnswer(invocation -> {
|
||||
inOwnerScope.set(true);
|
||||
try {
|
||||
return invocation.<Supplier<?>>getArgument(2).get();
|
||||
} finally {
|
||||
inOwnerScope.set(false);
|
||||
}
|
||||
});
|
||||
when(ec2Service.allocateVpcPrivateIp("222233334444", "eu-west-1", "subnet-a"))
|
||||
.thenReturn(Optional.of("172.31.0.25"));
|
||||
when(ec2Service.attachContainerToVpc("222233334444", "eu-west-1", "vpc-1", "subnet-a", "container-123",
|
||||
"172.31.0.25")).thenReturn(Optional.of("floci-vpc-1"));
|
||||
|
||||
launcher.launch(fn);
|
||||
}
|
||||
launcher.stop(launcher.launch(fn));
|
||||
|
||||
verify(ec2Service).attachContainerToVpc("eu-west-1", "vpc-1", "subnet-a", "container-123", "172.31.0.25");
|
||||
verify(ec2Service).attachContainerToVpc("222233334444", "eu-west-1", "vpc-1", "subnet-a", "container-123",
|
||||
"172.31.0.25");
|
||||
verify(ec2Service).releaseVpcPrivateIp("222233334444", "eu-west-1", "subnet-a", "172.31.0.25");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -154,13 +154,13 @@ class MwaaEnvironmentManagerTest {
|
||||
void anEnvironmentInAVpcJoinsItsNetworkBeforeAirflowStartsAndGivesTheAddressBackOnDelete() {
|
||||
airflowContainerStarts();
|
||||
subnetInVpc("subnet-a", "vpc-1");
|
||||
when(ec2Service.allocateVpcPrivateIp("us-east-1", "subnet-a")).thenReturn(Optional.of("172.31.0.20"));
|
||||
when(ec2Service.attachContainerToVpc("us-east-1", "vpc-1", "subnet-a", "airflow-container-id", "172.31.0.20"))
|
||||
when(ec2Service.allocateVpcPrivateIp("000000000000", "us-east-1", "subnet-a")).thenReturn(Optional.of("172.31.0.20"));
|
||||
when(ec2Service.attachContainerToVpc("000000000000", "us-east-1", "vpc-1", "subnet-a", "airflow-container-id", "172.31.0.20"))
|
||||
.thenReturn(Optional.of("floci-vpc-1"));
|
||||
Environment environment = environmentInSubnets("subnet-a", "subnet-b");
|
||||
|
||||
manager.startAirflowContainer(environment, "2.10.5", "172.18.0.9", "db-secret-pw", null, null);
|
||||
verify(ec2Service, never()).releaseVpcPrivateIp(any(), any(), any());
|
||||
verify(ec2Service, never()).releaseVpcPrivateIp(any(), any(), any(), any());
|
||||
assertTrue(manager.holdsSubnet("000000000000", "us-east-1", "subnet-a"));
|
||||
assertTrue(manager.holdsVpc("000000000000", "us-east-1", "vpc-1"));
|
||||
assertFalse(manager.holdsSubnet("111111111111", "us-east-1", "subnet-a"));
|
||||
@@ -170,25 +170,25 @@ class MwaaEnvironmentManagerTest {
|
||||
|
||||
InOrder order = Mockito.inOrder(lifecycleManager, ec2Service);
|
||||
order.verify(lifecycleManager).create(any());
|
||||
order.verify(ec2Service).attachContainerToVpc("us-east-1", "vpc-1", "subnet-a", "airflow-container-id",
|
||||
order.verify(ec2Service).attachContainerToVpc("000000000000", "us-east-1", "vpc-1", "subnet-a", "airflow-container-id",
|
||||
"172.31.0.20");
|
||||
order.verify(lifecycleManager).startCreated(eq("airflow-container-id"), any());
|
||||
order.verify(lifecycleManager).stopAndRemove("airflow-container-id", null);
|
||||
order.verify(ec2Service).releaseVpcPrivateIp("us-east-1", "subnet-a", "172.31.0.20");
|
||||
order.verify(ec2Service).releaseVpcPrivateIp("000000000000", "us-east-1", "subnet-a", "172.31.0.20");
|
||||
}
|
||||
|
||||
@Test
|
||||
void anAddressTheVpcNetworkRefusesIsGivenBackAndAirflowStillStarts() {
|
||||
airflowContainerStarts();
|
||||
subnetInVpc("subnet-a", "vpc-1");
|
||||
when(ec2Service.allocateVpcPrivateIp("us-east-1", "subnet-a")).thenReturn(Optional.of("172.31.0.21"));
|
||||
when(ec2Service.attachContainerToVpc("us-east-1", "vpc-1", "subnet-a", "airflow-container-id", "172.31.0.21"))
|
||||
when(ec2Service.allocateVpcPrivateIp("000000000000", "us-east-1", "subnet-a")).thenReturn(Optional.of("172.31.0.21"));
|
||||
when(ec2Service.attachContainerToVpc("000000000000", "us-east-1", "vpc-1", "subnet-a", "airflow-container-id", "172.31.0.21"))
|
||||
.thenReturn(Optional.empty());
|
||||
|
||||
manager.startAirflowContainer(environmentInSubnets("subnet-a"), "2.10.5", "172.18.0.9", "db-secret-pw",
|
||||
null, null);
|
||||
|
||||
verify(ec2Service).releaseVpcPrivateIp("us-east-1", "subnet-a", "172.31.0.21");
|
||||
verify(ec2Service).releaseVpcPrivateIp("000000000000", "us-east-1", "subnet-a", "172.31.0.21");
|
||||
verify(lifecycleManager).startCreated(eq("airflow-container-id"), any());
|
||||
}
|
||||
|
||||
@@ -196,8 +196,8 @@ class MwaaEnvironmentManagerTest {
|
||||
void aFailedStartGivesTheVpcAddressBackAfterRemovingTheContainer() {
|
||||
airflowContainerStarts();
|
||||
subnetInVpc("subnet-a", "vpc-1");
|
||||
when(ec2Service.allocateVpcPrivateIp("us-east-1", "subnet-a")).thenReturn(Optional.of("172.31.0.22"));
|
||||
when(ec2Service.attachContainerToVpc("us-east-1", "vpc-1", "subnet-a", "airflow-container-id", "172.31.0.22"))
|
||||
when(ec2Service.allocateVpcPrivateIp("000000000000", "us-east-1", "subnet-a")).thenReturn(Optional.of("172.31.0.22"));
|
||||
when(ec2Service.attachContainerToVpc("000000000000", "us-east-1", "vpc-1", "subnet-a", "airflow-container-id", "172.31.0.22"))
|
||||
.thenReturn(Optional.of("floci-vpc-1"));
|
||||
when(lifecycleManager.startCreated(eq("airflow-container-id"), any()))
|
||||
.thenThrow(new RuntimeException("start failed"));
|
||||
@@ -208,15 +208,15 @@ class MwaaEnvironmentManagerTest {
|
||||
|
||||
InOrder order = Mockito.inOrder(lifecycleManager, ec2Service);
|
||||
order.verify(lifecycleManager).removeIfExists("airflow-container-id");
|
||||
order.verify(ec2Service).releaseVpcPrivateIp("us-east-1", "subnet-a", "172.31.0.22");
|
||||
order.verify(ec2Service).releaseVpcPrivateIp("000000000000", "us-east-1", "subnet-a", "172.31.0.22");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aContainerDockerFailedToRemoveKeepsItsVpcAddress() {
|
||||
airflowContainerStarts();
|
||||
subnetInVpc("subnet-a", "vpc-1");
|
||||
when(ec2Service.allocateVpcPrivateIp("us-east-1", "subnet-a")).thenReturn(Optional.of("172.31.0.24"));
|
||||
when(ec2Service.attachContainerToVpc("us-east-1", "vpc-1", "subnet-a", "airflow-container-id", "172.31.0.24"))
|
||||
when(ec2Service.allocateVpcPrivateIp("000000000000", "us-east-1", "subnet-a")).thenReturn(Optional.of("172.31.0.24"));
|
||||
when(ec2Service.attachContainerToVpc("000000000000", "us-east-1", "vpc-1", "subnet-a", "airflow-container-id", "172.31.0.24"))
|
||||
.thenReturn(Optional.of("floci-vpc-1"));
|
||||
Environment environment = environmentInSubnets("subnet-a");
|
||||
manager.startAirflowContainer(environment, "2.10.5", "172.18.0.9", "db-secret-pw", null, null);
|
||||
@@ -224,7 +224,7 @@ class MwaaEnvironmentManagerTest {
|
||||
|
||||
manager.stopEnvironment(environment);
|
||||
|
||||
verify(ec2Service, never()).releaseVpcPrivateIp(any(), any(), any());
|
||||
verify(ec2Service, never()).releaseVpcPrivateIp(any(), any(), any(), any());
|
||||
assertTrue(manager.holdsSubnet("000000000000", "us-east-1", "subnet-a"));
|
||||
}
|
||||
|
||||
@@ -246,12 +246,12 @@ class MwaaEnvironmentManagerTest {
|
||||
when(settings.getNetworks()).thenReturn(Map.of(
|
||||
"floci-mwaa", new ContainerNetwork().withIpv4Address("172.18.0.5"),
|
||||
"floci-vpc-1", new ContainerNetwork().withIpv4Address("172.31.0.30")));
|
||||
when(ec2Service.reserveVpcPrivateIp("us-east-1", "subnet-a", "172.31.0.30")).thenReturn(true);
|
||||
when(ec2Service.reserveVpcPrivateIp("000000000000", "us-east-1", "subnet-a", "172.31.0.30")).thenReturn(true);
|
||||
|
||||
manager.reclaimVpcAddress(environment);
|
||||
|
||||
verify(ec2Service).reserveVpcPrivateIp("us-east-1", "subnet-a", "172.31.0.30");
|
||||
verify(ec2Service, never()).reserveVpcPrivateIp("us-east-1", "subnet-a", "172.18.0.5");
|
||||
verify(ec2Service).reserveVpcPrivateIp("000000000000", "us-east-1", "subnet-a", "172.31.0.30");
|
||||
verify(ec2Service, never()).reserveVpcPrivateIp("000000000000", "us-east-1", "subnet-a", "172.18.0.5");
|
||||
assertTrue(manager.holdsSubnet("000000000000", "us-east-1", "subnet-a"));
|
||||
}
|
||||
|
||||
@@ -266,8 +266,8 @@ class MwaaEnvironmentManagerTest {
|
||||
withoutNetwork.setName("no-vpc-env");
|
||||
manager.startAirflowContainer(withoutNetwork, "2.10.5", "172.18.0.9", "db-secret-pw", null, null);
|
||||
|
||||
verify(ec2Service, never()).allocateVpcPrivateIp(any(), any());
|
||||
verify(ec2Service, never()).attachContainerToVpc(any(), any(), any(), any(), any());
|
||||
verify(ec2Service, never()).allocateVpcPrivateIp(any(), any(), any());
|
||||
verify(ec2Service, never()).attachContainerToVpc(any(), any(), any(), any(), any(), any());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
||||
Reference in new issue
Block a user