mirror of
https://github.com/floci-io/floci.git
synced 2026-11-28 08:09:43 -05:00
fix(cloudformation): report and continue a failed REST API update rollback (#6568)
This commit is contained in:
1 parent
6b21aebdc3
commit
e1726e8394
7 files changed
+421
-48
No files matched your search
+59
-27
@@ -179,6 +179,21 @@ public class ApiGatewayRestApiCfnProvisioner implements CfnResourceProvisioner {
|
||||
ReplacementCleanup.clear(resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Keeps the snapshot of a kept API whose own undo did not complete. The engine restores the
|
||||
* previous resource and reports the rollback failure once; with the snapshot, a
|
||||
* ContinueUpdateRollback tries the restore again, or fails again when the update's document had
|
||||
* replaced the definition, instead of finding nothing to undo.
|
||||
*/
|
||||
@Override
|
||||
public void mergeFailedUpdateResourceTracking(StackResource previous, StackResource attempted) {
|
||||
String snapshot = attempted.getAttributes().get(CfnRollback.REST_API_UPDATE_SNAPSHOT_ATTR);
|
||||
if (REST_API.equals(previous.getResourceType()) && snapshot != null
|
||||
&& attempted.getAttributes().containsKey(CfnRollback.UPDATE_ROLLBACK_FAILURE_ATTR)) {
|
||||
previous.getAttributes().put(CfnRollback.REST_API_UPDATE_SNAPSHOT_ATTR, snapshot);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Puts a replaced RestApi, Resource or Method back when a later resource fails the update, and a
|
||||
* RestApi patched in place back to its snapshot. Without a replacement, a Resource update
|
||||
@@ -198,8 +213,8 @@ public class ApiGatewayRestApiCfnProvisioner implements CfnResourceProvisioner {
|
||||
}
|
||||
|
||||
/**
|
||||
* Patches the API back from the snapshot taken before its in-place update. An update that also
|
||||
* re-applied an OpenAPI document replaced the API's resources, methods and models, which the
|
||||
* Patches the API back from the snapshot taken before its in-place update. An update whose
|
||||
* OpenAPI document was applied replaced the API's resources, methods and models, which the
|
||||
* snapshot does not hold, so its rollback is not implemented. With no snapshot, nothing changed.
|
||||
*/
|
||||
private boolean rollbackRestApiUpdate(StackResource resource) {
|
||||
@@ -286,12 +301,14 @@ public class ApiGatewayRestApiCfnProvisioner implements CfnResourceProvisioner {
|
||||
// applyOpenApiSpec is the only place that turns it into resources and methods. Resolved
|
||||
// before anything changes, so a document that cannot be read leaves the API as it was.
|
||||
JsonNode openApiDocument = OpenApiDocuments.resolve(props, engine, s3Service, objectMapper);
|
||||
// The declared Name and Description; null clears an undeclared Description.
|
||||
List<Map<String, String>> nameAndDescription = List.of(
|
||||
replacePatchOp("/name", name), replacePatchOp("/description", description));
|
||||
|
||||
RestApi api;
|
||||
if (existing != null) {
|
||||
snapshotBeforeUpdate(r, existing, region, openApiDocument != null);
|
||||
List<Map<String, String>> operations = new ArrayList<>(List.of(
|
||||
replacePatchOp("/name", name), replacePatchOp("/description", description)));
|
||||
snapshotBeforeUpdate(r, existing, region);
|
||||
List<Map<String, String>> operations = new ArrayList<>(nameAndDescription);
|
||||
if (endpoint != null) {
|
||||
operations.addAll(endpointPatchOps(existing.getEndpointConfiguration(),
|
||||
endpointTypes.isEmpty() ? null : endpointTypes.getFirst(),
|
||||
@@ -312,17 +329,22 @@ public class ApiGatewayRestApiCfnProvisioner implements CfnResourceProvisioner {
|
||||
}
|
||||
r.setPhysicalId(api.getId());
|
||||
boolean created = existing == null;
|
||||
// putRestApi overwrites Name and Description from the document's info, so the declared Name
|
||||
// and Description (null clearing an undeclared Description) are re-applied immediately after.
|
||||
List<Map<String, String>> declaredNameAndDescription = List.of(
|
||||
replacePatchOp("/name", name), replacePatchOp("/description", description));
|
||||
// A step that fails once the API exists undoes this provision: an API it created is deleted
|
||||
// again (or left marked for the stack's rollback when that delete fails too), and a kept API
|
||||
// gets back what the update patched. putRestApi leaves the API as it was when it rejects the
|
||||
// document.
|
||||
// A step that fails once the API exists undoes this provision. An API it created is deleted
|
||||
// again; if that delete fails too, a create leaves the API marked as created by the stack, and
|
||||
// an update lists it for the stack's next cleanup. A kept API gets back what the update
|
||||
// patched (putRestApi leaves the API as it was when it rejects the document); when its
|
||||
// document was already applied, the definition cannot be put back, which is reported as a
|
||||
// rollback failure.
|
||||
CfnRollback.guarded(r, ctx, created,
|
||||
() -> delete(REST_API, api.getId(), region),
|
||||
() -> restore(api.getId(), readSnapshot(r)),
|
||||
() -> {
|
||||
JsonNode snapshot = readSnapshot(r);
|
||||
restore(api.getId(), snapshot);
|
||||
if (snapshot.path("body").asBoolean(false)) {
|
||||
throw new IllegalStateException("its OpenAPI document was already applied, and the"
|
||||
+ " update keeps no copy of the definition it replaced");
|
||||
}
|
||||
},
|
||||
() -> {
|
||||
r.getAttributes().put("RestApiId", api.getId());
|
||||
// Not the first of getResources, which is the root only on a freshly created API.
|
||||
@@ -330,17 +352,12 @@ public class ApiGatewayRestApiCfnProvisioner implements CfnResourceProvisioner {
|
||||
apiGatewayService.findRootResourceId(region, api.getId()).orElseThrow());
|
||||
if (openApiDocument != null) {
|
||||
apiGatewayService.putRestApi(region, api.getId(), "overwrite", openApiDocument.toString());
|
||||
if (created) {
|
||||
apiGatewayService.updateRestApi(region, api.getId(), declaredNameAndDescription);
|
||||
}
|
||||
markDocumentApplied(r);
|
||||
// putRestApi overwrites Name and Description from the document's info, so the
|
||||
// declared ones are applied again.
|
||||
apiGatewayService.updateRestApi(region, api.getId(), nameAndDescription);
|
||||
}
|
||||
});
|
||||
// On a kept API the re-apply follows the guard: the document putRestApi accepted replaced the
|
||||
// API's definition, which the snapshot does not hold, so putting back only the patch would
|
||||
// not restore the API.
|
||||
if (!created && openApiDocument != null) {
|
||||
apiGatewayService.updateRestApi(region, api.getId(), declaredNameAndDescription);
|
||||
}
|
||||
// An API created in place of one removed out of band is a replacement: a rollback deletes it.
|
||||
ReplacementCleanup.record(r, ctx, attributesBefore);
|
||||
}
|
||||
@@ -381,10 +398,11 @@ public class ApiGatewayRestApiCfnProvisioner implements CfnResourceProvisioner {
|
||||
|
||||
/**
|
||||
* Keeps what an in-place update is about to patch, the name, description and endpoint
|
||||
* configuration the API has now, so {@link #rollbackUpdate} can put them back, and whether the
|
||||
* update also re-applies an OpenAPI document.
|
||||
* configuration the API has now, so {@link #rollbackUpdate} can put them back. Its {@code body}
|
||||
* flag starts false and is set by {@link #markDocumentApplied} once the update's OpenAPI
|
||||
* document has replaced the API's definition.
|
||||
*/
|
||||
private void snapshotBeforeUpdate(StackResource r, RestApi existing, String region, boolean body) {
|
||||
private void snapshotBeforeUpdate(StackResource r, RestApi existing, String region) {
|
||||
ObjectNode snapshot = objectMapper.createObjectNode();
|
||||
snapshot.put("region", region);
|
||||
snapshot.put("name", existing.getName());
|
||||
@@ -394,7 +412,21 @@ public class ApiGatewayRestApiCfnProvisioner implements CfnResourceProvisioner {
|
||||
if (existing.getEndpointConfiguration() != null) {
|
||||
existing.getEndpointConfiguration().getVpcEndpointIds().forEach(vpcEndpointIds::add);
|
||||
}
|
||||
snapshot.put("body", body);
|
||||
snapshot.put("body", false);
|
||||
r.getAttributes().put(CfnRollback.REST_API_UPDATE_SNAPSHOT_ATTR, snapshot.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Records on an in-place update's snapshot that putRestApi accepted its OpenAPI document, which
|
||||
* replaced the API's resources, methods and models: the snapshot cannot put those back. An API
|
||||
* this provision created has no snapshot.
|
||||
*/
|
||||
private void markDocumentApplied(StackResource r) {
|
||||
if (!r.getAttributes().containsKey(CfnRollback.REST_API_UPDATE_SNAPSHOT_ATTR)) {
|
||||
return;
|
||||
}
|
||||
ObjectNode snapshot = (ObjectNode) readSnapshot(r);
|
||||
snapshot.put("body", true);
|
||||
r.getAttributes().put(CfnRollback.REST_API_UPDATE_SNAPSHOT_ATTR, snapshot.toString());
|
||||
}
|
||||
|
||||
|
||||
+4
-3
@@ -372,9 +372,10 @@ public class CfnResourceDispatcher {
|
||||
}
|
||||
|
||||
/**
|
||||
* Carries ownership discovered by a failed update onto the last known-good resource metadata
|
||||
* that CloudFormation restores. Only additive cleanup tracking belongs here; normal attempted
|
||||
* attributes must not overwrite the committed resource state.
|
||||
* Carries ownership discovered by a failed update, and the snapshot of an undo it could not
|
||||
* complete, onto the last known-good resource metadata that CloudFormation restores. Only
|
||||
* additive tracking belongs here; normal attempted attributes must not overwrite the committed
|
||||
* resource state.
|
||||
*/
|
||||
public void mergeFailedUpdateResourceTracking(StackResource previous, StackResource attempted) {
|
||||
// Any provisioner using ReplacementCleanup: an entity the failed attempt created and could
|
||||
|
||||
+8
-5
@@ -150,12 +150,15 @@ public interface CfnResourceProvisioner {
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* not remove are not orphaned. Only additive tracking belongs here; committed resource state
|
||||
* must not be overwritten. The default has none to carry.
|
||||
* Carries additive tracking a failed update discovered onto the last known-good resource
|
||||
* metadata CloudFormation restores: entities the failed attempt created but could not remove,
|
||||
* so they are not orphaned, and the snapshot of an undo the attempt could not complete, so a
|
||||
* ContinueUpdateRollback can try it again, or report again that it cannot. Only additive
|
||||
* tracking belongs here; committed resource state must not be overwritten. The default has
|
||||
* none to carry.
|
||||
*/
|
||||
default void mergeFailedUpdateResourceTracking(StackResource previous, StackResource attempted) {
|
||||
// no-op by default: only a provisioner that tracks generated sub-resources needs this
|
||||
// no-op by default: only a provisioner with tracking of its own to carry (generated
|
||||
// sub-resources, cleanup entries, an undo snapshot) needs this
|
||||
}
|
||||
}
|
||||
+8
-4
@@ -128,9 +128,11 @@ public final class CfnRollback {
|
||||
|
||||
/**
|
||||
* Holds the name, description and endpoint configuration a REST API had before an in-place
|
||||
* update patched them, and whether the update also re-applied an OpenAPI document, so a failed
|
||||
* stack update can put them back. Written by {@code ApiGatewayRestApiCfnProvisioner} before its
|
||||
* update call and spent by its {@code rollbackUpdate}.
|
||||
* update patched them, so a failed stack update can put them back, and a {@code body} flag set
|
||||
* once the update's OpenAPI document was applied, which the snapshot cannot undo. Written by
|
||||
* {@code ApiGatewayRestApiCfnProvisioner} before its update call and spent by its
|
||||
* {@code rollbackUpdate}; when its own undo did not complete, the provisioner carries it onto
|
||||
* the restored resource through {@code mergeFailedUpdateResourceTracking}.
|
||||
*/
|
||||
public static final String REST_API_UPDATE_SNAPSHOT_ATTR = "__FlociRestApiUpdateSnapshot";
|
||||
|
||||
@@ -324,7 +326,9 @@ public final class CfnRollback {
|
||||
* deleted again by {@code undoCreate}, a reused one is put back by {@code undoInPlace}, which
|
||||
* restores the provisioner's snapshot and is a no-op when there is none. The engine only removes
|
||||
* a failed create it can see, and on a failed update it restores the previous resource metadata
|
||||
* and discards the attempted one, snapshot included, so the undo has to happen here.
|
||||
* and discards the attempted one, snapshot included, so the undo has to happen here. A
|
||||
* provisioner whose snapshot a ContinueUpdateRollback still needs after an undo that failed
|
||||
* carries it in {@link CfnResourceProvisioner#mergeFailedUpdateResourceTracking}.
|
||||
*
|
||||
* <p>A compensation that fails is reported, never hidden. A created entity carries
|
||||
* {@link #ROLLBACK_OWNED_ATTR} while the steps run, so the engine's create rollback deletes it
|
||||
|
||||
Reference in new issue
Block a user