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

[MISC] Bugfixes #252

Merged
merged 5 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 10 additions & 10 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,9 @@ def _on_update_status(self, _) -> None:

def update_status(self):
"""Health check to update pgbouncer status based on charm state."""
if self.unit.status.message == EXTENSIONS_BLOCKING_MESSAGE:
return

if self.backend.postgres is None:
self.unit.status = BlockedStatus("waiting for backend database relation to initialise")
return
Expand All @@ -382,9 +385,6 @@ def update_status(self):
self.unit.status = BlockedStatus("backend database relation not ready")
return

if self.unit.status.message == EXTENSIONS_BLOCKING_MESSAGE:
return

try:
if self.check_pgb_running():
self.unit.status = ActiveStatus()
Expand Down Expand Up @@ -457,7 +457,8 @@ def check_pgb_running(self):
"""Checks that pgbouncer pebble service is running, and updates status accordingly."""
pgb_container = self.unit.get_container(PGB)
if not pgb_container.can_connect():
self.unit.status = WaitingStatus(CONTAINER_UNAVAILABLE_MESSAGE)
if self.unit.status.message != EXTENSIONS_BLOCKING_MESSAGE:
self.unit.status = WaitingStatus(CONTAINER_UNAVAILABLE_MESSAGE)
logger.warning(CONTAINER_UNAVAILABLE_MESSAGE)
return False

Expand All @@ -474,7 +475,8 @@ def check_pgb_running(self):
pgb_service_status = pgb_container.get_services().get(service).current
if pgb_service_status != ServiceStatus.ACTIVE:
pgb_not_running = f"PgBouncer service {service} not running: service status = {pgb_service_status}"
self.unit.status = BlockedStatus(pgb_not_running)
if self.unit.status.message != EXTENSIONS_BLOCKING_MESSAGE:
self.unit.status = BlockedStatus(pgb_not_running)
logger.warning(pgb_not_running)
return False

Expand Down Expand Up @@ -644,22 +646,20 @@ def generate_relation_databases(self) -> Dict[str, Dict[str, Union[str, bool]]]:
"""Generates a mapping between relation and database and sets it in the app databag."""
if not self.unit.is_leader():
return {}
if dbs := self.get_relation_databases():
return dbs
Comment on lines -647 to -648
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regenerating should be fast enough and if the mapping is wrong for whatever reason, always using the cache will never fix it.


databases = {}
for relation in self.model.relations.get("db", []):
database = self.legacy_db_relation.get_databags(relation)[0].get("database")
if database:
databases[relation.id] = {
databases[str(relation.id)] = {
"name": database,
"legacy": True,
}

for relation in self.model.relations.get("db-admin", []):
database = self.legacy_db_admin_relation.get_databags(relation)[0].get("database")
if database:
databases[relation.id] = {
databases[str(relation.id)] = {
"name": database,
"legacy": True,
}
Expand All @@ -669,7 +669,7 @@ def generate_relation_databases(self) -> Dict[str, Dict[str, Union[str, bool]]]:
).items():
database = data.get("database")
if database:
databases[rel_id] = {
databases[str(rel_id)] = {
"name": database,
"legacy": False,
}
Expand Down
11 changes: 5 additions & 6 deletions src/relations/backend_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
from ops.charm import CharmBase, RelationBrokenEvent, RelationDepartedEvent
from ops.framework import Object
from ops.model import (
ActiveStatus,
Application,
BlockedStatus,
MaintenanceStatus,
Expand Down Expand Up @@ -232,7 +231,7 @@ def _on_database_created(self, event: DatabaseCreatedEvent) -> None:
self.charm.render_auth_file(auth_file)
self.charm.render_pgb_config(reload_pgbouncer=True)
self.charm.toggle_monitoring_layer(True)
self.charm.unit.status = ActiveStatus()
self.charm.update_status()
return

logger.info("initialising pgbouncer backend relation")
Expand Down Expand Up @@ -274,7 +273,7 @@ def _on_database_created(self, event: DatabaseCreatedEvent) -> None:
self.charm.render_pgb_config(reload_pgbouncer=True)
self.charm.toggle_monitoring_layer(True)

self.charm.unit.status = ActiveStatus("backend-database relation initialised.")
self.charm.update_status()

def _on_endpoints_changed(self, _):
self.charm.render_pgb_config(reload_pgbouncer=True)
Expand All @@ -300,7 +299,8 @@ def _on_relation_departed(self, event: RelationDepartedEvent):
the postgres relation-broken hook removes the user needed to remove authentication for the
users we create.
"""
self.charm.render_pgb_config(reload_pgbouncer=True)
if self.charm.peers.relation:
self.charm.render_pgb_config(reload_pgbouncer=True)
self.charm.update_client_connection_info()

if event.departing_unit == self.charm.unit:
Expand Down Expand Up @@ -335,7 +335,6 @@ def _on_relation_departed(self, event: RelationDepartedEvent):
return

self.postgres.delete_user(self.auth_user)
self.charm.peers.remove_user(self.auth_user)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't exist any more

logger.info("pgbouncer auth user removed")

def _on_relation_broken(self, event: RelationBrokenEvent):
Expand All @@ -344,11 +343,11 @@ def _on_relation_broken(self, event: RelationBrokenEvent):
Removes all traces of this relation from pgbouncer config.
"""
depart_flag = f"{BACKEND_RELATION_NAME}_{event.relation.id}_departing"
self.charm.toggle_monitoring_layer(False)
if self.charm.peers.unit_databag.get(depart_flag, False):
logging.info("exiting relation-broken hook - nothing to do")
return

self.charm.toggle_monitoring_layer(False)
try:
self.charm.delete_file(f"{PGB_DIR}/userlist.txt")
except PathError:
Expand Down
9 changes: 7 additions & 2 deletions src/relations/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def _on_relation_joined(self, join_event: RelationJoinedEvent):
return

dbs = self.charm.generate_relation_databases()
dbs[join_event.relation.id] = {"name": database, "legacy": True}
dbs[str(join_event.relation.id)] = {"name": database, "legacy": True}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JSON keys are string

self.charm.set_relation_databases(dbs)

self.update_databags(
Expand All @@ -252,14 +252,16 @@ def _on_relation_joined(self, join_event: RelationJoinedEvent):
# Create user and database in backend postgresql database
try:
init_msg = f"initialising database and user for {self.relation_name} relation"
initial_status = self.charm.unit.status
self.charm.unit.status = MaintenanceStatus(init_msg)
logger.info(init_msg)

self.charm.backend.postgres.create_user(user, password, admin=self.admin)
self.charm.backend.postgres.create_database(database, user)

created_msg = f"database and user for {self.relation_name} relation created"
self.charm.unit.status = ActiveStatus()
self.charm.unit.status = initial_status
self.charm.update_status()
logger.info(created_msg)
except (PostgreSQLCreateDatabaseError, PostgreSQLCreateUserError):
err_msg = f"failed to create database or user for {self.relation_name}"
Expand Down Expand Up @@ -323,6 +325,9 @@ def _on_relation_changed(self, change_event: RelationChangedEvent):

def update_connection_info(self, relation: Relation, port: str):
"""Updates databag connection information."""
if not port:
port = self.charm.config["listen_port"]

databag = self.get_databags(relation)[0]
database = databag.get("database")
user = databag.get("user")
Expand Down
7 changes: 4 additions & 3 deletions src/relations/pgbouncer_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
from ops.charm import CharmBase, RelationBrokenEvent, RelationDepartedEvent
from ops.framework import Object
from ops.model import (
ActiveStatus,
Application,
BlockedStatus,
MaintenanceStatus,
Expand Down Expand Up @@ -138,7 +137,7 @@ def _on_database_requested(self, event: DatabaseRequestedEvent) -> None:
return

dbs = self.charm.generate_relation_databases()
dbs[event.relation.id] = {"name": database, "legacy": False}
dbs[str(event.relation.id)] = {"name": database, "legacy": False}
self.charm.set_relation_databases(dbs)

# Share the credentials and updated connection info with the client application.
Expand Down Expand Up @@ -193,6 +192,7 @@ def update_connection_info(self, relation):
# Set the read/write endpoint.
if not self.charm.unit.is_leader():
return
initial_status = self.charm.unit.status
self.charm.unit.status = MaintenanceStatus(
f"Updating {self.relation_name} relation connection information"
)
Expand All @@ -206,8 +206,9 @@ def update_connection_info(self, relation):
self.database_provides.set_version(
relation.id, self.charm.backend.postgres.get_postgresql_version()
)
self.charm.unit.status = initial_status

self.charm.unit.status = ActiveStatus()
self.charm.update_status()

def update_read_only_endpoints(self, event: DatabaseRequestedEvent = None) -> None:
"""Set the read-only endpoint only if there are replicas."""
Expand Down
13 changes: 0 additions & 13 deletions tests/integration/relations/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

FINOS_WALTZ = "finos-waltz"
ANOTHER_FINOS_WALTZ = "another-finos-waltz"
OPENLDAP = "openldap"

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -198,15 +197,3 @@ async def test_extensions_blocking(ops_test: OpsTest) -> None:
raise_on_blocked=False,
timeout=3000,
)


@pytest.mark.group(1)
@pytest.mark.unstable
async def test_relation_with_openldap(ops_test: OpsTest):
Comment on lines -203 to -205
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The image for the charm cannot be downloaded.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which image? I deployed OpenLDAP through juju deploy openldap-charmers-openldap --channel edge and got it in the Waiting for database relation state.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got errors pulling the image for the same charm last week. Will recheck.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking deeper, it looks like this is the podspec charm and the pull failure seems to originate from the image pulled by the podspec.

"""Test the relation with OpenLDAP charm."""
await ops_test.model.deploy(
"openldap-charmers-openldap", application_name=OPENLDAP, channel="edge"
)
await ops_test.model.add_relation(f"{PGB}:db", f"{OPENLDAP}:db")
wait_for_relation_joined_between(ops_test, PGB, OPENLDAP)
await ops_test.model.wait_for_idle(apps=[PG, PGB, OPENLDAP], status="active", timeout=1000)
4 changes: 2 additions & 2 deletions tests/unit/relations/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def test_on_relation_joined(

_set_rel_dbs.reset_mock()
self.db_admin_relation._on_relation_joined(mock_event)
_set_rel_dbs.assert_called_once_with({1: {"name": "test_db", "legacy": True}})
_set_rel_dbs.assert_called_once_with({"1": {"name": "test_db", "legacy": True}})

_create_user.assert_called_with(user, password, admin=True)
_create_database.assert_called_with(database, user)
Expand All @@ -121,7 +121,7 @@ def test_on_relation_joined(
_set_rel_dbs.reset_mock()
self.db_relation._on_relation_joined(mock_event)
_create_user.assert_called_with(user, password, admin=False)
_set_rel_dbs.assert_called_once_with({1: {"name": "test_db", "legacy": True}})
_set_rel_dbs.assert_called_once_with({"1": {"name": "test_db", "legacy": True}})

@patch("relations.backend_database.BackendDatabaseRequires.check_backend", return_value=True)
@patch(
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/relations/test_pgbouncer_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def test_on_database_requested(
rel_id, f"{self.charm.leader_hostname}:{self.charm.config['listen_port']}"
)
_update_read_only_endpoints.assert_called()
_set_rel_dbs.assert_called_once_with({1: {"name": "test-db", "legacy": False}})
_set_rel_dbs.assert_called_once_with({"1": {"name": "test-db", "legacy": False}})

@patch("relations.backend_database.BackendDatabaseRequires.check_backend", return_value=True)
@patch(
Expand Down
Loading