Skip to content

Commit

Permalink
[DPE-3535] Rerender config from secrets during upgrade (#150)
Browse files Browse the repository at this point in the history
* Rerender config from secrets during upgrade

* Fix charm bootup
  • Loading branch information
dragomirp authored Feb 12, 2024
1 parent a1193fb commit 742a844
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,14 @@ def __init__(self, *args):
# Charm Lifecycle Hooks
# =======================

def render_utility_files(self):
def render_utility_files(self, cfg_file=None):
"""Render charm utility services and configuration."""
# Initialise pgbouncer.ini config files from defaults set in charm lib and current config.
# We'll add basic configs for now even if this unit isn't a leader, so systemd doesn't
# throw a fit.
cfg = pgb.PgbConfig(pgb.DEFAULT_CONFIG)
if not cfg_file:
cfg_file = pgb.DEFAULT_CONFIG
cfg = pgb.PgbConfig(cfg_file)
cfg["pgbouncer"]["listen_addr"] = "127.0.0.1"
cfg["pgbouncer"]["user"] = "snap_daemon"
self.render_pgb_config(cfg)
Expand Down
6 changes: 4 additions & 2 deletions src/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
from tenacity import Retrying, stop_after_attempt, wait_fixed
from typing_extensions import override

from constants import SNAP_PACKAGES
from constants import APP_SCOPE, SNAP_PACKAGES
from relations.peers import CFG_FILE_DATABAG_KEY

DEFAULT_MESSAGE = "Pre-upgrade check failed and cannot safely upgrade"

Expand Down Expand Up @@ -83,7 +84,8 @@ def _on_upgrade_granted(self, event: UpgradeGrantedEvent) -> None:
self.charm._install_snap_packages(packages=SNAP_PACKAGES, refresh=True)

self.charm.unit.status = MaintenanceStatus("restarting services")
self.charm.render_utility_files()
cfg = self.charm.get_secret(APP_SCOPE, CFG_FILE_DATABAG_KEY)
self.charm.render_utility_files(cfg)
self.charm.reload_pgbouncer()
if self.charm.backend.postgres:
self.charm.render_prometheus_service()
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/test_upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def test_log_rollback_instructions(self, _logger: Mock):
"Run `juju refresh --revision <previous-revision> pgbouncer` to initiate the rollback"
)

@patch("charm.PgBouncerCharm.get_secret")
@patch("charm.BackendDatabaseRequires.postgres", return_value=True, new_callable=PropertyMock)
@patch("charm.PgbouncerUpgrade.on_upgrade_changed")
@patch("charm.PgbouncerUpgrade.set_unit_completed")
Expand All @@ -62,6 +63,7 @@ def test_on_upgrade_granted(
_set_unit_completed: Mock,
_on_upgrade_changed: Mock,
_,
__,
):
event = Mock()

Expand All @@ -84,6 +86,7 @@ def test_on_upgrade_granted(

_on_upgrade_changed.assert_called_once_with(event)

@patch("charm.PgBouncerCharm.get_secret")
@patch("upgrade.wait_fixed", return_value=tenacity.wait_fixed(0))
@patch("charm.BackendDatabaseRequires.postgres", return_value=True, new_callable=PropertyMock)
@patch("charm.PgbouncerUpgrade.on_upgrade_changed")
Expand All @@ -108,6 +111,7 @@ def test_on_upgrade_granted_error(
_on_upgrade_changed: Mock,
_,
__,
___,
):
_cluster_checks.side_effect = ClusterNotReadyError("test", "test")

Expand Down

0 comments on commit 742a844

Please sign in to comment.