mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat(cloudformation): add optional-value and delete cleanup hooks (#5067)
This commit is contained in:
1 parent
233d05eab1
commit
07b529e150
6 files changed
+258
-14
No files matched your search
+17
-4
@@ -1863,6 +1863,10 @@ public class CloudFormationService implements ResourceProvider {
|
||||
}
|
||||
|
||||
private List<UpdateCleanupFailure> finishCommittedResourceCleanup(Stack stack, String region) {
|
||||
return finishCommittedResourceCleanup(stack, region, false);
|
||||
}
|
||||
|
||||
private List<UpdateCleanupFailure> finishCommittedResourceCleanup(Stack stack, String region, boolean deleting) {
|
||||
List<UpdateCleanupFailure> failures = new ArrayList<>();
|
||||
// Dependents go before what they depend on, as when the stack is deleted: a displaced
|
||||
// listener has to go before the displaced target group it still forwards to, or that
|
||||
@@ -1883,7 +1887,8 @@ public class CloudFormationService implements ResourceProvider {
|
||||
null);
|
||||
}
|
||||
while (true) {
|
||||
UpdateCleanupResult result = dispatcher.completeUpdate(resource);
|
||||
UpdateCleanupResult result = deleting ? dispatcher.completeDeleteCleanup(resource)
|
||||
: dispatcher.completeUpdate(resource);
|
||||
if (!result.applicable()) {
|
||||
break;
|
||||
}
|
||||
@@ -1897,7 +1902,11 @@ public class CloudFormationService implements ResourceProvider {
|
||||
"DELETE_COMPLETE",
|
||||
null);
|
||||
}
|
||||
dispatcher.clearUpdate(resource);
|
||||
if (deleting) {
|
||||
dispatcher.clearDeleteCleanup(resource);
|
||||
} else {
|
||||
dispatcher.clearUpdate(resource);
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (result.attempts() < 3) {
|
||||
@@ -1916,7 +1925,11 @@ public class CloudFormationService implements ResourceProvider {
|
||||
resource.getResourceType(),
|
||||
"DELETE_FAILED",
|
||||
reason);
|
||||
dispatcher.clearUpdate(resource);
|
||||
if (deleting) {
|
||||
dispatcher.clearDeleteCleanup(resource);
|
||||
} else {
|
||||
dispatcher.clearUpdate(resource);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -2350,7 +2363,7 @@ public class CloudFormationService implements ResourceProvider {
|
||||
// last update left in place. An entity displaced by a replacement whose cleanup phase
|
||||
// never ended is named only by the cleanup the resource still carries, so the stack
|
||||
// deletes that one too: nothing else ever will.
|
||||
for (UpdateCleanupFailure displacedFailure : finishCommittedResourceCleanup(stack, region)) {
|
||||
for (UpdateCleanupFailure displacedFailure : finishCommittedResourceCleanup(stack, region, true)) {
|
||||
failedResources.add(displacedFailure.logicalId());
|
||||
}
|
||||
for (StackResource resource : resources) {
|
||||
|
||||
+57
-4
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.cloudformation;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.MissingNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.fasterxml.jackson.databind.node.TextNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
@@ -207,12 +208,10 @@ public class CloudFormationTemplateEngine {
|
||||
if (node.has("Fn::Split") || node.has("Fn::GetAZs") || node.has("Fn::Cidr")) {
|
||||
return objectMapper.valueToTree(resolveList(node));
|
||||
}
|
||||
if (node.has("Ref") || node.has("Fn::Sub") || node.has("Fn::Join") ||
|
||||
node.has("Fn::Select") || node.has("Fn::Base64") ||
|
||||
node.has("Fn::GetAtt") || node.has("Fn::ImportValue") || node.has("Fn::FindInMap")) {
|
||||
if (isIntrinsic(node)) {
|
||||
return TextNode.valueOf(resolve(node));
|
||||
}
|
||||
// Plain object — resolve each field
|
||||
// Plain object: resolve each field.
|
||||
ObjectNode resolved = objectMapper.createObjectNode();
|
||||
Iterator<Map.Entry<String, JsonNode>> fields = node.fields();
|
||||
while (fields.hasNext()) {
|
||||
@@ -231,6 +230,60 @@ public class CloudFormationTemplateEngine {
|
||||
return node;
|
||||
}
|
||||
|
||||
/** Resolves optional structured properties, omitting only selected AWS::NoValue expressions. */
|
||||
public JsonNode resolveNodeOmittingNoValue(JsonNode node) {
|
||||
return resolveNode(omitNoValue(node));
|
||||
}
|
||||
|
||||
private JsonNode omitNoValue(JsonNode node) {
|
||||
if (node == null || node.isNull() || node.isMissingNode()) {
|
||||
return node;
|
||||
}
|
||||
if (node.isObject()) {
|
||||
if (node.size() == 1 && "AWS::NoValue".equals(node.path("Ref").asText())) {
|
||||
return MissingNode.getInstance();
|
||||
}
|
||||
if (node.size() == 1 && node.has("Fn::If")) {
|
||||
JsonNode branch = selectIfBranch(node.get("Fn::If"));
|
||||
if (branch != null) {
|
||||
return omitNoValue(branch);
|
||||
}
|
||||
}
|
||||
if (isIntrinsic(node)) {
|
||||
return node;
|
||||
}
|
||||
ObjectNode resolved = objectMapper.createObjectNode();
|
||||
Iterator<Map.Entry<String, JsonNode>> fields = node.fields();
|
||||
while (fields.hasNext()) {
|
||||
Map.Entry<String, JsonNode> field = fields.next();
|
||||
JsonNode value = omitNoValue(field.getValue());
|
||||
if (!value.isMissingNode()) {
|
||||
resolved.set(field.getKey(), value);
|
||||
}
|
||||
}
|
||||
return resolved;
|
||||
}
|
||||
if (node.isArray()) {
|
||||
ArrayNode resolved = objectMapper.createArrayNode();
|
||||
for (JsonNode item : node) {
|
||||
JsonNode value = omitNoValue(item);
|
||||
if (!value.isMissingNode()) {
|
||||
resolved.add(value);
|
||||
}
|
||||
}
|
||||
return resolved;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
private static boolean isIntrinsic(JsonNode node) {
|
||||
return node.has("Ref") || node.has("Fn::Sub") || node.has("Fn::Join")
|
||||
|| node.has("Fn::Select") || node.has("Fn::Base64") || node.has("Fn::GetAtt")
|
||||
|| node.has("Fn::ImportValue") || node.has("Fn::FindInMap")
|
||||
|| node.has("Fn::Split") || node.has("Fn::GetAZs") || node.has("Fn::Cidr")
|
||||
|| node.has("Fn::If");
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a value against {@link #dynamicReferenceResolver} when it carries CloudFormation
|
||||
* dynamic reference syntax ({@code {{resolve:ssm:...}}}, {@code {{resolve:secretsmanager:...}}}).
|
||||
|
||||
+13
@@ -238,6 +238,19 @@ public class CfnResourceDispatcher {
|
||||
.orElseGet(UpdateCleanupResult::notApplicable);
|
||||
}
|
||||
|
||||
/** Cleanup owed by an unfinished update while deleting the enclosing stack. */
|
||||
public UpdateCleanupResult completeDeleteCleanup(StackResource resource) {
|
||||
return registry.forType(resource.getResourceType())
|
||||
.map(owner -> owner.completeDeleteCleanup(resource))
|
||||
.filter(UpdateCleanupResult::applicable)
|
||||
.orElseGet(UpdateCleanupResult::notApplicable);
|
||||
}
|
||||
|
||||
/** Clears completed replacement cleanup without assuming a stack update committed. */
|
||||
public void clearDeleteCleanup(StackResource resource) {
|
||||
registry.forType(resource.getResourceType()).ifPresent(owner -> owner.clearDeleteCleanup(resource));
|
||||
}
|
||||
|
||||
/**
|
||||
* The physical id this update displaced, announced as DELETE_IN_PROGRESS before the stack
|
||||
* update closes. A type whose {@code UpdateReplacePolicy} is {@code Retain} owes no cleanup.
|
||||
|
||||
+18
@@ -78,6 +78,15 @@ public interface CfnResourceProvisioner {
|
||||
return UpdateCleanupResult.notApplicable();
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes entities displaced by an unfinished update before the stack itself is deleted.
|
||||
* The default uses normal replacement cleanup. A provisioner may distinguish this operation
|
||||
* from committing an update when it still carries configuration needed for rollback.
|
||||
*/
|
||||
default UpdateCleanupResult completeDeleteCleanup(StackResource resource) {
|
||||
return completeUpdate(resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this update replaced the physical entity, so the stack has cleanup pending and enters
|
||||
* UPDATE_COMPLETE_CLEANUP_IN_PROGRESS. The default reports no replacement.
|
||||
@@ -94,6 +103,15 @@ public interface CfnResourceProvisioner {
|
||||
// no-op by default: a type with no replacement cleanup records nothing to clear
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops completed replacement cleanup during stack deletion. The default preserves the normal
|
||||
* cleanup behavior; an override may keep rollback configuration until the managed entity is
|
||||
* successfully deleted.
|
||||
*/
|
||||
default void clearDeleteCleanup(StackResource resource) {
|
||||
clearUpdate(resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Carries additive ownership tracking a failed update discovered onto the last known-good
|
||||
* resource metadata CloudFormation restores, so entities the failed attempt created but could
|
||||
|
||||
Reference in new issue
Block a user