-
Notifications
You must be signed in to change notification settings - Fork 2
fix(garm): make teardown lifecycle safe #333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4992b8d
fix(garm): gate teardown before state reconstruction
yanksyoon dd2bc24
refactor(garm): route inherited hooks through teardown gate
yanksyoon 131a9b4
refactor(garm): loop over reconcile observers
yanksyoon a57a084
refactor(garm): isolate teardown dispatch
yanksyoon 79a62c7
refactor(garm): alias inherited reconcile hooks
yanksyoon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -130,49 +130,99 @@ def __init__(self, *args: typing.Any) -> None: | |
| args: Passed through to CharmBase. | ||
| """ | ||
| super().__init__(*args) | ||
| self.framework.observe(self.on.install, self._reconcile) | ||
| self.framework.observe(self.on.leader_elected, self._reconcile) | ||
| self.framework.observe( | ||
| self.on[GARM_CONFIGURATOR_RELATION_NAME].relation_joined, | ||
| self._reconcile, | ||
| ) | ||
| self.framework.observe( | ||
| self.on[GARM_CONFIGURATOR_RELATION_NAME].relation_changed, | ||
| self._reconcile, | ||
| ) | ||
| self.framework.observe( | ||
| self.on[GARM_CONFIGURATOR_RELATION_NAME].relation_departed, | ||
| self._reconcile, | ||
| ) | ||
| self.framework.observe( | ||
| self.on[GARM_CONFIGURATOR_RELATION_NAME].relation_broken, | ||
| self._reconcile, | ||
| ) | ||
| for event in ( | ||
| self.on.install, | ||
| self.on.leader_elected, | ||
| self.on.update_status, | ||
| ): | ||
| self.framework.observe(event, self._reconcile) | ||
|
|
||
| for relation_events in ( | ||
| self.on[GARM_CONFIGURATOR_RELATION_NAME], | ||
| self.on[DEBUG_SSH_INTEGRATION_NAME], | ||
| ): | ||
| for event in ( | ||
| relation_events.relation_joined, | ||
| relation_events.relation_changed, | ||
| relation_events.relation_departed, | ||
| relation_events.relation_broken, | ||
| ): | ||
| self.framework.observe(event, self._reconcile) | ||
|
|
||
| self.framework.observe(self.on.get_credentials_action, self._on_get_credentials_action) | ||
| self.framework.observe( | ||
| self.on[DEBUG_SSH_INTEGRATION_NAME].relation_joined, | ||
| self._reconcile, | ||
| ) | ||
| self.framework.observe( | ||
| self.on[DEBUG_SSH_INTEGRATION_NAME].relation_changed, | ||
| self._reconcile, | ||
| ) | ||
| self.framework.observe( | ||
| self.on[DEBUG_SSH_INTEGRATION_NAME].relation_departed, | ||
| self._reconcile, | ||
| ) | ||
| self.framework.observe( | ||
| self.on[DEBUG_SSH_INTEGRATION_NAME].relation_broken, | ||
| self._reconcile, | ||
| ) | ||
| self.framework.observe(self.on.update_status, self._reconcile) | ||
| self.framework.observe(self.on.remove, self._on_remove) | ||
|
|
||
| def _is_tearing_down(self) -> bool: | ||
| """Return whether Juju plans no remaining units for the local application.""" | ||
| return self.app.planned_units() == 0 | ||
|
|
||
| def _reconcile(self, event: ops.EventBase) -> None: | ||
| """Reconcile GARM, or handle local teardown before normal state construction.""" | ||
| if self._is_tearing_down(): | ||
| self._teardown(event) | ||
| return | ||
| self._normal_reconcile(event) | ||
|
|
||
| def _reconcile_with_migrations(self, event: ops.EventBase) -> None: | ||
| """Reconcile a database event, preserving the teardown gate.""" | ||
| if self._is_tearing_down(): | ||
| self._teardown(event) | ||
| return | ||
| self._normal_reconcile_with_migrations(event) | ||
|
|
||
| def _teardown(self, event: ops.EventBase) -> None: | ||
| """Handle a local teardown event without normal reconciliation.""" | ||
| logger.info( | ||
| "Skipping normal GARM reconciliation for %s during local teardown", | ||
| event.handle.kind, | ||
| ) | ||
|
|
||
| @block_if_invalid_data | ||
| def _reconcile(self, _: ops.EventBase) -> None: | ||
| """Reconcile charm state.""" | ||
| def _normal_reconcile(self, _: ops.EventBase) -> None: | ||
| """Reconcile active GARM charm state.""" | ||
| self.restart() | ||
|
|
||
| @block_if_invalid_data | ||
| def _normal_reconcile_with_migrations(self, _: ops.EventBase) -> None: | ||
| """Reconcile active GARM state and rerun database migrations.""" | ||
| self.restart(rerun_migrations=True) | ||
|
|
||
| def _route_reconcile(self, event: ops.EventBase) -> None: | ||
| """Route an inherited framework event through GARM's teardown gate.""" | ||
| self._reconcile(event) | ||
|
|
||
| # PaasCharm.__init__ resolves these hook names dynamically. Aliasing them keeps the | ||
| # teardown check before the inherited decorators without registering duplicate observers. | ||
| _on_config_changed = _route_reconcile | ||
| _on_secret_changed = _route_reconcile | ||
| _on_secret_storage_relation_changed = _route_reconcile | ||
| _on_secret_storage_relation_departed = _route_reconcile | ||
| _on_postgresql_database_relation_broken = _route_reconcile | ||
| _on_ingress_ready = _route_reconcile | ||
| _on_ingress_revoked = _route_reconcile | ||
| _on_pebble_ready = _route_reconcile | ||
|
|
||
| def _route_reconcile_with_migrations(self, event: ops.EventBase) -> None: | ||
| """Route an inherited database event through GARM's migration gate.""" | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is strictly for the database relations |
||
| self._reconcile_with_migrations(event) | ||
|
|
||
| _on_postgresql_database_database_created = _route_reconcile_with_migrations | ||
| _on_postgresql_database_endpoints_changed = _route_reconcile_with_migrations | ||
|
|
||
| def _on_update_status(self, event: ops.HookEvent) -> None: | ||
| """Run the framework update-status handler only while active.""" | ||
| if self._is_tearing_down(): | ||
| logger.info("Skipping update-status handling during local teardown") | ||
| return | ||
| super()._on_update_status(event) | ||
|
|
||
| def _on_rotate_secret_key_action(self, event: ops.ActionEvent) -> None: | ||
| """Reject secret rotation during teardown before the base decorator runs.""" | ||
| if self._is_tearing_down(): | ||
| event.fail("cannot rotate the secret key during local teardown") | ||
| return | ||
| super()._on_rotate_secret_key_action(event) | ||
|
|
||
| def _on_remove(self, _: ops.RemoveEvent) -> None: | ||
| """Drain GARM resources before Juju removes the application.""" | ||
| if not self.unit.is_leader(): | ||
|
|
@@ -287,6 +337,10 @@ def restart(self, rerun_migrations: bool = False) -> None: | |
| Args: | ||
| rerun_migrations: Passed through to the parent restart. | ||
| """ | ||
| if self._is_tearing_down(): | ||
| logger.info("Skipping GARM workload restart during local teardown") | ||
| return | ||
|
|
||
| self._ensure_secrets() | ||
|
|
||
| if not self.is_ready(): | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ref 1.
12-factor reference comment for override