mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(cloudformation): honor resource-level DeletionPolicy (#2167)
A resource's DeletionPolicy attribute was never read off the template, so every managed resource was deleted regardless of what it declared: deleteStackResources and rollbackCreatedResources both called provisioner.delete unconditionally. Read DeletionPolicy alongside Type in executeTemplate and apply it at both delete sites. Retain keeps the resource on every stack operation. RetainExceptOnCreate keeps it too, except when the create that made it is rolled back — deleting it there is the entire meaning of that value, and rollbackCreatedResources runs only for a failed create. A retained resource is reported as DELETE_SKIPPED, on the resource and as a stack event, and is not counted as a failed delete, so the stack still reaches DELETE_COMPLETE. That also lets a stack owning a non-empty S3 bucket be deleted, which otherwise fails on the objects. Any other value falls through to the existing delete. That includes Snapshot: floci cannot take the snapshots AWS supports for those types, and AWS does delete the resource under Snapshot, so Delete is the faithful half. The docs record that and the other divergences. The policy is set on the resource after the provisioner returns, because provision() and executeNestedStack() both return a fresh StackResource that replaces the map entry. Closes #1555
This commit is contained in:
1 parent
1795e131a2
commit
7d9b9aedd2
4 files changed
+309
No files matched your search
+33
@@ -454,6 +454,7 @@ public class CloudFormationService {
|
||||
for (String logicalId : sortedLogicalIds) {
|
||||
JsonNode resDef = resources.get(logicalId);
|
||||
String type = resDef.path("Type").asText();
|
||||
String deletionPolicy = resDef.path("DeletionPolicy").asText(null);
|
||||
JsonNode props = resDef.path("Properties");
|
||||
|
||||
CloudFormationTemplateEngine engine = new CloudFormationTemplateEngine(
|
||||
@@ -479,6 +480,9 @@ public class CloudFormationService {
|
||||
engine, region, accountId, stack.getStackName(),
|
||||
resource.getPhysicalId(), resource.getAttributes());
|
||||
}
|
||||
// Both branches return a fresh StackResource, so the policy is carried over here
|
||||
// rather than on the instance the loop started with.
|
||||
resource.setDeletionPolicy(deletionPolicy);
|
||||
stack.getResources().put(logicalId, resource);
|
||||
|
||||
physicalIds.put(logicalId, resource.getPhysicalId());
|
||||
@@ -632,6 +636,9 @@ public class CloudFormationService {
|
||||
if (resource.getPhysicalId() == null || (!completed && !ownedFailedResource)) {
|
||||
continue;
|
||||
}
|
||||
if (skipRetainedResource(stack, resource, true)) {
|
||||
continue;
|
||||
}
|
||||
addEvent(stack, resource.getLogicalId(), resource.getPhysicalId(),
|
||||
resource.getResourceType(), "DELETE_IN_PROGRESS", null);
|
||||
try {
|
||||
@@ -667,6 +674,9 @@ public class CloudFormationService {
|
||||
if (resource.getPhysicalId() == null || !deletable) {
|
||||
continue;
|
||||
}
|
||||
if (skipRetainedResource(stack, resource, false)) {
|
||||
continue;
|
||||
}
|
||||
addEvent(stack, resource.getLogicalId(), resource.getPhysicalId(),
|
||||
resource.getResourceType(), "DELETE_IN_PROGRESS", null);
|
||||
try {
|
||||
@@ -719,6 +729,29 @@ public class CloudFormationService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies a resource's {@code DeletionPolicy}. {@code Retain} keeps the resource on every stack
|
||||
* operation; {@code RetainExceptOnCreate} keeps it too, except when the create that made it is
|
||||
* rolled back. Every other value — including {@code Snapshot}, which floci cannot snapshot —
|
||||
* falls through to a normal delete, matching the default.
|
||||
*
|
||||
* @return {@code true} when the resource was kept and reported as {@code DELETE_SKIPPED}
|
||||
*/
|
||||
private boolean skipRetainedResource(Stack stack, StackResource resource, boolean createRollback) {
|
||||
String policy = resource.getDeletionPolicy();
|
||||
boolean retained = "Retain".equals(policy)
|
||||
|| (!createRollback && "RetainExceptOnCreate".equals(policy));
|
||||
if (!retained) {
|
||||
return false;
|
||||
}
|
||||
resource.setStatus("DELETE_SKIPPED");
|
||||
addEvent(stack, resource.getLogicalId(), resource.getPhysicalId(),
|
||||
resource.getResourceType(), "DELETE_SKIPPED", null);
|
||||
LOG.infov("Retained {0} ({1}) in stack {2}: DeletionPolicy {3}",
|
||||
resource.getResourceType(), resource.getPhysicalId(), stack.getStackName(), policy);
|
||||
return true;
|
||||
}
|
||||
|
||||
private Map<String, Boolean> resolveConditions(JsonNode template, Map<String, String> params,
|
||||
Stack stack, String region, String accountId) {
|
||||
Map<String, Boolean> conditions = new HashMap<>();
|
||||
|
||||
+3
@@ -13,6 +13,7 @@ public class StackResource {
|
||||
private String logicalId;
|
||||
private String physicalId;
|
||||
private String resourceType;
|
||||
private String deletionPolicy;
|
||||
private String status = "CREATE_IN_PROGRESS";
|
||||
private String statusReason;
|
||||
private Instant timestamp = Instant.now();
|
||||
@@ -24,6 +25,8 @@ public class StackResource {
|
||||
public void setPhysicalId(String physicalId) { this.physicalId = physicalId; }
|
||||
public String getResourceType() { return resourceType; }
|
||||
public void setResourceType(String resourceType) { this.resourceType = resourceType; }
|
||||
public String getDeletionPolicy() { return deletionPolicy; }
|
||||
public void setDeletionPolicy(String deletionPolicy) { this.deletionPolicy = deletionPolicy; }
|
||||
public String getStatus() { return status; }
|
||||
public void setStatus(String status) { this.status = status; }
|
||||
public String getStatusReason() { return statusReason; }
|
||||
|
||||
Reference in new issue
Block a user