mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
fix(cloudformation): retain deleted stacks briefly for destroy polling (#1029)
This commit is contained in:
1 parent
37f37d1292
commit
8be5d03bbc
6 files changed
+295
-6
No files matched your search
@@ -0,0 +1,16 @@
|
||||
package io.github.hectorvent.floci.config;
|
||||
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.enterprise.inject.Produces;
|
||||
|
||||
import java.time.Clock;
|
||||
|
||||
@ApplicationScoped
|
||||
public class ClockProducer {
|
||||
|
||||
@Produces
|
||||
@ApplicationScoped
|
||||
public Clock clock() {
|
||||
return Clock.systemUTC();
|
||||
}
|
||||
}
|
||||
@@ -577,6 +577,9 @@ public interface EmulatorConfig {
|
||||
interface CloudFormationServiceConfig {
|
||||
@WithDefault("true")
|
||||
boolean enabled();
|
||||
|
||||
@WithDefault("30")
|
||||
long deletedStackRetentionSeconds();
|
||||
}
|
||||
|
||||
interface AcmServiceConfig {
|
||||
|
||||
+53
-6
@@ -17,6 +17,7 @@ import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.net.URI;
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
@@ -35,6 +36,7 @@ public class CloudFormationService {
|
||||
private static final Logger LOG = Logger.getLogger(CloudFormationService.class);
|
||||
|
||||
private final ConcurrentHashMap<String, Stack> stacks = new ConcurrentHashMap<>();
|
||||
private final ConcurrentHashMap<String, DeletedStackEntry> deletedStacks = new ConcurrentHashMap<>();
|
||||
// Global exports registry: region:exportName -> exportValue
|
||||
private final ConcurrentHashMap<String, String> exports = new ConcurrentHashMap<>();
|
||||
private final ExecutorService executor = Executors.newCachedThreadPool();
|
||||
@@ -44,23 +46,25 @@ public class CloudFormationService {
|
||||
private final ObjectMapper objectMapper;
|
||||
private final EmulatorConfig config;
|
||||
private final RegionResolver regionResolver;
|
||||
private final Clock clock;
|
||||
|
||||
@Inject
|
||||
public CloudFormationService(CloudFormationResourceProvisioner provisioner, S3Service s3Service,
|
||||
ObjectMapper objectMapper, EmulatorConfig config,
|
||||
RegionResolver regionResolver) {
|
||||
RegionResolver regionResolver, Clock clock) {
|
||||
this.provisioner = provisioner;
|
||||
this.s3Service = s3Service;
|
||||
this.objectMapper = objectMapper;
|
||||
this.config = config;
|
||||
this.regionResolver = regionResolver;
|
||||
this.clock = clock;
|
||||
}
|
||||
|
||||
// ── DescribeStacks ────────────────────────────────────────────────────────
|
||||
|
||||
public List<Stack> describeStacks(String stackName, String region) {
|
||||
if (stackName != null && !stackName.isBlank()) {
|
||||
Stack stack = resolveStack(stackName, region);
|
||||
Stack stack = resolveStackForDescribe(stackName, region);
|
||||
if (stack == null) {
|
||||
throw new AwsException("ValidationError",
|
||||
"Stack with id " + stackName + " does not exist", 400);
|
||||
@@ -129,7 +133,7 @@ public class CloudFormationService {
|
||||
"CREATE_IN_PROGRESS".equals(stack.getStatus());
|
||||
|
||||
stack.setStatus(isCreate ? "CREATE_IN_PROGRESS" : "UPDATE_IN_PROGRESS");
|
||||
stack.setLastUpdatedTime(Instant.now());
|
||||
stack.setLastUpdatedTime(now());
|
||||
addEvent(stack, stack.getStackName(), stack.getStackId(),
|
||||
"AWS::CloudFormation::Stack", isCreate ? "CREATE_IN_PROGRESS" : "UPDATE_IN_PROGRESS", null);
|
||||
|
||||
@@ -156,6 +160,7 @@ public class CloudFormationService {
|
||||
// ── DeleteStack ───────────────────────────────────────────────────────────
|
||||
|
||||
public void deleteStack(String stackName, String region) {
|
||||
purgeExpiredDeletedStacks();
|
||||
Stack stack = resolveStack(stackName, region);
|
||||
if (stack == null) {
|
||||
return; // Already gone — no-op
|
||||
@@ -177,7 +182,11 @@ public class CloudFormationService {
|
||||
// ── DescribeStackEvents ───────────────────────────────────────────────────
|
||||
|
||||
public List<StackEvent> describeStackEvents(String stackName, String region) {
|
||||
Stack stack = getStackOrThrow(stackName, region);
|
||||
Stack stack = resolveStackForDescribe(stackName, region);
|
||||
if (stack == null) {
|
||||
throw new AwsException("ValidationError",
|
||||
"Stack with id " + stackName + " does not exist", 400);
|
||||
}
|
||||
List<StackEvent> events = new ArrayList<>(stack.getEvents());
|
||||
Collections.reverse(events);
|
||||
return events;
|
||||
@@ -370,7 +379,7 @@ public class CloudFormationService {
|
||||
|
||||
String completeStatus = isCreate ? "CREATE_COMPLETE" : "UPDATE_COMPLETE";
|
||||
stack.setStatus(completeStatus);
|
||||
stack.setLastUpdatedTime(Instant.now());
|
||||
stack.setLastUpdatedTime(now());
|
||||
addEvent(stack, stack.getStackName(), stack.getStackId(),
|
||||
"AWS::CloudFormation::Stack", completeStatus, null);
|
||||
LOG.infov("Stack {0} execution complete: {1}", stack.getStackName(), completeStatus);
|
||||
@@ -406,6 +415,9 @@ public class CloudFormationService {
|
||||
"AWS::CloudFormation::Stack", "DELETE_COMPLETE", null);
|
||||
removeStackExports(stack, region);
|
||||
stacks.remove(key(stack.getStackName(), region));
|
||||
deletedStacks.put(stack.getStackId(), new DeletedStackEntry(
|
||||
stack,
|
||||
now().plusSeconds(config.services().cloudformation().deletedStackRetentionSeconds())));
|
||||
LOG.infov("Stack {0} deleted", stack.getStackName());
|
||||
|
||||
} catch (Exception e) {
|
||||
@@ -618,7 +630,7 @@ public class CloudFormationService {
|
||||
stack.setStatus("REVIEW_IN_PROGRESS");
|
||||
String stackId = AwsArnUtils.Arn.of("cloudformation", region, regionResolver.getAccountId(), "stack/" + stackName + "/" + UUID.randomUUID()).toString();
|
||||
stack.setStackId(stackId);
|
||||
stack.setCreationTime(Instant.now());
|
||||
stack.setCreationTime(now());
|
||||
return stack;
|
||||
}
|
||||
|
||||
@@ -644,6 +656,41 @@ public class CloudFormationService {
|
||||
return stack;
|
||||
}
|
||||
|
||||
private Stack resolveStackForDescribe(String stackNameOrArn, String region) {
|
||||
Stack stack = resolveStack(stackNameOrArn, region);
|
||||
if (stack != null) {
|
||||
return stack;
|
||||
}
|
||||
if (stackNameOrArn != null && stackNameOrArn.startsWith("arn:")) {
|
||||
DeletedStackEntry deleted = deletedStacks.get(stackNameOrArn);
|
||||
if (deleted != null) {
|
||||
if (deleted.isExpired(now())) {
|
||||
deletedStacks.remove(stackNameOrArn, deleted);
|
||||
return null;
|
||||
}
|
||||
if (region.equals(deleted.stack().getRegion())) {
|
||||
return deleted.stack();
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Instant now() {
|
||||
return Instant.now(clock);
|
||||
}
|
||||
|
||||
private void purgeExpiredDeletedStacks() {
|
||||
Instant current = now();
|
||||
deletedStacks.entrySet().removeIf(entry -> entry.getValue().isExpired(current));
|
||||
}
|
||||
|
||||
private record DeletedStackEntry(Stack stack, Instant expiresAt) {
|
||||
private boolean isExpired(Instant now) {
|
||||
return now.isAfter(expiresAt);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a changeset name from either a short name or a full ARN.
|
||||
* The AWS CLI passes the full ARN (arn:aws:cloudformation:…:changeSet/<name>/<uuid>)
|
||||
|
||||
Reference in new issue
Block a user