fix(ecs): retain inactive services for terraform draining and recreation with correct idempotency (#1166)

* fix(ecs): retain inactive services for terraform draining and recreation with correct idempotency

* fix(ecs): enforce strict aws compliance for service lifecycles and idempotency

- retain inactive services
- added status filters to listServices and listServicesByNamespace
- wrote a shared extractServiceName helper
This commit is contained in:
Samarth Tikotkar authored and GitHub committed 2026-06-06 08:58:21 -05:00
1 parent b9dd0c38a9
commit f359ae854d
2 files changed
+297 -5

No files matched your search

@@ -31,11 +31,13 @@ import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.time.Instant;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
@@ -492,8 +494,12 @@ public class EcsService {
String key = serviceKey(region, cluster.getClusterName(), serviceName);
if (services.containsKey(key)) {
throw new AwsException("InvalidParameterException",
"Creation of service was not idempotent.", 400);
EcsServiceModel existingSvc = services.get(key);
if ("ACTIVE".equals(existingSvc.getStatus())) {
throw new AwsException("InvalidParameterException",
"Creation of service was not idempotent.", 400);
}
}
EcsServiceModel svc = new EcsServiceModel();
@@ -523,11 +529,18 @@ public class EcsService {
Integer desiredCount, NetworkConfiguration networkConfiguration,
String region) {
EcsCluster cluster = resolveClusterOrDefault(clusterRef, region);
serviceName = extractServiceName(serviceName);
String key = serviceKey(region, cluster.getClusterName(), serviceName);
EcsServiceModel svc = services.get(key);
if (svc == null) {
throw new AwsException("ServiceNotFoundException", "Service " + serviceName + " not found.", 404);
}
if ("INACTIVE".equals(svc.getStatus())) {
throw new AwsException("ServiceNotActiveException",
"Service " + serviceName + " is not active.", 400);
}
if (desiredCount != null) {
svc.setDesiredCount(desiredCount);
}
@@ -544,11 +557,17 @@ public class EcsService {
public EcsServiceModel deleteService(String clusterRef, String serviceName, boolean force, String region) {
EcsCluster cluster = resolveClusterOrDefault(clusterRef, region);
serviceName = extractServiceName(serviceName);
String key = serviceKey(region, cluster.getClusterName(), serviceName);
EcsServiceModel svc = services.get(key);
if (svc == null) {
throw new AwsException("ServiceNotFoundException", "Service " + serviceName + " not found.", 404);
}
if ("INACTIVE".equals(svc.getStatus())) {
return svc;
}
if (!force && svc.getDesiredCount() > 0) {
throw new AwsException("InvalidParameterException",
"The service cannot be stopped. Update the service to 0 tasks or use the force flag.", 400);
@@ -571,7 +590,6 @@ public class EcsService {
t.getTaskArn(), e.getMessage());
}
});
services.remove(key);
return svc;
}
@@ -592,6 +610,7 @@ public class EcsService {
String prefix = serviceKeyPrefix(region, cluster.getClusterName());
return services.entrySet().stream()
.filter(e -> e.getKey().startsWith(prefix))
.filter(e -> !"INACTIVE".equals(e.getValue().getStatus()))
.map(e -> e.getValue().getServiceArn())
.toList();
}
@@ -599,6 +618,7 @@ public class EcsService {
public List<String> listServicesByNamespace(String namespace, String region) {
return services.values().stream()
.filter(s -> namespace.equals(s.getNamespace()))
.filter(s -> !"INACTIVE".equals(s.getStatus()))
.map(EcsServiceModel::getServiceArn)
.toList();
}
@@ -1352,6 +1372,13 @@ public class EcsService {
return key.substring(0, key.indexOf("::"));
}
private static String extractServiceName(String serviceName) {
if (serviceName != null && serviceName.contains("/")) {
return serviceName.substring(serviceName.lastIndexOf("/") + 1);
}
return serviceName;
}
private static String extractClusterNameFromServiceKey(String key) {
String after = key.substring(key.indexOf("::") + 2);
int slash = after.indexOf('/');