Skip to content

Commit

Permalink
Make nodeport patching routine more resilient
Browse files Browse the repository at this point in the history
  • Loading branch information
phvalguima committed Apr 18, 2024
1 parent f5b20ae commit 1b50c01
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/relations/pgbouncer_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,10 @@ def external_connectivity(self, event=None) -> bool:
# Disregard of what has been requested by the client, this relation is being removed
relations.remove(event.relation)

# Now, we will return true if any of the relations are marked as external OR if the event
# itself is requesting for an external_node_connectivity.
# Not all events received have external-node-connectivity
external_conn = getattr(event, "external_node_connectivity", lambda: False)()
return (event and external_conn) or any(
relation.data[relation.app].get("external-node-connectivity", "false") == "true"
# Now, we will return true if any of the relations are marked as external
return any(
relation.data.get(relation.app, {}).get("external-node-connectivity", "false")
== "true"
for relation in relations
)

Expand Down Expand Up @@ -164,6 +162,8 @@ def _on_database_requested(self, event: DatabaseRequestedEvent) -> None:
self.database_provides.set_credentials(rel_id, user, password)
# Set the database name
self.database_provides.set_database(rel_id, database)

self.charm.patch_port(self.external_connectivity(event))
self.update_connection_info(event.relation)

def _on_relation_departed(self, event: RelationDepartedEvent) -> None:
Expand Down Expand Up @@ -214,10 +214,6 @@ def update_connection_info(self, relation):
if not self.charm.unit.is_leader():
return

if relation.data[relation.app].get("external-node-connectivity", "false") == "true":
# Make sure we have a node port exposed
self.charm.patch_port(True)

self.update_endpoints(relation)

# Set the database version.
Expand All @@ -239,6 +235,10 @@ def update_endpoints(self, relation=None) -> None:

# Set the endpoints for each relation.
for relation in relations:
if not relation or not relation.data or not relation.data.get(relation.app):
# This is a relation that is going away and finds itself in a broken state
# proceed to the next relation
continue
# Read-write endpoint
if relation.data[relation.app].get("external-node-connectivity", "false") == "true":
self.database_provides.set_endpoints(relation.id, nodeports["rw"])
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/relations/test_pgbouncer_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def setUp(self):
self.client_rel_id = self.harness.add_relation(CLIENT_RELATION_NAME, "application")
self.harness.add_relation_unit(self.client_rel_id, "application/0")

@patch("charm.lightkube")
@patch("relations.backend_database.BackendDatabaseRequires.check_backend", return_value=True)
@patch(
"relations.backend_database.BackendDatabaseRequires.postgres", new_callable=PropertyMock
Expand Down Expand Up @@ -78,6 +79,7 @@ def test_on_database_requested(
_pg_databag,
_pg,
_check_backend,
_,
):
self.harness.set_leader()
_gen_rel_dbs.return_value = {}
Expand Down

0 comments on commit 1b50c01

Please sign in to comment.