mirror of
https://github.com/floci-io/floci.git
synced 2026-11-26 23:29:28 -05:00
feat(ecs): support stop-first replacement on stable published ports (#7539)
With an explicit zero minimum, drain stale REPLICA tasks whose fixed published ports conflict with a replacement. Wait for background Docker cleanup to confirm removal before placing the replacement, including port-owning security-group helpers. Keep task protection and failed-deployment guards, remove helpers outside the firewall monitor, and revoke task credentials even when cleanup fails. Closes #7523 Centralize fixed host-port selection in EcsContainerManager and use it from both container publishers and scheduler conflict detection. Preserve existing bridge, host, and awsvpc publication behavior. Validation: 179 focused ECS and firewall tests passed.
This commit is contained in:
1 parent
a389299b67
commit
4b09c3cf2b
5 files changed
+637
-23
No files matched your search
+11
@@ -191,6 +191,17 @@ public class SecurityGroupFirewallManager {
|
||||
}
|
||||
}
|
||||
|
||||
/** Removes the port-owning helper without forgetting its identity if Docker fails. */
|
||||
public void removeNamespaceStrict(String eniId) {
|
||||
ProtectedEndpoint endpoint;
|
||||
synchronized (this) {
|
||||
endpoint = endpoints.get(eniId);
|
||||
}
|
||||
if (endpoint != null) {
|
||||
lifecycleManager.removeIfExistsStrict(endpoint.helperId());
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void unregister(String eniId) {
|
||||
ProtectedEndpoint removed = endpoints.remove(eniId);
|
||||
if (removed != null) {
|
||||
|
||||
@@ -137,6 +137,8 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
private final CloudWatchMetricsService cloudWatchMetricsService;
|
||||
private final Clock clock;
|
||||
private final boolean dockerMode;
|
||||
private final boolean publishAwsvpcPortsToHost;
|
||||
private final boolean dynamicAwsvpcHostPorts;
|
||||
private final boolean sweepLeftoverContainers;
|
||||
private final String baseUrl;
|
||||
// Replaced by afterReset() after a state reset, whose container teardown shuts this scheduler down.
|
||||
@@ -287,6 +289,8 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
this.regionResolver = regionResolver;
|
||||
this.containerManager = containerManager;
|
||||
this.dockerMode = !config.services().ecs().mock();
|
||||
this.publishAwsvpcPortsToHost = config.services().ecs().publishAwsvpcPortsToHost();
|
||||
this.dynamicAwsvpcHostPorts = config.services().ecs().dynamicAwsvpcHostPorts();
|
||||
this.sweepLeftoverContainers = dockerMode && config.services().ecs().reconcileContainersOnStartup();
|
||||
this.leftoverTaskVolumesRemoved = !sweepLeftoverContainers;
|
||||
this.baseUrl = config.effectiveBaseUrl();
|
||||
@@ -5706,6 +5710,34 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
if (currentDeploymentId.equals(svc.getFailedDeploymentId())) {
|
||||
return;
|
||||
}
|
||||
Map<String, Object> deploymentConfiguration = svc.getDeploymentConfiguration();
|
||||
if (dockerMode && deploymentConfiguration != null
|
||||
&& deploymentConfiguration.get("minimumHealthyPercent") instanceof Number minimum
|
||||
&& minimum.doubleValue() == 0) {
|
||||
Set<Integer> replacementPorts = fixedPublishedPorts(
|
||||
resolveTaskDefinitionOrThrow(currentTaskDefinitionArn, region));
|
||||
List<EcsTask> draining = tasks.values().stream()
|
||||
.filter(task -> ownedBy(task, svc, cluster))
|
||||
.filter(task -> isStaleForDeployment(task, currentDeploymentId, currentTaskDefinitionArn))
|
||||
.filter(task -> !TaskStatus.STOPPED.name().equals(task.getLastStatus()))
|
||||
.filter(task -> !Collections.disjoint(replacementPorts,
|
||||
fixedPublishedPorts(resolveTaskDefinitionOrThrow(task.getTaskDefinitionArn(), region))))
|
||||
.toList();
|
||||
if (!draining.isEmpty()) {
|
||||
for (EcsTask task : draining) {
|
||||
try {
|
||||
stopServiceTaskIfUnprotected(task,
|
||||
"Service deployment replaced task definition " + task.getTaskDefinitionArn(), region);
|
||||
} catch (Exception e) {
|
||||
LOG.warnv(e, "Service reconciler failed to drain task {0}: {1}",
|
||||
task.getTaskArn(), e.getMessage());
|
||||
}
|
||||
}
|
||||
svc.setRunningCount((int) runningTasks.stream()
|
||||
.filter(task -> TaskStatus.RUNNING.name().equals(task.getLastStatus())).count());
|
||||
return;
|
||||
}
|
||||
}
|
||||
deployment = deploymentRecordForCurrentWorkload(svc);
|
||||
int toStart = svc.getDesiredCount() - (int) current;
|
||||
List<EcsTask> currentTasks = runningTasks.stream()
|
||||
@@ -5756,6 +5788,17 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
}
|
||||
}
|
||||
|
||||
private Set<Integer> fixedPublishedPorts(TaskDefinition definition) {
|
||||
return definition.getContainerDefinitions().stream()
|
||||
.filter(container -> container.getPortMappings() != null)
|
||||
.flatMap(container -> container.getPortMappings().stream())
|
||||
// ContainerLifecycleManager currently publishes every mapping as TCP.
|
||||
.map(port -> EcsContainerManager.publishedHostPort(port, definition.getNetworkMode(),
|
||||
publishAwsvpcPortsToHost, dynamicAwsvpcHostPorts))
|
||||
.filter(hostPort -> hostPort > 0)
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
private ServiceDeployment deploymentRecordForCurrentWorkload(EcsServiceModel svc) {
|
||||
String currentTaskSetId = taskSetId(svc);
|
||||
ServiceDeployment rollingBack = latestServiceDeployment(svc.getServiceArn(), deployment ->
|
||||
|
||||
+29
-21
@@ -479,12 +479,12 @@ public class EcsContainerManager {
|
||||
boolean publishToHost = !containerDetector.isRunningInContainer()
|
||||
|| publishAwsvpcPorts;
|
||||
for (PortMapping pm : def.getPortMappings()) {
|
||||
int hostPort = publishedHostPort(pm, taskDef.getNetworkMode(), publishAwsvpcPorts,
|
||||
config.services().ecs().dynamicAwsvpcHostPorts());
|
||||
if (awsvpc && publishToHost) {
|
||||
int hostPort = awsvpcHostPort(pm, publishAwsvpcPorts,
|
||||
config.services().ecs().dynamicAwsvpcHostPorts());
|
||||
specBuilder.withPortBinding(pm.containerPort(), hostPort, hostPortBindAddress());
|
||||
} else if (!awsvpc && pm.hostPort() > 0) {
|
||||
specBuilder.withPortBinding(pm.containerPort(), pm.hostPort());
|
||||
} else if (hostPort > 0) {
|
||||
specBuilder.withPortBinding(pm.containerPort(), hostPort);
|
||||
} else if (publishToHost) {
|
||||
specBuilder.withDynamicPort(pm.containerPort());
|
||||
} else {
|
||||
@@ -1413,18 +1413,23 @@ public class EcsContainerManager {
|
||||
continue;
|
||||
}
|
||||
for (PortMapping port : container.getPortMappings()) {
|
||||
bindings.put(port.containerPort(), awsvpcHostPort(port, publishAwsvpcPortsToHost,
|
||||
dynamicAwsvpcHostPorts));
|
||||
bindings.put(port.containerPort(), publishedHostPort(port, NetworkMode.awsvpc,
|
||||
publishAwsvpcPortsToHost, dynamicAwsvpcHostPorts));
|
||||
}
|
||||
}
|
||||
return bindings;
|
||||
}
|
||||
|
||||
private static int awsvpcHostPort(PortMapping port, boolean publishToHost, boolean dynamicHostPorts) {
|
||||
if (publishToHost && !dynamicHostPorts) {
|
||||
return port.hostPort() > 0 ? port.hostPort() : port.containerPort();
|
||||
/** Host port to publish for a mapping; zero means no fixed host-port reservation. */
|
||||
public static int publishedHostPort(PortMapping port, NetworkMode networkMode,
|
||||
boolean publishAwsvpcPortsToHost, boolean dynamicAwsvpcHostPorts) {
|
||||
if (networkMode != NetworkMode.awsvpc) {
|
||||
return Math.max(0, port.hostPort());
|
||||
}
|
||||
return 0;
|
||||
if (!publishAwsvpcPortsToHost || dynamicAwsvpcHostPorts) {
|
||||
return 0;
|
||||
}
|
||||
return port.hostPort() > 0 ? port.hostPort() : port.containerPort();
|
||||
}
|
||||
|
||||
private record PreparedNetwork(NetworkInterface eni, SecurityGroupFirewallManager.Namespace namespace) {}
|
||||
@@ -1546,18 +1551,21 @@ public class EcsContainerManager {
|
||||
// A force removal terminates Docker's follow-log transport even when the preceding stop failed.
|
||||
// Preserve handles for any container that still may be running after both operations failed.
|
||||
terminatedContainerIds.forEach(dockerId -> finalizeLogStream(handle, dockerId));
|
||||
if (handle.allContainersRemoved()) {
|
||||
cleanupProtectedNetwork(handle);
|
||||
removeTaskVolumes(handle);
|
||||
// Only now: a container that survived a failed removal still holds its address.
|
||||
releaseTaskLinkLocalAddresses(handle.getTaskArn());
|
||||
try {
|
||||
if (handle.allContainersRemoved()) {
|
||||
// Confirm the helper released its host ports before unregister forgets its identity.
|
||||
if (firewallManager != null && handle.getNetworkInterfaceId() != null) {
|
||||
firewallManager.removeNamespaceStrict(handle.getNetworkInterfaceId());
|
||||
}
|
||||
cleanupProtectedNetwork(handle);
|
||||
removeTaskVolumes(handle);
|
||||
// Only now: a container that survived a failed removal still holds its address.
|
||||
releaseTaskLinkLocalAddresses(handle.getTaskArn());
|
||||
}
|
||||
} finally {
|
||||
// A failed container or helper removal must not keep the stopping task's credentials usable.
|
||||
revokeTaskRoleCredentials(handle.getTaskArn());
|
||||
}
|
||||
// Revoked whether or not removal succeeded, unlike everything above. A task being
|
||||
// stopped must not keep usable credentials just because Docker could not remove one of
|
||||
// its containers: the retry that would eventually revoke them depends on the handle
|
||||
// being retained, which only happens while a log stream is still open, so a container
|
||||
// without one (a FireLens router, say) would strand them for the life of the process.
|
||||
revokeTaskRoleCredentials(handle.getTaskArn());
|
||||
return exitCodes;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user