|
4 | 4 | """Postgres client relation hooks & helpers."""
|
5 | 5 |
|
6 | 6 | import logging
|
| 7 | +from datetime import datetime |
7 | 8 |
|
8 | 9 | from charms.data_platform_libs.v0.data_interfaces import (
|
9 | 10 | DatabaseProvides,
|
|
21 | 22 | from ops.charm import CharmBase, RelationBrokenEvent, RelationChangedEvent, RelationDepartedEvent
|
22 | 23 | from ops.framework import Object
|
23 | 24 | from ops.model import ActiveStatus, BlockedStatus, Relation
|
| 25 | +from tenacity import RetryError, Retrying, stop_after_attempt, wait_fixed |
24 | 26 |
|
25 | 27 | from constants import (
|
26 | 28 | DATABASE_PORT,
|
@@ -66,9 +68,6 @@ def __init__(self, charm: CharmBase, relation_name: str = "database") -> None:
|
66 | 68 | self.framework.observe(
|
67 | 69 | self.database_provides.on.database_requested, self._on_database_requested
|
68 | 70 | )
|
69 |
| - self.framework.observe( |
70 |
| - charm.on[self.relation_name].relation_changed, self._on_relation_changed |
71 |
| - ) |
72 | 71 |
|
73 | 72 | @staticmethod
|
74 | 73 | def _sanitize_extra_roles(extra_roles: str | None) -> list[str]:
|
@@ -163,26 +162,19 @@ def _on_database_requested(self, event: DatabaseRequestedEvent) -> None:
|
163 | 162 | if issubclass(type(e), PostgreSQLCreateUserError) and e.message is not None
|
164 | 163 | else f"Failed to initialize {self.relation_name} relation"
|
165 | 164 | )
|
166 |
| - |
167 |
| - def _on_relation_changed(self, event: RelationChangedEvent) -> None: |
168 |
| - # Check for some conditions before trying to access the PostgreSQL instance. |
169 |
| - if not self.charm.is_cluster_initialised: |
170 |
| - logger.debug( |
171 |
| - "Deferring on_relation_changed: Cluster must be initialized before configuration can be updated with relation users" |
172 |
| - ) |
173 |
| - event.defer() |
174 | 165 | return
|
175 | 166 |
|
176 |
| - if ( |
177 |
| - not self.charm._patroni.member_started |
178 |
| - or f"relation_id_{event.relation.id}" |
179 |
| - not in self.charm.postgresql.list_users(current_host=True) |
180 |
| - ): |
181 |
| - logger.debug("Deferring on_relation_changed: user was not created yet") |
182 |
| - event.defer() |
183 |
| - return |
184 |
| - |
185 |
| - self.charm.update_config() |
| 167 | + # Try to wait for pg_hba trigger |
| 168 | + try: |
| 169 | + for attempt in Retrying(stop=stop_after_attempt(3), wait=wait_fixed(1)): |
| 170 | + with attempt: |
| 171 | + if not self.charm.postgresql.is_user_in_hba(user): |
| 172 | + raise Exception("pg_hba not ready") |
| 173 | + self.charm.unit_peer_data.update({ |
| 174 | + "pg_hba_needs_update_timestamp": str(datetime.now()) |
| 175 | + }) |
| 176 | + except RetryError: |
| 177 | + logger.warning("database requested: Unable to check pg_hba rule update") |
186 | 178 |
|
187 | 179 | def _on_relation_departed(self, event: RelationDepartedEvent) -> None:
|
188 | 180 | """Set a flag to avoid deleting database users when not wanted."""
|
|
0 commit comments