Skip to content

Commit

Permalink
No deferring on missing tls
Browse files Browse the repository at this point in the history
  • Loading branch information
dragomirp committed Feb 7, 2024
1 parent 13760fc commit a24fd1a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 37 deletions.
24 changes: 8 additions & 16 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,16 +270,7 @@ def _on_pgbouncer_pebble_ready(self, event: PebbleReadyEvent) -> None:
event.defer()
return

tls_enabled = all(self.tls.get_tls_files())
if self.model.relations.get("certificates", []) and not tls_enabled:
logger.debug(
"pgbouncer_pebble_ready: Deferring as certificates files are not yet populated for existing certificates relation"
)
self.unit.status = WaitingStatus("Waiting for certificates")
event.defer()
return
# in case of pod restart
elif tls_enabled:
if all(self.tls.get_tls_files()):
self.push_tls_files_to_workload(False)

pebble_layer = self._pgbouncer_layer()
Expand All @@ -290,13 +281,14 @@ def _on_pgbouncer_pebble_ready(self, event: PebbleReadyEvent) -> None:

self.unit.set_workload_version(self.version)

# Update postgres endpoints in config to match the current state of the charm.
self.update_postgres_endpoints(reload_pgbouncer=True)
if self.unit.is_leader():
# Update postgres endpoints in config to match the current state of the charm.
self.update_postgres_endpoints(reload_pgbouncer=True)

if JujuVersion.from_environ().supports_open_port_on_k8s:
self.unit.open_port("tcp", self.config["listen_port"])
else:
self._patch_port()
if JujuVersion.from_environ().supports_open_port_on_k8s:
self.unit.open_port("tcp", self.config["listen_port"])
else:
self._patch_port()

def _on_config_changed(self, event: ConfigChangedEvent) -> None:
"""Handle changes in configuration.
Expand Down
21 changes: 0 additions & 21 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,27 +130,6 @@ def test_on_pgbouncer_pebble_ready(self, _mkdir, _exec, _update_status, _):
self.assertTrue(container_service.is_running())
_update_status.assert_called_once_with()

@patch("charm.PostgreSQLTLS.get_tls_files")
@patch("ops.model.Container.make_dir")
def test_on_pgbouncer_pebble_ready_defer_tls(self, _mkdir, get_tls_files):
get_tls_files.return_value = (None, None, None)

self.harness.add_relation(BACKEND_RELATION_NAME, "postgres")
self.harness.add_relation("certificates", "tls_op")
self.harness.set_leader(True)
# emit on start to ensure config file render
self.charm.on.start.emit()
initial_plan = self.harness.get_container_pebble_plan(PGB)
self.assertEqual(initial_plan.to_yaml(), "{}\n")

container = self.harness.model.unit.get_container(PGB)
self.charm.on.pgbouncer_pebble_ready.emit(container)

assert not len(self.harness.model.unit.get_container(PGB).get_services())
get_tls_files.assert_called_once_with()
self.assertIsInstance(self.harness.model.unit.status, WaitingStatus)
self.assertEqual(self.harness.model.unit.status.message, "Waiting for certificates")

@patch("charm.PgBouncerK8sCharm._patch_port")
@patch("charm.PgBouncerK8sCharm.update_status")
@patch("ops.model.Container.exec")
Expand Down

0 comments on commit a24fd1a

Please sign in to comment.