Improve the cleanup code on enrollment handling error #890
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What is the problem this PR solves?
The cleanup code for enrollment errors is scattered in couple of places:
This change consolidates the cleanup code through the rollback/cleanup wrapper.
How does this PR solve the problem?
Implemented a simple "rollback" wrapper that can have the "rollback/cleanup" cleanup functions added as needed. The
Rollbackis called on error which in turn calls the registered cleanup functions.For example, the log when Fleet Server fails to create the agent record:
So, now:
api key invalidationfunction is registered with the Rollbackdelete agentfunction is registered with the RollbackhandleEnrollcallsRollbackif there was an error during enrollment. the Rollback calls all the registered cleanup function in order to remove/invalidate the data that was created.Checklist
CHANGELOG.next.asciidocorCHANGELOG-developer.next.asciidoc.