mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
refactor(cloudformation): tell each provisioner what the update changed in its properties (#8087)
This commit is contained in:
1 parent
c0ef5b2bfd
commit
5876a37888
15 files changed
+1069
-31
No files matched your search
@@ -31,6 +31,7 @@ import io.floci.aws.services.cloudformation.model.TemplateSummary;
|
||||
import io.floci.aws.services.cloudformation.provisioners.CfnDynamicReferences;
|
||||
import io.floci.aws.services.cloudformation.provisioners.CfnResourceDispatcher;
|
||||
import io.floci.aws.services.cloudformation.provisioners.CfnRollback;
|
||||
import io.floci.aws.services.cloudformation.provisioners.PropertyChanges;
|
||||
import io.floci.aws.services.cloudformation.provisioners.ProvisionContext;
|
||||
import io.floci.aws.services.cloudformation.provisioners.UpdateCleanupResult;
|
||||
import io.floci.aws.services.s3.S3Service;
|
||||
@@ -1004,6 +1005,17 @@ public class CloudFormationService implements ResourceProvider {
|
||||
* executed a template) report every resource with a truthy {@code Condition} as an Add.
|
||||
*/
|
||||
public List<ResourceChange> computeChangeSetChanges(ChangeSet cs, String region) {
|
||||
return computeChangeSetChanges(cs, region, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* As above. When {@code comparedProperties} is given, it receives for each modified resource the
|
||||
* {@code Properties} of the forms the diff compared, previous then current, so an update can tell
|
||||
* which of a resource's properties changed. A resource diffed on its definitions as written, or
|
||||
* modified only because it reads a replaced resource or a changed attribute, gets no entry.
|
||||
*/
|
||||
private List<ResourceChange> computeChangeSetChanges(ChangeSet cs, String region,
|
||||
Map<String, JsonNode[]> comparedProperties) {
|
||||
if ("FAILED".equals(cs.getStatus())) {
|
||||
// A SAM transform failure already recorded by createChangeSet (see
|
||||
// samTransformFailureReason): the change set carries 0 changes, matching real
|
||||
@@ -1156,6 +1168,11 @@ public class CloudFormationService implements ResourceProvider {
|
||||
if (replacement) {
|
||||
replacedResourceIds.add(logicalId);
|
||||
}
|
||||
if (comparedProperties != null && compared[0] != null) {
|
||||
comparedProperties.put(logicalId, new JsonNode[] {
|
||||
compared[0].path(logicalId).path("Properties"),
|
||||
compared[1].path(logicalId).path("Properties")});
|
||||
}
|
||||
changes.add(new ResourceChange("Modify", logicalId,
|
||||
resourcePhysicalId(stack, logicalId), resourceType,
|
||||
replacement ? "True" : "False"));
|
||||
@@ -2334,9 +2351,10 @@ public class CloudFormationService implements ResourceProvider {
|
||||
boolean updateCommitted = false;
|
||||
Set<String> attemptedResourceIds = new LinkedHashSet<>();
|
||||
try {
|
||||
Map<String, JsonNode[]> comparedProperties = new HashMap<>();
|
||||
Set<String> changedResourceIds = isCreate
|
||||
? Set.of()
|
||||
: changedResourceIds(stack, templateBody, params, region);
|
||||
: changedResourceIds(stack, templateBody, params, region, comparedProperties);
|
||||
JsonNode previousResources = isCreate
|
||||
? objectMapper.missingNode()
|
||||
: previousResources(stack, previousState, region, accountId);
|
||||
@@ -2487,12 +2505,19 @@ public class CloudFormationService implements ResourceProvider {
|
||||
JsonNode priorProps = type.equals(previousDefinition.path("Type").asText())
|
||||
&& previousDefinition.path("Properties").isObject()
|
||||
? previousDefinition.path("Properties") : null;
|
||||
resource = dispatcher.provision(logicalId, type, props.isMissingNode() ? null : props,
|
||||
JsonNode provisionProps = props.isMissingNode() ? null : props;
|
||||
JsonNode[] comparedForms = comparedProperties.get(logicalId);
|
||||
PropertyChanges changes = PropertyChanges.between(type,
|
||||
ProvisionContext.isUpdate(resource.getPhysicalId()), priorProps,
|
||||
provisionProps,
|
||||
comparedForms != null ? comparedForms[0] : null,
|
||||
comparedForms != null ? comparedForms[1] : null);
|
||||
resource = dispatcher.provision(logicalId, type, provisionProps,
|
||||
engine, region, accountId, stack.getStackName(),
|
||||
resource.getPhysicalId(), resource.getAttributes(),
|
||||
event -> addEvent(stack, logicalId, event.getPhysicalResourceId(), type,
|
||||
event.getResourceStatus(), event.getResourceStatusReason()),
|
||||
priorProps);
|
||||
priorProps, changes);
|
||||
}
|
||||
resource.setUpdateReplacePolicy(
|
||||
resDef.path("UpdateReplacePolicy").asText(null));
|
||||
@@ -2756,8 +2781,13 @@ public class CloudFormationService implements ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The ids of the resources an update adds or modifies. {@code comparedProperties} receives, for
|
||||
* the modified ones the diff compared, the {@code Properties} of the compared forms, previous
|
||||
* then current; see the change-set diff it comes from.
|
||||
*/
|
||||
private Set<String> changedResourceIds(Stack stack, String templateBody, Map<String, String> params,
|
||||
String region) {
|
||||
String region, Map<String, JsonNode[]> comparedProperties) {
|
||||
ChangeSet changeSet = new ChangeSet();
|
||||
changeSet.setStackName(stack.getStackName());
|
||||
changeSet.setStackId(stack.getStackId());
|
||||
@@ -2766,7 +2796,7 @@ public class CloudFormationService implements ResourceProvider {
|
||||
changeSet.setParameters(params);
|
||||
|
||||
Set<String> changedResourceIds = new LinkedHashSet<>();
|
||||
for (ResourceChange change : computeChangeSetChanges(changeSet, region)) {
|
||||
for (ResourceChange change : computeChangeSetChanges(changeSet, region, comparedProperties)) {
|
||||
if ("Add".equals(change.action()) || "Modify".equals(change.action())) {
|
||||
changedResourceIds.add(change.logicalResourceId());
|
||||
}
|
||||
|
||||
+16
-5
@@ -90,15 +90,26 @@ public class CfnResourceDispatcher {
|
||||
existingPhysicalId, existingAttributes, event -> {}, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code priorProperties} are the resource's properties in the previous template, unresolved,
|
||||
* or null when there is none; see {@link ProvisionContext#previouslyDeclared(String)}.
|
||||
*/
|
||||
public StackResource provision(String logicalId, String resourceType, JsonNode properties,
|
||||
CloudFormationTemplateEngine engine, String region, String accountId,
|
||||
String stackName, String existingPhysicalId,
|
||||
Map<String, String> existingAttributes, Consumer<StackEvent> progress,
|
||||
JsonNode priorProperties) {
|
||||
return provision(logicalId, resourceType, properties, engine, region, accountId, stackName,
|
||||
existingPhysicalId, existingAttributes, progress, priorProperties, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code priorProperties} are the resource's properties in the previous template, unresolved,
|
||||
* or null when there is none; see {@link ProvisionContext#previouslyDeclared(String)}.
|
||||
* {@code changes} is what the engine found changed in them, or null when it made no statement;
|
||||
* see {@link ProvisionContext#changes(String, JsonNode)}.
|
||||
*/
|
||||
public StackResource provision(String logicalId, String resourceType, JsonNode properties,
|
||||
CloudFormationTemplateEngine engine, String region, String accountId,
|
||||
String stackName, String existingPhysicalId,
|
||||
Map<String, String> existingAttributes, Consumer<StackEvent> progress,
|
||||
JsonNode priorProperties, PropertyChanges changes) {
|
||||
StackResource resource = new StackResource();
|
||||
resource.setLogicalId(logicalId);
|
||||
resource.setResourceType(resourceType);
|
||||
@@ -110,7 +121,7 @@ public class CfnResourceDispatcher {
|
||||
if (owner != null) {
|
||||
owner.provision(resource, properties,
|
||||
new ProvisionContext(engine, region, accountId, stackName, existingPhysicalId, progress,
|
||||
priorProperties));
|
||||
priorProperties, changes));
|
||||
// A provisioner that migrated a stub without writing its own Arn would leave the
|
||||
// stub's, and the real resource would read as a stub on its next update.
|
||||
String arn = resource.getAttributes().get("Arn");
|
||||
|
||||
+1
-2
@@ -118,8 +118,7 @@ final class CognitoPoolScopedLifecycle {
|
||||
}
|
||||
String cleanupPrior = prior.stub() ? prior.physicalId()
|
||||
: prior.identity() == null ? null : encode(prior.identity());
|
||||
ProvisionContext cleanupContext = new ProvisionContext(ctx.engine(), ctx.region(), ctx.accountId(),
|
||||
ctx.stackName(), cleanupPrior, ctx.progress(), ctx.priorProperties());
|
||||
ProvisionContext cleanupContext = ctx.withPriorPhysicalId(cleanupPrior);
|
||||
withProjection(resource, projected -> {
|
||||
ReplacementCleanup.record(projected, cleanupContext, attributesBefore);
|
||||
return null;
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
package io.floci.aws.services.cloudformation.provisioners;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.floci.aws.services.cloudformation.CfnCreateOnlyProperties;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* What an update did to each top-level property of one resource: added, changed, dropped or
|
||||
* unchanged, and whether the registry marks it create-only. The engine states it once per
|
||||
* resource so a provisioner reads it instead of deriving it from the two templates.
|
||||
*
|
||||
* <p>Declared-ness follows what the provisioner sees: a property whose {@code Fn::If} chose
|
||||
* {@code AWS::NoValue} is not declared on that side. Only the choice between changed and
|
||||
* unchanged can use the forms CloudFormation compares.
|
||||
*
|
||||
* <p>Names keep the previous template's field order for the ones it declared, then the rest in
|
||||
* the order of the current properties, so {@link #dropped()} keeps the order callers saw before
|
||||
* this type existed.
|
||||
*
|
||||
* <p>UNCHANGED means unchanged in the template. A property whose {@code Ref} or
|
||||
* {@code Fn::GetAtt} reads a resource this update replaces still reads UNCHANGED, so UNCHANGED
|
||||
* alone is no reason to skip a write.
|
||||
*/
|
||||
public record PropertyChanges(Map<String, PropertyChange> byName) {
|
||||
|
||||
public enum Kind { ADDED, CHANGED, DROPPED, UNCHANGED }
|
||||
|
||||
/**
|
||||
* One property's fate. {@code prior} and {@code current} are the nodes as the previous and the
|
||||
* current properties hold them, null on the side that does not declare the name.
|
||||
*/
|
||||
public record PropertyChange(String name, Kind kind, JsonNode prior, JsonNode current,
|
||||
boolean createOnly) {}
|
||||
|
||||
public PropertyChanges {
|
||||
byName = byName == null
|
||||
? Map.of()
|
||||
: Collections.unmodifiableMap(new LinkedHashMap<>(byName));
|
||||
}
|
||||
|
||||
/** What the provisioner's own inputs say, values compared as written. */
|
||||
public static PropertyChanges between(String resourceType, boolean update,
|
||||
JsonNode priorProperties, JsonNode properties) {
|
||||
return between(resourceType, update, priorProperties, properties, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* The same, with changed against unchanged decided by the forms CloudFormation compares when
|
||||
* both are given and hold the name, and by the values as written otherwise.
|
||||
*/
|
||||
public static PropertyChanges between(String resourceType, boolean update,
|
||||
JsonNode priorProperties, JsonNode properties,
|
||||
JsonNode comparedPrior, JsonNode comparedCurrent) {
|
||||
Map<String, PropertyChange> changes = new LinkedHashMap<>();
|
||||
boolean currentIsObject = properties != null && properties.isObject();
|
||||
if (!update || priorProperties == null || !priorProperties.isObject()) {
|
||||
if (currentIsObject) {
|
||||
properties.fieldNames().forEachRemaining(name -> changes.put(name,
|
||||
change(resourceType, name, Kind.ADDED, null, properties.get(name))));
|
||||
}
|
||||
return new PropertyChanges(changes);
|
||||
}
|
||||
priorProperties.fieldNames().forEachRemaining(name -> {
|
||||
JsonNode before = priorProperties.get(name);
|
||||
if (before.isNull()) {
|
||||
return;
|
||||
}
|
||||
if (currentIsObject && properties.has(name)) {
|
||||
JsonNode now = properties.get(name);
|
||||
Kind kind = unchanged(name, before, now, comparedPrior, comparedCurrent)
|
||||
? Kind.UNCHANGED : Kind.CHANGED;
|
||||
changes.put(name, change(resourceType, name, kind, before, now));
|
||||
} else {
|
||||
changes.put(name, change(resourceType, name, Kind.DROPPED, before, null));
|
||||
}
|
||||
});
|
||||
if (currentIsObject) {
|
||||
properties.fieldNames().forEachRemaining(name -> {
|
||||
if (!changes.containsKey(name)) {
|
||||
changes.put(name, change(resourceType, name, Kind.ADDED, null, properties.get(name)));
|
||||
}
|
||||
});
|
||||
}
|
||||
return new PropertyChanges(changes);
|
||||
}
|
||||
|
||||
private static PropertyChange change(String resourceType, String name, Kind kind,
|
||||
JsonNode prior, JsonNode current) {
|
||||
boolean createOnly = resourceType != null
|
||||
&& CfnCreateOnlyProperties.isCreateOnly(resourceType, name);
|
||||
return new PropertyChange(name, kind, prior, current, createOnly);
|
||||
}
|
||||
|
||||
private static boolean unchanged(String name, JsonNode before, JsonNode now,
|
||||
JsonNode comparedPrior, JsonNode comparedCurrent) {
|
||||
if (comparedPrior != null && comparedCurrent != null) {
|
||||
JsonNode comparedBefore = comparedPrior.path(name);
|
||||
JsonNode comparedNow = comparedCurrent.path(name);
|
||||
if (!comparedBefore.isMissingNode() && !comparedNow.isMissingNode()) {
|
||||
return comparedBefore.equals(comparedNow);
|
||||
}
|
||||
}
|
||||
return before.equals(now);
|
||||
}
|
||||
|
||||
/** The kind of {@code name}, or null when it is in neither template. */
|
||||
public Kind kindOf(String name) {
|
||||
PropertyChange found = byName.get(name);
|
||||
return found == null ? null : found.kind();
|
||||
}
|
||||
|
||||
public Set<String> added() {
|
||||
return names(Kind.ADDED);
|
||||
}
|
||||
|
||||
public Set<String> changed() {
|
||||
return names(Kind.CHANGED);
|
||||
}
|
||||
|
||||
public Set<String> dropped() {
|
||||
return names(Kind.DROPPED);
|
||||
}
|
||||
|
||||
public Set<String> unchanged() {
|
||||
return names(Kind.UNCHANGED);
|
||||
}
|
||||
|
||||
private Set<String> names(Kind kind) {
|
||||
Set<String> names = new LinkedHashSet<>();
|
||||
for (PropertyChange found : byName.values()) {
|
||||
if (found.kind() == kind) {
|
||||
names.add(found.name());
|
||||
}
|
||||
}
|
||||
return Collections.unmodifiableSet(names);
|
||||
}
|
||||
}
|
||||
+42
-12
@@ -7,7 +7,6 @@ import io.floci.aws.services.cloudformation.model.StackEvent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@@ -22,11 +21,29 @@ import java.util.function.Consumer;
|
||||
*/
|
||||
public record ProvisionContext(CloudFormationTemplateEngine engine, String region,
|
||||
String accountId, String stackName, String priorPhysicalId,
|
||||
Consumer<StackEvent> progress, JsonNode priorProperties) {
|
||||
Consumer<StackEvent> progress, JsonNode priorProperties,
|
||||
PropertyChanges changes) {
|
||||
|
||||
public ProvisionContext(CloudFormationTemplateEngine engine, String region,
|
||||
String accountId, String stackName, String priorPhysicalId,
|
||||
Consumer<StackEvent> progress, JsonNode priorProperties) {
|
||||
this(engine, region, accountId, stackName, priorPhysicalId, progress, priorProperties, null);
|
||||
}
|
||||
|
||||
public ProvisionContext(CloudFormationTemplateEngine engine, String region,
|
||||
String accountId, String stackName, String priorPhysicalId) {
|
||||
this(engine, region, accountId, stackName, priorPhysicalId, event -> {}, null);
|
||||
this(engine, region, accountId, stackName, priorPhysicalId, event -> {}, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* A copy of this context that differs only in the prior physical id. The engine's change list,
|
||||
* the previous properties and the progress callback travel with it, so a provisioner that
|
||||
* re-bases its context (after restoring a rollback snapshot, or for a cleanup record) does not
|
||||
* drop the engine's statement of the update, and a component added later is kept too.
|
||||
*/
|
||||
public ProvisionContext withPriorPhysicalId(String priorPhysicalId) {
|
||||
return new ProvisionContext(engine, region, accountId, stackName, priorPhysicalId, progress,
|
||||
priorProperties, changes);
|
||||
}
|
||||
|
||||
/** Intermediate resource events retain the stack pipeline's event IDs and metadata. */
|
||||
@@ -58,6 +75,15 @@ public record ProvisionContext(CloudFormationTemplateEngine engine, String regio
|
||||
* this as "reuse the existing entity" must still reject or replace on createOnly properties.
|
||||
*/
|
||||
public boolean isUpdate() {
|
||||
return isUpdate(priorPhysicalId);
|
||||
}
|
||||
|
||||
/**
|
||||
* The one definition of "this call is an update": a non-blank prior physical id. The engine
|
||||
* uses it to build the property changes it hands a provisioner, so the statement and
|
||||
* {@link #isUpdate()} cannot disagree.
|
||||
*/
|
||||
public static boolean isUpdate(String priorPhysicalId) {
|
||||
return priorPhysicalId != null && !priorPhysicalId.isBlank();
|
||||
}
|
||||
|
||||
@@ -88,16 +114,20 @@ public record ProvisionContext(CloudFormationTemplateEngine engine, String regio
|
||||
* (a create-only property is a replacement question, not a clear).
|
||||
*/
|
||||
public Set<String> droppedProperties(JsonNode props) {
|
||||
Set<String> dropped = new LinkedHashSet<>();
|
||||
if (!isUpdate() || priorProperties == null) {
|
||||
return dropped;
|
||||
return PropertyChanges.between(null, isUpdate(), priorProperties, props).dropped();
|
||||
}
|
||||
|
||||
/**
|
||||
* What this update did to each top-level property of the resource: the engine's own statement
|
||||
* when it made one, which decides changed against unchanged by the forms CloudFormation
|
||||
* compares, otherwise one derived from this context's previous properties and {@code props}.
|
||||
* {@code resourceType} only tells which properties are create-only on the derived form.
|
||||
*/
|
||||
public PropertyChanges changes(String resourceType, JsonNode props) {
|
||||
if (changes != null) {
|
||||
return changes;
|
||||
}
|
||||
priorProperties.fieldNames().forEachRemaining(name -> {
|
||||
if (previouslyDeclared(name) && (props == null || !props.has(name))) {
|
||||
dropped.add(name);
|
||||
}
|
||||
});
|
||||
return dropped;
|
||||
return PropertyChanges.between(resourceType, isUpdate(), priorProperties, props);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+2
-4
@@ -63,8 +63,7 @@ public class SchedulerScheduleCfnProvisioner implements CfnResourceProvisioner {
|
||||
if (ctx.isUpdate() && resource.getAttributes().containsKey(SNAPSHOT_ATTR)) {
|
||||
try {
|
||||
rollbackUpdate(resource);
|
||||
ctx = new ProvisionContext(ctx.engine(), ctx.region(), ctx.accountId(), ctx.stackName(),
|
||||
resource.getPhysicalId(), ctx.progress(), ctx.priorProperties());
|
||||
ctx = ctx.withPriorPhysicalId(resource.getPhysicalId());
|
||||
resource.getAttributes().remove(CfnRollback.UPDATE_ROLLBACK_FAILURE_ATTR);
|
||||
resource.getAttributes().remove(CfnRollback.UPDATE_ROLLBACK_RESTORED_ATTR);
|
||||
} catch (RuntimeException failure) {
|
||||
@@ -137,8 +136,7 @@ public class SchedulerScheduleCfnProvisioner implements CfnResourceProvisioner {
|
||||
priorIdentity = CfnResourceDispatcher.isStub(ctx.priorPhysicalId(), attributesBefore)
|
||||
? ctx.priorPhysicalId() : address(priorGroup, ctx.priorPhysicalId());
|
||||
}
|
||||
ProvisionContext cleanupContext = new ProvisionContext(ctx.engine(), ctx.region(), ctx.accountId(),
|
||||
ctx.stackName(), priorIdentity, ctx.progress(), ctx.priorProperties());
|
||||
ProvisionContext cleanupContext = ctx.withPriorPhysicalId(priorIdentity);
|
||||
ReplacementCleanup.record(cleanupResource(resource), cleanupContext, attributesBefore);
|
||||
if (moveSnapshot != null) {
|
||||
moveSnapshot.put("destinationCreated", true);
|
||||
|
||||
+1
-2
@@ -52,8 +52,7 @@ public class SesCfnProvisioner implements CfnResourceProvisioner {
|
||||
// it before either an in-place update or replacement can overwrite its snapshot.
|
||||
rollbackUpdate(resource);
|
||||
recoveredSnapshot = true;
|
||||
ctx = new ProvisionContext(ctx.engine(), ctx.region(), ctx.accountId(), ctx.stackName(),
|
||||
resource.getPhysicalId(), ctx.progress(), ctx.priorProperties());
|
||||
ctx = ctx.withPriorPhysicalId(resource.getPhysicalId());
|
||||
}
|
||||
String identityName = desired.identity();
|
||||
boolean updating = ctx.reusesPriorEntity(identityName);
|
||||
|
||||
Reference in new issue
Block a user