mirror of
https://github.com/floci-io/floci.git
synced 2026-11-28 08:09:43 -05:00
fix(ecs): register reachable task addresses in Cloud Map and bound synthesised ENI addresses (#4509)
* fix(ecs): register reachable task addresses in Cloud Map and bound synthesised ENI addresses ECS service discovery registered an awsvpc task's ENI address, but task containers never join the subnet's network, so peers resolved a name and then timed out. Register the address the task container holds on its Docker network instead, falling back to the ENI address only when there is no running container. The synthesised private-address fallback in EC2 counted past the subnet CIDR (10.70.1.549) and restarted at .10 on every boot. It now allocates inside the subnet CIDR, skips the AWS-reserved addresses and every address a persisted ENI, instance or NAT gateway still holds, and reports InsufficientFreeAddressesInSubnet when the subnet is full. * fix(ec2): hold the synthesised-address lock until the resource is persisted The allocator chose a free address under its lock but released it before the network interface, instance or NAT gateway holding the address was stored, and it decides what is free from stored resources. Two concurrent creates could both take the same address, most visibly at the end of a /28 where the cursor clamps back onto the last one handed out. A dedicated lock now covers allocation through persistence at each caller: CreateNetworkInterface, CreateNatGateway and the RunInstances loop, where it is taken inside imageRegistryLock. * fix(ecs): advertise the container port for an awsvpc task registered at its container address * docs(ecs): state which Cloud Map address and port each network mode registers * fix(ecs): look for stale Cloud Map instances at an awsvpc task's container address The registrar registers an awsvpc task at the address its container holds on its Docker network, but the eviction from #4513 resolved the task's addresses through resolveContainerHost, which is 127.0.0.1 when Floci runs natively, so a stale instance at the container address was never found. Load balancer target eviction is unchanged, since targets are still registered through resolveContainerHost. * refactor(ec2): nest the address allocation lock visibly inside runInstances Scope privateIpAllocationLock to the instance-building loop, the only part that allocates and persists addresses, and indent it under imageRegistryLock. * fix(ec2): allocate a launch's addresses before storing any of its instances A RunInstances larger than the subnet's free addresses failed with InsufficientFreeAddressesInSubnet only after the instances that fit had been stored with their root volumes. The launch now allocates every address first, counting the ones it has already picked as taken, and releases any Docker leases it took when the subnet runs out.
This commit is contained in:
1 parent
b2d668d9ae
commit
014156a546
10 files changed
+662
-192
No files matched your search
@@ -84,6 +84,7 @@ import io.github.hectorvent.floci.services.ec2.model.VpcIpv6CidrBlockAssociation
|
||||
import io.github.hectorvent.floci.services.ec2.model.VpcPeeringConnection;
|
||||
import io.github.hectorvent.floci.services.ec2.model.VpcPeeringConnectionStateReason;
|
||||
import io.github.hectorvent.floci.services.ec2.model.VpcPeeringConnectionVpcInfo;
|
||||
import io.github.hectorvent.floci.services.ec2.net.Cidr4;
|
||||
import io.github.hectorvent.floci.services.ec2.net.VpcNetworkManager;
|
||||
import io.github.hectorvent.floci.services.ec2.portforward.Ec2PortForwardManager;
|
||||
import io.github.hectorvent.floci.services.iam.IamService;
|
||||
@@ -101,8 +102,10 @@ import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
@@ -116,7 +119,6 @@ import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.BiPredicate;
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -137,6 +139,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
private static final DateTimeFormatter ISO_FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
|
||||
.withZone(ZoneOffset.UTC);
|
||||
private static final int DEFAULT_ROOT_VOLUME_SIZE_GIB = 8;
|
||||
private static final long SYNTHETIC_FIRST_OFFSET = 10;
|
||||
private static final String SYNTHETIC_FALLBACK_CIDR = "172.31.0.0/24";
|
||||
private static final String DEFAULT_ROOT_VOLUME_TYPE = "gp3";
|
||||
private static final Set<String> VALID_VOLUME_TYPES =
|
||||
Set.of("standard", "io1", "io2", "gp2", "sc1", "st1", "gp3");
|
||||
@@ -239,8 +243,13 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
private final StorageBackend<String, List<Tag>> tags;
|
||||
private final StorageBackend<String, CapacityReservation> capacityReservations;
|
||||
private final Set<String> seededAccountRegions = ConcurrentHashMap.newKeySet();
|
||||
// subnetId → counter for IP assignment (runtime-only, not persisted)
|
||||
private final Map<String, AtomicInteger> subnetIpCounters = new ConcurrentHashMap<>();
|
||||
// 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<>();
|
||||
// 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.
|
||||
private final Object privateIpAllocationLock = new Object();
|
||||
|
||||
/**
|
||||
* Null in the hermetic unit tests, which reach the constructors that do not take it; CDI always
|
||||
@@ -2968,111 +2977,114 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
if (dryRun) {
|
||||
throw new AwsException("DryRunOperation", "Request would have succeeded, but DryRun flag is set.", 412);
|
||||
}
|
||||
for (int i = 0; i < count; i++) {
|
||||
String instanceId = "i-" + randomHex(17);
|
||||
String privateIp = suppliedEni != null
|
||||
? suppliedEni.getPrivateIpAddress()
|
||||
: assignPrivateIp(region, finalSubnetId);
|
||||
synchronized (privateIpAllocationLock) {
|
||||
List<String> privateIps = allocateLaunchAddresses(region, finalSubnetId, suppliedEni, count);
|
||||
for (int i = 0; i < count; i++) {
|
||||
String instanceId = "i-" + randomHex(17);
|
||||
String privateIp = privateIps.get(i);
|
||||
|
||||
Instance inst = new Instance();
|
||||
inst.setInstanceId(instanceId);
|
||||
inst.setImageId(imageId);
|
||||
inst.setState(InstanceState.pending());
|
||||
inst.setInstanceType(effectiveInstanceType);
|
||||
inst.setPlacement(new Placement(az));
|
||||
inst.setSubnetId(finalSubnetId);
|
||||
inst.setVpcId(vpcId);
|
||||
// AWS precedence (#1984): the launch-time AssociatePublicIpAddress
|
||||
// override wins in both directions; the subnet's MapPublicIpOnLaunch
|
||||
// attribute is only the default when the launch does not specify it.
|
||||
inst.setAssociatePublicIp(associatePublicIp != null
|
||||
? associatePublicIp
|
||||
: subnet != null && subnet.isMapPublicIpOnLaunch());
|
||||
inst.setPrivateIpAddress(privateIp);
|
||||
inst.setPrivateDnsName("ip-" + privateIp.replace('.', '-') + ".ec2.internal");
|
||||
inst.setKeyName(keyName);
|
||||
inst.setSecurityGroups(new ArrayList<>(sgIdentifiers));
|
||||
inst.setArchitecture(architecture);
|
||||
inst.setLaunchTime(Instant.now());
|
||||
inst.setAmiLaunchIndex(i);
|
||||
inst.setClientToken(clientToken);
|
||||
inst.setRegion(region);
|
||||
inst.setUserData(userData);
|
||||
inst.setEncodedUserData(encodedUserData);
|
||||
inst.setIamInstanceProfileArn(iamInstanceProfileArn);
|
||||
if (iamInstanceProfileArn != null) {
|
||||
inst.setIamInstanceProfileAssociationTime(inst.getLaunchTime());
|
||||
}
|
||||
inst.setMetadataOptions(LaunchTemplateData.MetadataOptions.merge(launchMetadataOptions, null));
|
||||
inst.setCreditSpecificationCpuCredits(
|
||||
acquiredCpuCredits(effectiveInstanceType, creditSpecificationCpuCredits));
|
||||
if (instanceTags != null && !instanceTags.isEmpty()) {
|
||||
inst.setTags(new ArrayList<>(instanceTags));
|
||||
tags.put(instanceId, new ArrayList<>(instanceTags));
|
||||
}
|
||||
Instance inst = new Instance();
|
||||
inst.setInstanceId(instanceId);
|
||||
inst.setImageId(imageId);
|
||||
inst.setState(InstanceState.pending());
|
||||
inst.setInstanceType(effectiveInstanceType);
|
||||
inst.setPlacement(new Placement(az));
|
||||
inst.setSubnetId(finalSubnetId);
|
||||
inst.setVpcId(vpcId);
|
||||
// AWS precedence (#1984): the launch-time AssociatePublicIpAddress
|
||||
// override wins in both directions; the subnet's MapPublicIpOnLaunch
|
||||
// attribute is only the default when the launch does not specify it.
|
||||
inst.setAssociatePublicIp(associatePublicIp != null
|
||||
? associatePublicIp
|
||||
: subnet != null && subnet.isMapPublicIpOnLaunch());
|
||||
inst.setPrivateIpAddress(privateIp);
|
||||
inst.setPrivateDnsName("ip-" + privateIp.replace('.', '-') + ".ec2.internal");
|
||||
inst.setKeyName(keyName);
|
||||
inst.setSecurityGroups(new ArrayList<>(sgIdentifiers));
|
||||
inst.setArchitecture(architecture);
|
||||
inst.setLaunchTime(Instant.now());
|
||||
inst.setAmiLaunchIndex(i);
|
||||
inst.setClientToken(clientToken);
|
||||
inst.setRegion(region);
|
||||
inst.setUserData(userData);
|
||||
inst.setEncodedUserData(encodedUserData);
|
||||
inst.setIamInstanceProfileArn(iamInstanceProfileArn);
|
||||
if (iamInstanceProfileArn != null) {
|
||||
inst.setIamInstanceProfileAssociationTime(inst.getLaunchTime());
|
||||
}
|
||||
inst.setMetadataOptions(LaunchTemplateData.MetadataOptions.merge(launchMetadataOptions, null));
|
||||
inst.setCreditSpecificationCpuCredits(
|
||||
acquiredCpuCredits(effectiveInstanceType, creditSpecificationCpuCredits));
|
||||
if (instanceTags != null && !instanceTags.isEmpty()) {
|
||||
inst.setTags(new ArrayList<>(instanceTags));
|
||||
tags.put(instanceId, new ArrayList<>(instanceTags));
|
||||
}
|
||||
|
||||
// Network interface, either the caller-supplied standalone ENI (override-default-eni,
|
||||
// floci-kt9) or a freshly-minted implicit primary interface.
|
||||
InstanceNetworkInterface eni = new InstanceNetworkInterface();
|
||||
eni.setNetworkInterfaceId(suppliedEni != null ? suppliedEni.getNetworkInterfaceId() : "eni-" + randomHex(17));
|
||||
eni.setSubnetId(finalSubnetId);
|
||||
eni.setVpcId(vpcId);
|
||||
eni.setOwnerId(callerAccountId());
|
||||
eni.setDescription(suppliedEni != null ? suppliedEni.getDescription() : null);
|
||||
eni.setMacAddress(suppliedEni != null ? suppliedEni.getMacAddress() : null);
|
||||
eni.setPrivateIpAddress(privateIp);
|
||||
eni.setPrivateDnsName(inst.getPrivateDnsName());
|
||||
eni.setGroups(new ArrayList<>(sgIdentifiers));
|
||||
eni.setAttachmentId("eni-attach-" + randomHex(17));
|
||||
eni.setDeviceIndex(suppliedEni != null ? networkInterfaceDeviceIndex : 0);
|
||||
if (inst.getLaunchTime() != null) {
|
||||
eni.setAttachTime(ISO_FMT.format(inst.getLaunchTime()));
|
||||
}
|
||||
inst.getNetworkInterfaces().add(eni);
|
||||
if (suppliedEni != null) {
|
||||
// The standalone record stays authoritative rather than being folded into the
|
||||
// instance: AWS defaults deleteOnTermination to false for an interface the caller
|
||||
// created and handed to a launch, so it outlives the instance and returns to
|
||||
// "available" on termination instead of vanishing with it. Double-counting is
|
||||
// avoided in describeNetworkInterfaces, which skips the instance-side copy of any
|
||||
// id the standalone store owns.
|
||||
NetworkInterfaceAttachment launchAttachment = new NetworkInterfaceAttachment();
|
||||
launchAttachment.setAttachmentId(eni.getAttachmentId());
|
||||
launchAttachment.setDeviceIndex(eni.getDeviceIndex());
|
||||
launchAttachment.setStatus("attached");
|
||||
launchAttachment.setInstanceId(instanceId);
|
||||
launchAttachment.setInstanceOwnerId(callerAccountId());
|
||||
launchAttachment.setAttachTime(eni.getAttachTime());
|
||||
launchAttachment.setDeleteOnTermination(false);
|
||||
suppliedEni.setAttachment(launchAttachment);
|
||||
suppliedEni.setStatus("in-use");
|
||||
networkInterfaces.put(key(region, suppliedEni.getNetworkInterfaceId()), suppliedEni);
|
||||
}
|
||||
// Network interface, either the caller-supplied standalone ENI (override-default-eni,
|
||||
// floci-kt9) or a freshly-minted implicit primary interface.
|
||||
InstanceNetworkInterface eni = new InstanceNetworkInterface();
|
||||
eni.setNetworkInterfaceId(suppliedEni != null
|
||||
? suppliedEni.getNetworkInterfaceId()
|
||||
: "eni-" + randomHex(17));
|
||||
eni.setSubnetId(finalSubnetId);
|
||||
eni.setVpcId(vpcId);
|
||||
eni.setOwnerId(callerAccountId());
|
||||
eni.setDescription(suppliedEni != null ? suppliedEni.getDescription() : null);
|
||||
eni.setMacAddress(suppliedEni != null ? suppliedEni.getMacAddress() : null);
|
||||
eni.setPrivateIpAddress(privateIp);
|
||||
eni.setPrivateDnsName(inst.getPrivateDnsName());
|
||||
eni.setGroups(new ArrayList<>(sgIdentifiers));
|
||||
eni.setAttachmentId("eni-attach-" + randomHex(17));
|
||||
eni.setDeviceIndex(suppliedEni != null ? networkInterfaceDeviceIndex : 0);
|
||||
if (inst.getLaunchTime() != null) {
|
||||
eni.setAttachTime(ISO_FMT.format(inst.getLaunchTime()));
|
||||
}
|
||||
inst.getNetworkInterfaces().add(eni);
|
||||
if (suppliedEni != null) {
|
||||
// The standalone record stays authoritative rather than being folded into the
|
||||
// instance: AWS defaults deleteOnTermination to false for an interface the caller
|
||||
// created and handed to a launch, so it outlives the instance and returns to
|
||||
// "available" on termination instead of vanishing with it. Double-counting is
|
||||
// avoided in describeNetworkInterfaces, which skips the instance-side copy of any
|
||||
// id the standalone store owns.
|
||||
NetworkInterfaceAttachment launchAttachment = new NetworkInterfaceAttachment();
|
||||
launchAttachment.setAttachmentId(eni.getAttachmentId());
|
||||
launchAttachment.setDeviceIndex(eni.getDeviceIndex());
|
||||
launchAttachment.setStatus("attached");
|
||||
launchAttachment.setInstanceId(instanceId);
|
||||
launchAttachment.setInstanceOwnerId(callerAccountId());
|
||||
launchAttachment.setAttachTime(eni.getAttachTime());
|
||||
launchAttachment.setDeleteOnTermination(false);
|
||||
suppliedEni.setAttachment(launchAttachment);
|
||||
suppliedEni.setStatus("in-use");
|
||||
networkInterfaces.put(key(region, suppliedEni.getNetworkInterfaceId()), suppliedEni);
|
||||
}
|
||||
|
||||
// Root EBS volume
|
||||
String rootVolId = "vol-" + randomHex(17);
|
||||
inst.setRootVolumeId(rootVolId);
|
||||
Volume rootVol = new Volume();
|
||||
rootVol.setVolumeId(rootVolId);
|
||||
rootVol.setAvailabilityZone(az);
|
||||
rootVol.setVolumeType(DEFAULT_ROOT_VOLUME_TYPE);
|
||||
rootVol.setSize(DEFAULT_ROOT_VOLUME_SIZE_GIB);
|
||||
rootVol.setState("in-use");
|
||||
rootVol.setRegion(region);
|
||||
rootVol.setCreateTime(Instant.now());
|
||||
VolumeAttachment att = new VolumeAttachment();
|
||||
att.setVolumeId(rootVolId);
|
||||
att.setInstanceId(instanceId);
|
||||
att.setDevice(inst.getRootDeviceName());
|
||||
att.setState("attached");
|
||||
att.setDeleteOnTermination(true);
|
||||
att.setAttachTime(Instant.now());
|
||||
rootVol.getAttachments().add(att);
|
||||
volumes.put(key(region, rootVolId), rootVol);
|
||||
// Root EBS volume
|
||||
String rootVolId = "vol-" + randomHex(17);
|
||||
inst.setRootVolumeId(rootVolId);
|
||||
Volume rootVol = new Volume();
|
||||
rootVol.setVolumeId(rootVolId);
|
||||
rootVol.setAvailabilityZone(az);
|
||||
rootVol.setVolumeType(DEFAULT_ROOT_VOLUME_TYPE);
|
||||
rootVol.setSize(DEFAULT_ROOT_VOLUME_SIZE_GIB);
|
||||
rootVol.setState("in-use");
|
||||
rootVol.setRegion(region);
|
||||
rootVol.setCreateTime(Instant.now());
|
||||
VolumeAttachment att = new VolumeAttachment();
|
||||
att.setVolumeId(rootVolId);
|
||||
att.setInstanceId(instanceId);
|
||||
att.setDevice(inst.getRootDeviceName());
|
||||
att.setState("attached");
|
||||
att.setDeleteOnTermination(true);
|
||||
att.setAttachTime(Instant.now());
|
||||
rootVol.getAttachments().add(att);
|
||||
volumes.put(key(region, rootVolId), rootVol);
|
||||
|
||||
instances.put(key(region, instanceId), inst);
|
||||
launched.add(inst);
|
||||
reservation.getInstances().add(inst);
|
||||
instances.put(key(region, instanceId), inst);
|
||||
launched.add(inst);
|
||||
reservation.getInstances().add(inst);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3302,7 +3314,44 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
"The availability zone '" + availabilityZone + "' has no subnet in this region.", 400));
|
||||
}
|
||||
|
||||
/**
|
||||
* Every private address a launch needs, allocated before any of its instances is stored, so
|
||||
* a subnet without room for the whole launch fails it without leaving the instances that did
|
||||
* fit behind. Callers must hold {@link #privateIpAllocationLock}.
|
||||
*/
|
||||
private List<String> allocateLaunchAddresses(String region, String subnetId, NetworkInterface suppliedEni,
|
||||
int count) {
|
||||
List<String> addresses = new ArrayList<>(count);
|
||||
if (suppliedEni != null) {
|
||||
for (int i = 0; i < count; i++) {
|
||||
addresses.add(suppliedEni.getPrivateIpAddress());
|
||||
}
|
||||
return addresses;
|
||||
}
|
||||
try {
|
||||
for (int i = 0; i < count; i++) {
|
||||
addresses.add(assignPrivateIp(region, subnetId, addresses));
|
||||
}
|
||||
} catch (AwsException e) {
|
||||
if (vpcNetworkManager != null) {
|
||||
for (String address : addresses) {
|
||||
vpcNetworkManager.releasePrivateIp(region, subnetId, address);
|
||||
}
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
return addresses;
|
||||
}
|
||||
|
||||
private String assignPrivateIp(String region, String subnetId) {
|
||||
return assignPrivateIp(region, subnetId, List.of());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param pending addresses already handed out to resources that are not stored yet, which the
|
||||
* synthesised fallback must not hand out again
|
||||
*/
|
||||
private String assignPrivateIp(String region, String subnetId, Collection<String> pending) {
|
||||
// A Docker-backed subnet allocates the real thing: an address on the network the
|
||||
// instance's container will actually hold. Only when there is no such network does
|
||||
// this fall back to the synthesised address below, which nothing can connect to.
|
||||
@@ -3315,17 +3364,88 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
if (subnetId == null) {
|
||||
return "172.31.0." + (10 + new Random().nextInt(200));
|
||||
}
|
||||
AtomicInteger counter = subnetIpCounters.computeIfAbsent(region + "::" + subnetId, k -> new AtomicInteger(10));
|
||||
int offset = counter.getAndIncrement();
|
||||
Subnet subnet = subnets.get(key(region, subnetId)).orElse(null);
|
||||
if (subnet == null) {
|
||||
return "172.31.0." + offset;
|
||||
Cidr4 cidr = subnets.get(key(region, subnetId))
|
||||
.flatMap(subnet -> Cidr4.parse(subnet.getCidrBlock()))
|
||||
.or(() -> Cidr4.parse(SYNTHETIC_FALLBACK_CIDR))
|
||||
.orElseThrow();
|
||||
// AWS reserves the first four addresses of a subnet and its last one.
|
||||
long first = 4;
|
||||
long last = cidr.size() - 2;
|
||||
if (last < first) {
|
||||
throw insufficientFreeAddresses(subnetId);
|
||||
}
|
||||
String cursorKey = key(region, subnetId);
|
||||
synchronized (privateIpAllocationLock) {
|
||||
// The addresses persisted resources already hold, so a restart, which forgets the
|
||||
// cursor, never hands out an address that is still in use.
|
||||
Set<String> inUse = privateIpsInUse(region, subnetId);
|
||||
inUse.addAll(pending);
|
||||
long start = Math.clamp(subnetIpCursors.getOrDefault(cursorKey, SYNTHETIC_FIRST_OFFSET), first, last);
|
||||
long span = last - first + 1;
|
||||
for (long step = 0; step < span; step++) {
|
||||
long offset = first + (start - first + step) % span;
|
||||
String address = cidr.addressAt(offset).orElseThrow();
|
||||
if (!inUse.contains(address)) {
|
||||
subnetIpCursors.put(cursorKey, offset + 1);
|
||||
return address;
|
||||
}
|
||||
}
|
||||
}
|
||||
throw insufficientFreeAddresses(subnetId);
|
||||
}
|
||||
|
||||
private static AwsException insufficientFreeAddresses(String subnetId) {
|
||||
return new AwsException("InsufficientFreeAddressesInSubnet",
|
||||
"There are not enough free addresses in subnet '" + subnetId + "' to satisfy the requested number of instances.",
|
||||
400);
|
||||
}
|
||||
|
||||
/** Every private address a live instance, network interface or NAT gateway holds in the subnet. */
|
||||
private Set<String> privateIpsInUse(String region, String subnetId) {
|
||||
String regionPrefix = region + "::";
|
||||
Set<String> inUse = new HashSet<>();
|
||||
for (NetworkInterface ni : networkInterfaces.scan(k -> k.startsWith(regionPrefix))) {
|
||||
if (!subnetId.equals(ni.getSubnetId())) {
|
||||
continue;
|
||||
}
|
||||
addIfSet(inUse, ni.getPrivateIpAddress());
|
||||
for (NetworkInterfacePrivateIpAddress ip : nullToEmpty(ni.getPrivateIpAddresses())) {
|
||||
addIfSet(inUse, ip.getPrivateIpAddress());
|
||||
}
|
||||
}
|
||||
for (Instance instance : instances.scan(k -> k.startsWith(regionPrefix))) {
|
||||
String state = instance.getState() == null ? null : instance.getState().getName();
|
||||
if ("terminated".equals(state)) {
|
||||
continue;
|
||||
}
|
||||
if (subnetId.equals(instance.getSubnetId())) {
|
||||
addIfSet(inUse, instance.getPrivateIpAddress());
|
||||
}
|
||||
for (InstanceNetworkInterface ni : nullToEmpty(instance.getNetworkInterfaces())) {
|
||||
if (subnetId.equals(ni.getSubnetId())) {
|
||||
addIfSet(inUse, ni.getPrivateIpAddress());
|
||||
}
|
||||
}
|
||||
}
|
||||
for (NatGateway natGateway : natGateways.scan(k -> k.startsWith(regionPrefix))) {
|
||||
if (!subnetId.equals(natGateway.getSubnetId()) || "deleted".equals(natGateway.getState())) {
|
||||
continue;
|
||||
}
|
||||
for (NatGatewayAddress address : nullToEmpty(natGateway.getNatGatewayAddresses())) {
|
||||
addIfSet(inUse, address.getPrivateIp());
|
||||
}
|
||||
}
|
||||
return inUse;
|
||||
}
|
||||
|
||||
private static <T> List<T> nullToEmpty(List<T> list) {
|
||||
return list == null ? List.of() : list;
|
||||
}
|
||||
|
||||
private static void addIfSet(Set<String> addresses, String address) {
|
||||
if (address != null && !address.isBlank()) {
|
||||
addresses.add(address);
|
||||
}
|
||||
// Parse base IP from CIDR
|
||||
String cidr = subnet.getCidrBlock();
|
||||
String baseIp = cidr.split("/")[0];
|
||||
String[] parts = baseIp.split("\\.");
|
||||
return parts[0] + "." + parts[1] + "." + parts[2] + "." + offset;
|
||||
}
|
||||
|
||||
public List<Reservation> describeInstances(String region, List<String> instanceIds, Map<String, List<String>> filters) {
|
||||
@@ -7973,12 +8093,14 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
natGateway.setConnectivityType(connectivityType != null && !connectivityType.isBlank() ? connectivityType : "public");
|
||||
natGateway.setCreateTime(Instant.now());
|
||||
natGateway.setRegion(region);
|
||||
natGateway.getNatGatewayAddresses().add(natGatewayAddress(region, subnetId, allocationId));
|
||||
if (natGatewayTags != null && !natGatewayTags.isEmpty()) {
|
||||
natGateway.setTags(new ArrayList<>(natGatewayTags));
|
||||
tags.put(natGateway.getNatGatewayId(), new ArrayList<>(natGatewayTags));
|
||||
synchronized (privateIpAllocationLock) {
|
||||
natGateway.getNatGatewayAddresses().add(natGatewayAddress(region, subnetId, allocationId));
|
||||
if (natGatewayTags != null && !natGatewayTags.isEmpty()) {
|
||||
natGateway.setTags(new ArrayList<>(natGatewayTags));
|
||||
tags.put(natGateway.getNatGatewayId(), new ArrayList<>(natGatewayTags));
|
||||
}
|
||||
natGateways.put(key(region, natGateway.getNatGatewayId()), natGateway);
|
||||
}
|
||||
natGateways.put(key(region, natGateway.getNatGatewayId()), natGateway);
|
||||
return natGateway;
|
||||
}
|
||||
|
||||
@@ -9199,49 +9321,51 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
String eniId = "eni-" + randomHex(17);
|
||||
String primaryIp = (privateIpAddress != null && !privateIpAddress.isBlank())
|
||||
? privateIpAddress : assignPrivateIp(region, subnetId);
|
||||
String primaryDns = "ip-" + primaryIp.replace('.', '-') + ".ec2.internal";
|
||||
synchronized (privateIpAllocationLock) {
|
||||
String eniId = "eni-" + randomHex(17);
|
||||
String primaryIp = (privateIpAddress != null && !privateIpAddress.isBlank())
|
||||
? privateIpAddress : assignPrivateIp(region, subnetId);
|
||||
String primaryDns = "ip-" + primaryIp.replace('.', '-') + ".ec2.internal";
|
||||
|
||||
NetworkInterface ni = new NetworkInterface();
|
||||
ni.setNetworkInterfaceId(eniId);
|
||||
ni.setSubnetId(subnetId);
|
||||
ni.setVpcId(subnet.getVpcId());
|
||||
ni.setAvailabilityZone(subnet.getAvailabilityZone());
|
||||
ni.setDescription(description);
|
||||
ni.setOwnerId(callerAccountId());
|
||||
ni.setStatus("available");
|
||||
ni.setMacAddress(randomMac());
|
||||
ni.setPrivateIpAddress(primaryIp);
|
||||
ni.setPrivateDnsName(primaryDns);
|
||||
ni.setGroups(sgIdentifiers);
|
||||
if (tagList != null) {
|
||||
ni.getTagSet().addAll(tagList);
|
||||
}
|
||||
|
||||
List<NetworkInterfacePrivateIpAddress> ipList = new ArrayList<>();
|
||||
NetworkInterfacePrivateIpAddress primary = new NetworkInterfacePrivateIpAddress();
|
||||
primary.setPrivateIpAddress(primaryIp);
|
||||
primary.setPrivateDnsName(primaryDns);
|
||||
primary.setPrimary(true);
|
||||
ipList.add(primary);
|
||||
if (privateIpAddresses != null) {
|
||||
for (String extra : privateIpAddresses) {
|
||||
if (extra == null || extra.isBlank() || extra.equals(primaryIp)) {
|
||||
continue;
|
||||
}
|
||||
NetworkInterfacePrivateIpAddress secondary = new NetworkInterfacePrivateIpAddress();
|
||||
secondary.setPrivateIpAddress(extra);
|
||||
secondary.setPrivateDnsName("ip-" + extra.replace('.', '-') + ".ec2.internal");
|
||||
secondary.setPrimary(false);
|
||||
ipList.add(secondary);
|
||||
NetworkInterface ni = new NetworkInterface();
|
||||
ni.setNetworkInterfaceId(eniId);
|
||||
ni.setSubnetId(subnetId);
|
||||
ni.setVpcId(subnet.getVpcId());
|
||||
ni.setAvailabilityZone(subnet.getAvailabilityZone());
|
||||
ni.setDescription(description);
|
||||
ni.setOwnerId(callerAccountId());
|
||||
ni.setStatus("available");
|
||||
ni.setMacAddress(randomMac());
|
||||
ni.setPrivateIpAddress(primaryIp);
|
||||
ni.setPrivateDnsName(primaryDns);
|
||||
ni.setGroups(sgIdentifiers);
|
||||
if (tagList != null) {
|
||||
ni.getTagSet().addAll(tagList);
|
||||
}
|
||||
}
|
||||
ni.setPrivateIpAddresses(ipList);
|
||||
|
||||
networkInterfaces.put(key(region, eniId), ni);
|
||||
return ni;
|
||||
List<NetworkInterfacePrivateIpAddress> ipList = new ArrayList<>();
|
||||
NetworkInterfacePrivateIpAddress primary = new NetworkInterfacePrivateIpAddress();
|
||||
primary.setPrivateIpAddress(primaryIp);
|
||||
primary.setPrivateDnsName(primaryDns);
|
||||
primary.setPrimary(true);
|
||||
ipList.add(primary);
|
||||
if (privateIpAddresses != null) {
|
||||
for (String extra : privateIpAddresses) {
|
||||
if (extra == null || extra.isBlank() || extra.equals(primaryIp)) {
|
||||
continue;
|
||||
}
|
||||
NetworkInterfacePrivateIpAddress secondary = new NetworkInterfacePrivateIpAddress();
|
||||
secondary.setPrivateIpAddress(extra);
|
||||
secondary.setPrivateDnsName("ip-" + extra.replace('.', '-') + ".ec2.internal");
|
||||
secondary.setPrimary(false);
|
||||
ipList.add(secondary);
|
||||
}
|
||||
}
|
||||
ni.setPrivateIpAddresses(ipList);
|
||||
|
||||
networkInterfaces.put(key(region, eniId), ni);
|
||||
return ni;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1892,12 +1892,15 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
/**
|
||||
* The addresses at which a Cloud Map instance resolves to a running task: its ENI address,
|
||||
* within the task's VPC only, and each container's address on Docker, on any port since a
|
||||
* DNS answer carries none. A loopback address counts only on the task's host ports.
|
||||
* DNS answer carries none. A loopback address counts only on the task's host ports. An
|
||||
* awsvpc task's containers count at the address each holds on its Docker network, which is
|
||||
* where {@link EcsServiceDiscoveryRegistrar} registers them, even when Floci runs natively.
|
||||
*/
|
||||
private Set<EcsTaskAddress> instanceAddresses(EcsTask task, String region) {
|
||||
Set<EcsTaskAddress> addresses = new LinkedHashSet<>();
|
||||
String eniAddress = task.getPrivateIpAddress();
|
||||
if (eniAddress != null && !eniAddress.isBlank() && !EcsTaskAddress.isLoopback(eniAddress)) {
|
||||
boolean awsvpc = eniAddress != null && !eniAddress.isBlank();
|
||||
if (awsvpc && !EcsTaskAddress.isLoopback(eniAddress)) {
|
||||
String vpcId = containerManager.taskVpcId(task, region);
|
||||
if (vpcId != null) {
|
||||
addresses.add(new EcsTaskAddress(eniAddress, null, vpcId));
|
||||
@@ -1907,7 +1910,9 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
return addresses;
|
||||
}
|
||||
for (Container container : task.getContainers()) {
|
||||
String host = containerManager.resolveContainerHost(container);
|
||||
String host = awsvpc
|
||||
? containerManager.resolvePeerAddress(container).orElse(null)
|
||||
: containerManager.resolveContainerHost(container);
|
||||
if (host == null || host.isBlank()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
+19
-12
@@ -246,9 +246,12 @@ public class EcsServiceDiscoveryRegistrar {
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the instance attributes AWS records for an ECS-registered instance. The address is
|
||||
* the task's own ENI address in awsvpc mode, and otherwise the container's address on the
|
||||
* Docker network, which is the one another container on that network can reach.
|
||||
* 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
|
||||
* 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.
|
||||
*
|
||||
* <p>The metadata attributes alongside it are the ones the ECS service discovery
|
||||
* documentation lists, so a caller can filter a {@code DiscoverInstances} response by them
|
||||
@@ -259,7 +262,10 @@ public class EcsServiceDiscoveryRegistrar {
|
||||
Map<String, Object> registry, String region) {
|
||||
Container container = containerFor(task, string(registry, "containerName"));
|
||||
String address = task.getPrivateIpAddress();
|
||||
if ((address == null || address.isBlank()) && container != null) {
|
||||
boolean awsvpc = address != null && !address.isBlank();
|
||||
if (awsvpc) {
|
||||
address = containerManager.resolvePeerAddress(container).orElse(address);
|
||||
} else if (container != null) {
|
||||
address = containerManager.resolveContainerHost(container);
|
||||
}
|
||||
if (address == null || address.isBlank()) {
|
||||
@@ -268,7 +274,7 @@ public class EcsServiceDiscoveryRegistrar {
|
||||
|
||||
Map<String, String> attributes = new LinkedHashMap<>();
|
||||
attributes.put("AWS_INSTANCE_IPV4", address);
|
||||
Integer port = instancePort(container, registry);
|
||||
Integer port = instancePort(container, registry, awsvpc);
|
||||
if (port != null) {
|
||||
attributes.put("AWS_INSTANCE_PORT", String.valueOf(port));
|
||||
}
|
||||
@@ -316,19 +322,20 @@ public class EcsServiceDiscoveryRegistrar {
|
||||
}
|
||||
|
||||
/**
|
||||
* The port an SRV record would carry. An explicit {@code port} wins, as it does on AWS;
|
||||
* otherwise the host port the selected container's {@code containerPort} was published on,
|
||||
* so a bridge-mode task advertises the port that is reachable rather than the one inside the
|
||||
* container. A {@code containerPort} with no matching binding falls back to itself, which is
|
||||
* the awsvpc case where the two are always equal.
|
||||
* The port an SRV record would carry. An explicit {@code port} wins, as it does on AWS.
|
||||
* An awsvpc task is registered at its container's own address, where it listens on the
|
||||
* {@code containerPort} itself, even when Floci also published that port on a dynamic host
|
||||
* port. A bridge-mode task is registered at the host, so it advertises the host port its
|
||||
* {@code containerPort} was published on, falling back to the {@code containerPort} when
|
||||
* there is no matching binding.
|
||||
*/
|
||||
private Integer instancePort(Container container, Map<String, Object> registry) {
|
||||
private Integer instancePort(Container container, Map<String, Object> registry, boolean awsvpc) {
|
||||
Integer port = integer(registry, "port");
|
||||
if (port != null) {
|
||||
return port;
|
||||
}
|
||||
Integer containerPort = integer(registry, "containerPort");
|
||||
if (container == null || containerPort == null || container.getNetworkBindings() == null) {
|
||||
if (awsvpc || container == null || containerPort == null || container.getNetworkBindings() == null) {
|
||||
return containerPort;
|
||||
}
|
||||
return container.getNetworkBindings().stream()
|
||||
|
||||
+37
-10
@@ -1916,13 +1916,40 @@ public class EcsContainerManager {
|
||||
if (!containerDetector.isRunningInContainer()) {
|
||||
return "127.0.0.1";
|
||||
}
|
||||
return dockerNetworkAddress(container).orElse("127.0.0.1");
|
||||
}
|
||||
|
||||
/**
|
||||
* The address another task container reaches this one at: its IP on the Docker network it
|
||||
* joined, whether Floci itself runs natively or in Docker. A task in a security-group
|
||||
* 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.
|
||||
*
|
||||
* @return the address, or empty when the container has not started or Docker cannot say
|
||||
*/
|
||||
public Optional<String> resolvePeerAddress(Container container) {
|
||||
if (container == null || container.getDockerId() == null || container.getDockerId().isBlank()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return dockerNetworkAddress(container);
|
||||
}
|
||||
|
||||
private Optional<String> dockerNetworkAddress(Container container) {
|
||||
String dockerId = container.getDockerId();
|
||||
if (dockerId == null || dockerId.isBlank()) {
|
||||
return "127.0.0.1";
|
||||
return Optional.empty();
|
||||
}
|
||||
try {
|
||||
var inspect = lifecycleManager.getDockerClient().inspectContainerCmd(dockerId).exec();
|
||||
var networks = inspect.getNetworkSettings().getNetworks();
|
||||
InspectContainerResponse inspect = lifecycleManager.getDockerClient().inspectContainerCmd(dockerId).exec();
|
||||
String networkMode = inspect.getHostConfig() == null ? null : inspect.getHostConfig().getNetworkMode();
|
||||
if (networkMode != null && networkMode.startsWith("container:")) {
|
||||
inspect = lifecycleManager.getDockerClient()
|
||||
.inspectContainerCmd(networkMode.substring("container:".length())).exec();
|
||||
}
|
||||
Map<String, ContainerNetwork> networks = inspect.getNetworkSettings().getNetworks();
|
||||
|
||||
// A container can be on multiple networks; getNetworks() is unordered.
|
||||
// Pick an IP that the Floci/ELBv2 process can actually route to:
|
||||
@@ -1932,26 +1959,26 @@ public class EcsContainerManager {
|
||||
// 3. otherwise the first non-blank IP.
|
||||
String configured = config.services().ecs().dockerNetwork().orElse(null);
|
||||
if (configured != null && !configured.isBlank()) {
|
||||
var net = networks.get(configured);
|
||||
ContainerNetwork net = networks.get(configured);
|
||||
if (net != null && isUsableIp(net.getIpAddress())) {
|
||||
return net.getIpAddress();
|
||||
return Optional.of(net.getIpAddress());
|
||||
}
|
||||
}
|
||||
for (var entry : networks.entrySet()) {
|
||||
for (Map.Entry<String, ContainerNetwork> entry : networks.entrySet()) {
|
||||
if (!isDefaultDockerNetwork(entry.getKey())
|
||||
&& isUsableIp(entry.getValue().getIpAddress())) {
|
||||
return entry.getValue().getIpAddress();
|
||||
return Optional.of(entry.getValue().getIpAddress());
|
||||
}
|
||||
}
|
||||
for (var net : networks.values()) {
|
||||
for (ContainerNetwork net : networks.values()) {
|
||||
if (isUsableIp(net.getIpAddress())) {
|
||||
return net.getIpAddress();
|
||||
return Optional.of(net.getIpAddress());
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("Could not resolve container IP for {0}: {1}", dockerId, e.getMessage());
|
||||
}
|
||||
return "127.0.0.1";
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
private static boolean isUsableIp(String ip) {
|
||||
|
||||
Reference in new issue
Block a user