Skip to content
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

[DPE-2613] fix rel tests (6/edge) #263

Merged
merged 8 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion lib/charms/mongodb/v0/mongodb_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 4
LIBPATCH = 5

logger = logging.getLogger(__name__)
REL_NAME = "database"
Expand Down Expand Up @@ -64,6 +64,9 @@ def __init__(self, charm: CharmBase, substrate="k8s", relation_name: str = "data
self.substrate = substrate

super().__init__(charm, self.relation_name)
self.framework.observe(
charm.on[self.relation_name].relation_departed, self._on_relation_departed
)
self.framework.observe(
charm.on[self.relation_name].relation_broken, self._on_relation_event
)
Expand All @@ -79,6 +82,16 @@ def __init__(self, charm: CharmBase, substrate="k8s", relation_name: str = "data
self.database_provides.on.database_requested, self._on_relation_event
)

def _on_relation_departed(self, event):
MiaAltieri marked this conversation as resolved.
Show resolved Hide resolved
"""Checks if users should be removed on the following event (relation-broken)."""
# relation departed and relation broken events occur during scaling down or during relation
# removal, only relation departed events have access to metadata to determine which case.
self.charm.set_scaling_down(event)

# check if were scaling down and add a log message
if self.charm.is_scaling_down(event.relation.id):
logger.info("Scaling down the application, no need to remove external users.")

def _on_relation_event(self, event):
"""Handle relation joined events.

Expand Down Expand Up @@ -110,7 +123,25 @@ def _on_relation_event(self, event):

departed_relation_id = None
if type(event) is RelationBrokenEvent:
# Only relation_deparated events can check if scaling down
departed_relation_id = event.relation.id
if not self.charm.has_departed_run(departed_relation_id):
logger.info(
"Deferring, must wait for relation departed hook to decide if relation should be removed."
)
event.defer()
return

# check if were scaling down and add a log message
if self.charm.is_scaling_down(departed_relation_id):
logger.info(
"Relation broken event occurring due to scale down, do not proceed to remove users."
)
return

logger.info(
"Relation broken event occurring due to relation removal, proceed to remove user."
)

try:
self.oversee_users(departed_relation_id, event)
Expand Down Expand Up @@ -186,6 +217,9 @@ def _diff(self, event: RelationChangedEvent) -> Diff:
a Diff instance containing the added, deleted and changed
keys from the event relation databag.
"""
if not isinstance(event, RelationChangedEvent):
logger.info("Cannot compute diff of event type: %s", type(event))
return
# TODO import marvelous unit tests in a future PR
# Retrieve the old data from the data key in the application relation databag.
old_data = json.loads(event.relation.data[self.charm.model.app].get("data", "{}"))
Expand Down
22 changes: 22 additions & 0 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -1407,6 +1407,28 @@ def _juju_secret_remove(self, scope: Scopes, key: str) -> None:
secret.set_content(secret_cache)
logging.debug(f"Secret {scope}:{key}")

def is_scaling_down(self, rel_id: int) -> bool:
"""Returns True if the application is scaling down."""
rel_departed_key = self._generate_relation_departed_key(rel_id)
return json.loads(self.unit_peer_data[rel_departed_key])

def has_departed_run(self, rel_id: int) -> bool:
"""Returns True if the relation departed event has run."""
rel_departed_key = self._generate_relation_departed_key(rel_id)
return rel_departed_key in self.unit_peer_data

def set_scaling_down(self, event: RelationDepartedEvent) -> None:
"""Sets whether or not the current unit is scaling down."""
# check if relation departed is due to current unit being removed. (i.e. scaling down the
# application.)
rel_departed_key = self._generate_relation_departed_key(event.relation.id)
self.unit_peer_data[rel_departed_key] = json.dumps(event.departing_unit == self.unit)

@staticmethod
def _generate_relation_departed_key(rel_id: int) -> str:
"""Generates the relation departed key for a specified relation id."""
return f"relation_{rel_id}_departed"

# END: helper functions


Expand Down
Loading