Skip to content

Commit

Permalink
Avoid early finalising of the new interface
Browse files Browse the repository at this point in the history
  • Loading branch information
dragomirp committed Jan 25, 2024
1 parent f0f945b commit b248aa9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
23 changes: 13 additions & 10 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,34 +763,37 @@ def _get_relation_databases(self) -> List[Dict[str, str]]:
database = self.client_relation.get_database(relation)
if database:
# TODO new rel admins
databases.append(
{
"name": database,
"legacy": False,
}
)
db_list = database.split(",")
for db in db_list:
databases.append(
{
"name": db,
"legacy": False,
}
)
return databases

def _get_relation_config(self) -> Tuple[Dict[str, Dict[str, str]], List[str]]:
"""Generate pgb config for databases and admin users."""
if not self.backend.relation or not (databases := self._get_relation_databases()):
return ({}, set())
return ({}, [])

# In postgres, "endpoints" will only ever have one value. Other databases using the library
# can have more, but that's not planned for the postgres charm.
if not (postgres_endpoint := self.backend.postgres_databag.get("endpoints")):
return ({}, set())
return ({}, [])
host, port = postgres_endpoint.split(":")

read_only_endpoints = self.backend.get_read_only_endpoints()
r_hosts = ",".join([host.split(":")[0] for host in read_only_endpoints])
if r_hosts:
for host in read_only_endpoints:
for r_host in read_only_endpoints:
r_port = host.split(":")[1]
break

pgb_dbs = {}
pgb_admins = []

for database in databases:
name = database["name"]
pgb_dbs[name] = {
Expand Down Expand Up @@ -884,7 +887,7 @@ def update_client_connection_info(self, port: Optional[str] = None):
TODO rename
"""
# Skip updates if backend.postgres doesn't exist yet.
if not self.backend.postgres:
if not self.backend.postgres or not self.unit.is_leader():
return

if not port:
Expand Down
5 changes: 1 addition & 4 deletions src/relations/peers.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,20 +153,17 @@ def _on_changed(self, event: HookEvent):
- If pgbouncer container is unavailable.
"""
self.unit_databag.update({ADDRESS_KEY: self.charm.unit_pod_hostname})
if self.charm.unit.is_leader():
self.charm.update_client_connection_info()
self.charm.render_pgb_config()

if self.charm.unit.is_leader():
self.update_leader()
return

if auth_file := self.charm.get_secret(APP_SCOPE, AUTH_FILE_DATABAG_KEY):
self.charm.render_auth_file(auth_file)

try:
# raises an error if this is fired before on_pebble_ready.
self.charm.reload_pgbouncer()
self.charm.render_pgb_config(reload_pgbouncer=True)
self.charm.toggle_monitoring_layer(self.charm.backend.ready)
except (ConnectionError, ChangeError):
logger.error(
Expand Down
1 change: 0 additions & 1 deletion src/relations/pgbouncer_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ def _on_database_requested(self, event: DatabaseRequestedEvent) -> None:

self.charm.peers.add_user(user, password)

# Update pgbouncer config
self.charm.render_pgb_config(reload_pgbouncer=True)

# Share the credentials and updated connection info with the client application.
Expand Down

0 comments on commit b248aa9

Please sign in to comment.