Skip to content

Commit

Permalink
Defer if no monitoring password
Browse files Browse the repository at this point in the history
  • Loading branch information
dragomirp committed Sep 11, 2023
1 parent 33bddb4 commit 392592f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,10 @@ def _on_start(self, event: StartEvent) -> None:

try:
# Set up the postgresql_exporter options.
self._setup_exporter()
if not self._setup_exporter():
logger.info("Monitoring password not ready")
event.defer()
return
except snap.SnapError:
logger.exception("failed to set up postgresql_exporter options")
self.unit.status = BlockedStatus("failed to set up postgresql_exporter options")
Expand All @@ -970,21 +973,25 @@ def _on_start(self, event: StartEvent) -> None:
# Bootstrap the cluster in the leader unit.
self._start_primary(event)

def _setup_exporter(self) -> None:
def _setup_exporter(self) -> bool:
"""Set up postgresql_exporter options."""
cache = snap.SnapCache()
postgres_snap = cache[POSTGRESQL_SNAP_NAME]
password = self.get_secret(APP_SCOPE, MONITORING_PASSWORD_KEY)
if not password:
return False

postgres_snap.set(
{
"exporter.user": MONITORING_USER,
"exporter.password": self.get_secret(APP_SCOPE, MONITORING_PASSWORD_KEY),
"exporter.password": password,
}
)
if postgres_snap.services[MONITORING_SNAP_SERVICE]["active"] is False:
postgres_snap.start(services=[MONITORING_SNAP_SERVICE], enable=True)
else:
postgres_snap.restart(services=[MONITORING_SNAP_SERVICE])
return True

def _start_primary(self, event: StartEvent) -> None:
"""Bootstrap the cluster."""
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ def test_on_start(
_enable_disable_extensions.assert_called_once()
self.assertTrue(isinstance(self.harness.model.unit.status, ActiveStatus))

@patch("charm.PostgresqlOperatorCharm.get_secret")
@patch("charm.snap.SnapCache")
@patch("charm.Patroni.get_postgresql_version")
@patch_network_get(private_address="1.1.1.1")
Expand Down Expand Up @@ -358,6 +359,7 @@ def test_on_start_replica(
_configure_patroni_on_unit,
_get_postgresql_version,
_snap_cache,
_get_secret
):
_get_postgresql_version.return_value = "14.0"

Expand All @@ -367,6 +369,7 @@ def test_on_start_replica(
# Mock the passwords.
_get_password.return_value = "fake-operator-password"
_replication_password.return_value = "fake-replication-password"
_get_secret.return_value = "fake-monitoring-passwordl"

# Test an uninitialized cluster.
self.charm._peers.data[self.charm.app].update({"cluster_initialised": ""})
Expand Down

0 comments on commit 392592f

Please sign in to comment.