fix(cloudformation): idempotent ECS resource deletion on stack delete (#1645)

Deleting a stack that tracked an AWS::ECS::TaskDefinition (or Cluster) failed
with DELETE_FAILED after a persistent restore when ECS no longer had the record.
Treat an already-missing task definition, cluster, or service as delete-complete,
matching how AWS tolerates absent resources during stack deletion.

Also hardens ECS persistence so records survive a native-image restore: register
the NetworkMode and LaunchType enums for reflection, and quarantine an unreadable
persisted file instead of silently starting empty.

Closes #1634
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-06-29 23:39:17 -05:00
1 parent 01c7e1e62d
commit 04e0f2dddb
5 files changed
+228 -4

No files matched your search

@@ -91,7 +91,25 @@ public class PersistentStorage<K, V> implements StorageBackend<K, V> {
store.putAll(data);
LOG.infov("Loaded {0} entries from {1}", store.size(), filePath);
} catch (IOException e) {
LOG.errorv(e, "Failed to load data from {0}", filePath);
// Starting empty here silently drops all persisted state for this store, which can leave
// other services (e.g. CloudFormation) referencing resources that now appear missing
// (see issue #1634). Quarantine the unreadable file and log loudly so the data loss is
// detectable rather than masquerading as an empty store.
quarantineUnreadableFile(e);
}
}
private void quarantineUnreadableFile(IOException cause) {
Path quarantine = filePath.resolveSibling(filePath.getFileName() + ".corrupt");
try {
Files.move(filePath, quarantine, StandardCopyOption.REPLACE_EXISTING);
LOG.errorv(cause, "Failed to load persisted data from {0}; moved the unreadable file to "
+ "{1} and started with an empty store. This store's state was lost.",
filePath, quarantine);
} catch (IOException moveError) {
LOG.errorv(cause, "Failed to load persisted data from {0}; could not quarantine it ({1}). "
+ "Starting with an empty store. This store's state was lost.",
filePath, moveError.getMessage());
}
}
@@ -425,8 +425,8 @@ public class CloudFormationResourceProvisioner {
case "AWS::Lambda::LayerVersion" -> deleteLambdaLayerVersion(physicalId, region);
case "AWS::Cognito::UserPool" -> cognitoService.deleteUserPool(physicalId);
case "AWS::Cognito::UserPoolClient" -> cognitoService.deleteUserPoolClient(physicalId);
case "AWS::ECS::Cluster" -> ecsService.deleteCluster(physicalId, region);
case "AWS::ECS::TaskDefinition" -> ecsService.deregisterTaskDefinition(physicalId, region);
case "AWS::ECS::Cluster" -> deleteEcsClusterSafe(physicalId, region);
case "AWS::ECS::TaskDefinition" -> deleteEcsTaskDefinitionSafe(physicalId, region);
case "AWS::ECS::Service" -> deleteEcsServiceSafe(physicalId, region);
case "AWS::ElasticLoadBalancingV2::LoadBalancer" -> elbV2Service.deleteLoadBalancer(region, physicalId);
case "AWS::ElasticLoadBalancingV2::TargetGroup" -> elbV2Service.deleteTargetGroup(region, physicalId);
@@ -3233,7 +3233,47 @@ public class CloudFormationResourceProvisioner {
} catch (IllegalArgumentException e) {
// Not an ARN; treat the value as a bare service name.
}
ecsService.deleteService(clusterRef, serviceName, true, region);
try {
ecsService.deleteService(clusterRef, serviceName, true, region);
} catch (AwsException e) {
// Idempotent delete: only an already-gone service (e.g. after a persistent restore that
// dropped ECS state) is treated as delete-complete. Any other error must still fail the
// stack delete rather than being silently swallowed. See issue #1634.
if (!"ServiceNotFoundException".equals(e.getErrorCode())) {
throw e;
}
LOG.debugv("ECS service {0} already gone, treating delete as complete: {1}",
serviceArn, e.getMessage());
}
}
private void deleteEcsTaskDefinitionSafe(String physicalId, String region) {
try {
ecsService.deregisterTaskDefinition(physicalId, region);
} catch (AwsException e) {
// Idempotent delete: only an already-missing task definition (ClientException "Unable to
// describe task definition", e.g. after a persistent restore) is delete-complete. Other
// errors must still fail the stack delete. See #1634.
if (!"ClientException".equals(e.getErrorCode())) {
throw e;
}
LOG.debugv("ECS task definition {0} already gone, treating delete as complete: {1}",
physicalId, e.getMessage());
}
}
private void deleteEcsClusterSafe(String physicalId, String region) {
try {
ecsService.deleteCluster(physicalId, region);
} catch (AwsException e) {
// Idempotent delete: only an already-missing cluster is delete-complete. A genuine
// failure such as ClusterContainsTasksException must still fail the stack delete. See #1634.
if (!"ClusterNotFoundException".equals(e.getErrorCode())) {
throw e;
}
LOG.debugv("ECS cluster {0} already gone, treating delete as complete: {1}",
physicalId, e.getMessage());
}
}
private List<ContainerDefinition> parseContainerDefinitions(JsonNode node, CloudFormationTemplateEngine engine) {
@@ -1,5 +1,8 @@
package io.github.hectorvent.floci.services.ecs.model;
import io.quarkus.runtime.annotations.RegisterForReflection;
@RegisterForReflection
public enum LaunchType {
EC2, FARGATE, EXTERNAL
}
@@ -1,5 +1,8 @@
package io.github.hectorvent.floci.services.ecs.model;
import io.quarkus.runtime.annotations.RegisterForReflection;
@RegisterForReflection
public enum NetworkMode {
bridge, host, awsvpc, none
}