mirror of
https://github.com/floci-io/floci.git
synced 2026-11-26 23:29:28 -05:00
fix(ecs): give an awsvpc task the address it reports (#7645)
With Docker-backed VPC networks, one container of an awsvpc task joins its VPC's Docker network at the task ENI address, so the address DescribeTasks reports answers. A Lambda function with VpcConfig joins the same network at an address leased from its first subnet, skipping addresses an interface already holds, and returns it once its container is removed. Peer and Cloud Map addresses still resolve on the network a container was created on. Closes #7639
This commit is contained in:
1 parent
dba4a4fc52
commit
2df2a1e149
13 files changed
+827
-69
No files matched your search
@@ -341,6 +341,9 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
// region::subnetId → offset the next synthesised address is tried at. Only an ordering hint:
|
||||
// restart forgets it, and privateIpsInUse is what keeps addresses from being handed out twice.
|
||||
private final Map<String, Long> subnetIpCursors = new HashMap<>();
|
||||
// region::subnetId → addresses allocateVpcPrivateIp leased to containers no stored resource
|
||||
// stands behind; guarded by privateIpAllocationLock.
|
||||
private final Map<String, Set<String>> containerPrivateIps = new HashMap<>();
|
||||
// Held from a synthesised address being chosen until the resource holding it is persisted, so
|
||||
// a concurrent allocation, which reads persisted resources to see what is taken, cannot pick
|
||||
// the same address in between. Taken after imageRegistryLock where a caller holds both.
|
||||
@@ -411,6 +414,43 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Attaches a container to the VPC's Docker network at {@code address}; empty when it stays off it. */
|
||||
public Optional<String> attachContainerToVpc(String region, String vpcId, String subnetId,
|
||||
String containerId, String address) {
|
||||
if (vpcNetworkManager == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return vpcNetworkManager.attach(region, vpcId, subnetId, containerId, address);
|
||||
}
|
||||
|
||||
/**
|
||||
* Leases an address of a Docker-backed subnet that nothing else holds, as an instance's is
|
||||
* chosen; empty when the subnet is not Docker-backed or has none left.
|
||||
*/
|
||||
public Optional<String> allocateVpcPrivateIp(String region, String subnetId) {
|
||||
if (vpcNetworkManager == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
synchronized (privateIpAllocationLock) {
|
||||
Optional<String> allocated = vpcNetworkManager.allocatePrivateIp(region, subnetId)
|
||||
.flatMap(first -> leaseNotHeld(region, subnetId, first,
|
||||
privateIpsHeldOnVpcNetwork(region, subnetId)));
|
||||
allocated.ifPresent(address -> containerPrivateIps
|
||||
.computeIfAbsent(key(region, subnetId), ignored -> new HashSet<>()).add(address));
|
||||
return allocated;
|
||||
}
|
||||
}
|
||||
|
||||
public void releaseVpcPrivateIp(String region, String subnetId, String address) {
|
||||
synchronized (privateIpAllocationLock) {
|
||||
containerPrivateIps.computeIfPresent(key(region, subnetId), (ignored, addresses) -> {
|
||||
addresses.remove(address);
|
||||
return addresses.isEmpty() ? null : addresses;
|
||||
});
|
||||
releasePrivateIps(region, subnetId, List.of(address));
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isVpcNetworkEnabled() {
|
||||
return vpcNetworkManager != null && vpcNetworkManager.enabled();
|
||||
}
|
||||
@@ -4544,6 +4584,23 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
return assignPrivateIp(region, subnetId, List.of());
|
||||
}
|
||||
|
||||
/**
|
||||
* Keeps the lease on {@code first}, or on the next address the network offers, unless
|
||||
* {@code held} contains it. Callers must hold {@link #privateIpAllocationLock}.
|
||||
*/
|
||||
private Optional<String> leaseNotHeld(String region, String subnetId, String first, Set<String> held) {
|
||||
Optional<String> allocated = Optional.of(first);
|
||||
List<String> skipped = new ArrayList<>();
|
||||
while (allocated.isPresent() && held.contains(allocated.get())) {
|
||||
skipped.add(allocated.get());
|
||||
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, so the network must not count one for them.
|
||||
releasePrivateIps(region, subnetId, skipped);
|
||||
return allocated;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gives back the VPC network's lease on each address, so automatic allocation can offer it
|
||||
* again. Callers must hold {@link #privateIpAllocationLock}.
|
||||
@@ -4577,16 +4634,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
if (allocated.isPresent()) {
|
||||
held = privateIpsHeldOnVpcNetwork(region, subnetId);
|
||||
held.addAll(pending);
|
||||
allocated = leaseNotHeld(region, subnetId, allocated.get(), held);
|
||||
}
|
||||
List<String> skipped = new ArrayList<>();
|
||||
while (allocated.isPresent() && held.contains(allocated.get())) {
|
||||
skipped.add(allocated.get());
|
||||
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 returns none, so a lease kept here
|
||||
// would outlive the holder.
|
||||
releasePrivateIps(region, subnetId, skipped);
|
||||
if (allocated.isPresent()) {
|
||||
return allocated.get();
|
||||
}
|
||||
@@ -4670,10 +4719,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 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.
|
||||
* the launch: it belongs to a launch that failed and has not given the address back yet,
|
||||
* which it does once its container is detached, or it outlived a holder that never returned
|
||||
* it. 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.
|
||||
*/
|
||||
@@ -4687,9 +4736,8 @@ 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 {@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}.
|
||||
* addresses stored interfaces and NAT gateways hold, so neither needs a lease. Callers must
|
||||
* hold {@link #privateIpAllocationLock}.
|
||||
*
|
||||
* @return whether the address lies in a Docker-backed range, where a lease could be taken
|
||||
*/
|
||||
@@ -4749,7 +4797,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
|
||||
/**
|
||||
* Every private address a live instance, network interface, NAT gateway or EC2 Instance Connect
|
||||
* Endpoint holds in the subnet.
|
||||
* Endpoint holds in the subnet, and those {@link #allocateVpcPrivateIp} leased to containers.
|
||||
*/
|
||||
private Set<String> privateIpsInUse(String region, String subnetId) {
|
||||
return new HashSet<>(privateIpsBySubnet(region).getOrDefault(subnetId, Set.of()));
|
||||
@@ -4768,10 +4816,21 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
|
||||
private Map<String, Set<String>> privateIpsBySubnet(String region) {
|
||||
String regionPrefix = region + "::";
|
||||
return privateIpsBySubnet(networkInterfaces.scan(k -> k.startsWith(regionPrefix)),
|
||||
Map<String, Set<String>> bySubnet = privateIpsBySubnet(networkInterfaces.scan(k -> k.startsWith(regionPrefix)),
|
||||
instances.scan(k -> k.startsWith(regionPrefix)),
|
||||
natGateways.scan(k -> k.startsWith(regionPrefix)),
|
||||
instanceConnectEndpoints.scan(k -> k.startsWith(regionPrefix)));
|
||||
// Read here, the one occupancy every allocator and endpoint derivation shares, so a derived
|
||||
// endpoint interface cannot land on a container's address.
|
||||
synchronized (privateIpAllocationLock) {
|
||||
containerPrivateIps.forEach((subnetKey, addresses) -> {
|
||||
if (subnetKey.startsWith(regionPrefix)) {
|
||||
bySubnet.computeIfAbsent(subnetKey.substring(regionPrefix.length()), ignored -> new HashSet<>())
|
||||
.addAll(addresses);
|
||||
}
|
||||
});
|
||||
}
|
||||
return bySubnet;
|
||||
}
|
||||
|
||||
private static Map<String, Set<String>> privateIpsBySubnet(List<NetworkInterface> regionInterfaces,
|
||||
@@ -7372,8 +7431,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
|
||||
/**
|
||||
* {@link #privateIpsInUse} as numeric IPv4 values, for endpoint derivation. Only persisted
|
||||
* resources are read, never another endpoint's derived interfaces, so the derivation cannot
|
||||
* recurse into itself through the allocator.
|
||||
* resources and container leases are read, never another endpoint's derived interfaces, so the
|
||||
* derivation cannot recurse into itself through the allocator.
|
||||
*/
|
||||
private Set<Long> persistedAddressValuesInUse(String region, String subnetId) {
|
||||
return addressValues(privateIpsInUse(region, subnetId));
|
||||
@@ -12995,9 +13054,9 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
// 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.
|
||||
// can carry is either a stale lease, 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());
|
||||
@@ -15265,9 +15324,11 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a standalone ENI. AWS refuses while it is still attached, floci-kt9, and equally
|
||||
* while a service holds it: {@link #holdNetworkInterfaceForService} leaves no attachment
|
||||
* behind, so the status is what says the interface is spoken for.
|
||||
* Deletes a standalone ENI and gives the lease its primary address took back to the VPC
|
||||
* network. A requested primary took no lease, so any it carries is a failed launch's, given
|
||||
* back early as a deleted NAT gateway's is. AWS refuses while it is still attached,
|
||||
* floci-kt9, and equally while a service holds it: {@link #holdNetworkInterfaceForService}
|
||||
* leaves no attachment behind, so the status is what says the interface is spoken for.
|
||||
*/
|
||||
public void deleteNetworkInterface(String region, String networkInterfaceId) {
|
||||
synchronized (privateIpAllocationLock) {
|
||||
@@ -15278,6 +15339,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
}
|
||||
releaseInterfaceAddresses(region, networkInterfaceId, true);
|
||||
networkInterfaces.delete(key(region, networkInterfaceId));
|
||||
releasePrivateIps(region, ni.getSubnetId(), List.of(ni.getPrivateIpAddress()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -249,8 +249,9 @@ public class EcsServiceDiscoveryRegistrar {
|
||||
/**
|
||||
* Builds the instance attributes AWS records for an ECS-registered instance. On AWS the
|
||||
* address of an awsvpc task is its ENI address, because that is where the task answers. In
|
||||
* Floci the ENI address belongs to no network the task's containers joined, so an awsvpc task
|
||||
* registers the address its container actually holds, and falls back to the ENI address only
|
||||
* Floci the ENI address is held at most on the VPC's Docker network, which peers outside that
|
||||
* VPC cannot reach, so an awsvpc task registers the address its container holds on
|
||||
* the network it was created on, and falls back to the ENI address only
|
||||
* when no running container can say what it holds. A bridge-mode task has no ENI and is
|
||||
* registered at the container's host address, as before.
|
||||
*
|
||||
|
||||
@@ -623,6 +623,9 @@ public class EcsContainerManager {
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
if (task.getNetworkInterfaceId() != null && protectedNetwork == null) {
|
||||
joinTaskVpcNetwork(task, taskDef, containerIds, region);
|
||||
}
|
||||
|
||||
Map<String, Container> runtimeContainersByName = new LinkedHashMap<>();
|
||||
for (Container container : runtimeContainers) {
|
||||
@@ -1150,6 +1153,30 @@ public class EcsContainerManager {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Puts one container of an awsvpc task on its VPC's Docker network at the task ENI address, so
|
||||
* the address DescribeTasks reports answers. AWS gives the whole task that one address; here it
|
||||
* goes to the first container that maps a port, else to the first container. Without
|
||||
* Docker-backed VPC networks, or for an ENI address outside the subnet's planned range, the
|
||||
* containers keep only the network they were created on. Removing the container disconnects
|
||||
* it, before {@link #releaseTaskNetwork} returns the address.
|
||||
*/
|
||||
private void joinTaskVpcNetwork(EcsTask task, TaskDefinition taskDef, Map<String, String> containerIds,
|
||||
String region) {
|
||||
String vpcId = taskVpcId(task, region);
|
||||
if (vpcId == null) {
|
||||
return;
|
||||
}
|
||||
List<ContainerDefinition> definitions = taskDef.getContainerDefinitions();
|
||||
ContainerDefinition holder = definitions.stream()
|
||||
.filter(def -> def.getPortMappings() != null && !def.getPortMappings().isEmpty())
|
||||
.findFirst()
|
||||
.orElse(definitions.getFirst());
|
||||
ec2Service.attachContainerToVpc(region, vpcId,
|
||||
task.getNetworkConfiguration().getAwsvpcConfiguration().getSubnets().getFirst(),
|
||||
containerIds.get(holder.getName()), task.getPrivateIpAddress());
|
||||
}
|
||||
|
||||
/**
|
||||
* Restates an EC2 lookup failure as the error RunTask declares. A caller naming a subnet or a
|
||||
* security group that is not there asked for something the task cannot have, and
|
||||
@@ -2168,8 +2195,9 @@ public class EcsContainerManager {
|
||||
* protected namespace has no network of its own, so the namespace holder's address is the one
|
||||
* it answers on.
|
||||
*
|
||||
* <p>This is deliberately not the task ENI address. ECS containers never join the subnet's
|
||||
* VPC network, so the ENI address is control-plane state that no peer can connect to.
|
||||
* <p>This is deliberately not the task ENI address. One task container holds that address on
|
||||
* the VPC's Docker network, when VPC networks are enabled, but peers outside that VPC reach the
|
||||
* task on the network its container was created on, so that address is the one returned.
|
||||
*
|
||||
* @return the address, or empty when the container has not started or Docker cannot say
|
||||
*/
|
||||
@@ -2196,17 +2224,25 @@ public class EcsContainerManager {
|
||||
|
||||
// A container can be on multiple networks; getNetworks() is unordered.
|
||||
// Pick an IP that the Floci/ELBv2 process can actually route to:
|
||||
// 1. the explicitly-configured ECS Docker network, if set;
|
||||
// 2. otherwise any user-defined network (Floci joins one when in Docker)
|
||||
// in preference to the default bridge;
|
||||
// 3. otherwise the first non-blank IP.
|
||||
String configured = config.services().ecs().dockerNetwork().orElse(null);
|
||||
// 1. the network ECS containers join: the configured one, or the one Floci runs on;
|
||||
// 2. otherwise the network the container was created on, rather than the VPC
|
||||
// network an awsvpc task also joins at its ENI address;
|
||||
// 3. otherwise any user-defined network in preference to the default bridge;
|
||||
// 4. otherwise the first non-blank IP.
|
||||
String configured = containerBuilder.resolveDockerNetwork(config.services().ecs().dockerNetwork())
|
||||
.orElse(null);
|
||||
if (configured != null && !configured.isBlank()) {
|
||||
ContainerNetwork net = networks.get(configured);
|
||||
if (net != null && isUsableIp(net.getIpAddress())) {
|
||||
return Optional.of(net.getIpAddress());
|
||||
}
|
||||
}
|
||||
String ownNetwork = inspect.getHostConfig() == null ? null : inspect.getHostConfig().getNetworkMode();
|
||||
ContainerNetwork own = ownNetwork == null ? null
|
||||
: networks.get("default".equals(ownNetwork) ? "bridge" : ownNetwork);
|
||||
if (own != null && isUsableIp(own.getIpAddress())) {
|
||||
return Optional.of(own.getIpAddress());
|
||||
}
|
||||
for (Map.Entry<String, ContainerNetwork> entry : networks.entrySet()) {
|
||||
if (!isDefaultDockerNetwork(entry.getKey())
|
||||
&& isUsableIp(entry.getValue().getIpAddress())) {
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.github.dockerjava.api.exception.NotFoundException;
|
||||
import io.floci.aws.config.EmulatorConfig;
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.Digests;
|
||||
import io.floci.aws.core.common.RequestScopes;
|
||||
import io.floci.aws.core.common.docker.ContainerBuilder;
|
||||
import io.floci.aws.core.common.docker.ContainerLifecycleManager;
|
||||
import io.floci.aws.core.common.docker.ContainerLogStreamer;
|
||||
@@ -14,6 +15,7 @@ import io.floci.aws.core.common.docker.ContainerStorageHelper;
|
||||
import io.floci.aws.core.common.docker.DockerHostResolver;
|
||||
import io.floci.aws.core.common.docker.LaunchedContainerAwsEnv;
|
||||
import io.floci.aws.core.common.docker.RetryingTarCopier;
|
||||
import io.floci.aws.services.ec2.Ec2Service;
|
||||
import io.floci.aws.services.ecr.registry.EcrRegistryManager;
|
||||
import io.floci.aws.services.iam.model.SessionCreds;
|
||||
import io.floci.aws.services.lambda.LambdaLayerService;
|
||||
@@ -124,6 +126,9 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
|
||||
private final LambdaLayerService layerService;
|
||||
private final LaunchedContainerAwsEnv awsEnv;
|
||||
private final LambdaExecutionRoleCredentials executionRoleCredentials;
|
||||
private final Ec2Service ec2Service;
|
||||
/** The VPC address each VpcConfig function's container holds, returned once it is removed. */
|
||||
private final Map<String, VpcLease> vpcLeases = new ConcurrentHashMap<>();
|
||||
|
||||
@Inject
|
||||
public ContainerLauncher(ContainerBuilder containerBuilder,
|
||||
@@ -136,7 +141,8 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
|
||||
EcrRegistryManager ecrRegistryManager,
|
||||
LambdaLayerService layerService,
|
||||
LaunchedContainerAwsEnv awsEnv,
|
||||
LambdaExecutionRoleCredentials executionRoleCredentials) {
|
||||
LambdaExecutionRoleCredentials executionRoleCredentials,
|
||||
Ec2Service ec2Service) {
|
||||
this.containerBuilder = containerBuilder;
|
||||
this.lifecycleManager = lifecycleManager;
|
||||
this.logStreamer = logStreamer;
|
||||
@@ -148,6 +154,7 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
|
||||
this.layerService = layerService;
|
||||
this.awsEnv = awsEnv;
|
||||
this.executionRoleCredentials = executionRoleCredentials;
|
||||
this.ec2Service = ec2Service;
|
||||
this.populateSemaphore = new Semaphore(resolvePopulateConcurrency(config));
|
||||
}
|
||||
|
||||
@@ -442,6 +449,7 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
|
||||
}
|
||||
|
||||
// Now start the container with code in place
|
||||
joinVpcNetwork(fn, containerId, lambdaAccountId, lambdaRegion);
|
||||
lifecycleManager.startCreated(containerId, spec);
|
||||
runtimeApiServer.markContainerStarted();
|
||||
watchForUnexpectedExit(dockerClient, containerId, runtimeApiServer);
|
||||
@@ -496,7 +504,8 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
|
||||
fn.getFunctionName(), e.getMessage());
|
||||
if (containerId != null) {
|
||||
try {
|
||||
lifecycleManager.stopAndRemove(containerId, null);
|
||||
lifecycleManager.stopAndRemoveStrict(containerId, null);
|
||||
releaseVpcLease(containerId);
|
||||
} catch (Exception cleanupError) {
|
||||
LOG.warnv(cleanupError, "Could not remove failed Lambda container {0}", containerId);
|
||||
}
|
||||
@@ -656,6 +665,9 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
|
||||
executionRoleCredentials.unregister(
|
||||
handle.getExecutionRoleSessionAccountId(), handle.getExecutionRoleAccessKeyId());
|
||||
}
|
||||
// Only once the container is gone: a failed removal is retried, and until then Docker
|
||||
// still holds the address.
|
||||
releaseVpcLease(handle.getContainerId());
|
||||
|
||||
// Only now close the runtime API socket. Any pollers that were still parked when
|
||||
// the container exited get terminated by their end of the connection closing;
|
||||
@@ -674,6 +686,38 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Puts a VpcConfig function's container on its VPC's Docker network at an address leased from
|
||||
* its first subnet, before it starts, so it reaches what the VPC holds by private address.
|
||||
* Without Docker-backed VPC networks it stays on the network it was created on.
|
||||
*/
|
||||
private void joinVpcNetwork(LambdaFunction fn, String containerId, String accountId, String region) {
|
||||
Object subnetIds = fn.getVpcConfig() == null ? null : fn.getVpcConfig().get("SubnetIds");
|
||||
if (fn.getVpcId() == null || ec2Service == null || !(subnetIds instanceof List<?> subnets)
|
||||
|| subnets.isEmpty() || !(subnets.getFirst() instanceof String subnetId)) {
|
||||
return;
|
||||
}
|
||||
// Provisioned and cross-account launches run outside the owner's request scope.
|
||||
Optional<String> address = RequestScopes.callAs(accountId, region,
|
||||
() -> ec2Service.allocateVpcPrivateIp(region, subnetId));
|
||||
if (address.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
if (ec2Service.attachContainerToVpc(region, fn.getVpcId(), subnetId, containerId, address.get()).isPresent()) {
|
||||
vpcLeases.put(containerId, new VpcLease(region, subnetId, address.get()));
|
||||
} else {
|
||||
ec2Service.releaseVpcPrivateIp(region, subnetId, address.get());
|
||||
}
|
||||
}
|
||||
|
||||
/** Docker holds the address until the container is gone, so callers run this after removing it. */
|
||||
private void releaseVpcLease(String containerId) {
|
||||
VpcLease lease = vpcLeases.remove(containerId);
|
||||
if (lease != null) {
|
||||
ec2Service.releaseVpcPrivateIp(lease.region(), lease.subnetId(), lease.address());
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isAwsCredentialVariable(String name) {
|
||||
return "AWS_ACCESS_KEY_ID".equals(name)
|
||||
|| "AWS_SECRET_ACCESS_KEY".equals(name)
|
||||
@@ -1389,6 +1433,8 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
|
||||
/** Where a container's output is sent: the CloudWatch log group/stream and its region. */
|
||||
private record LogDestination(String accountId, String logGroup, String logStream, String region) { }
|
||||
|
||||
private record VpcLease(String region, String subnetId, String address) { }
|
||||
|
||||
/**
|
||||
* Arms an async watch (via Docker's own wait-for-exit API, not polling) that notices when
|
||||
* this container's main process dies while nothing inside the runtime reported it - a
|
||||
|
||||
Reference in new issue
Block a user