Skip to content

Commit

Permalink
Uses the agent ID instead of the unit ID when setting the cluster bind
Browse files Browse the repository at this point in the history
addresses in the application data bag.

This is what is ultimately written to file and used by the agent, which
does knows its own ID, but not necessarily the unit's.
  • Loading branch information
manadart committed Mar 19, 2024
1 parent efe1af3 commit ede3f03
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,13 @@ def _on_dbcluster_relation_changed(self, event):
# The event only has *other* units so include this
# unit's bind address if we have managed to set it.
ip = self._stored.db_bind_address
all_bind_addresses = {self.unit.name: ip} if ip else dict()
all_bind_addresses = {self._controller_agent_id(): ip} if ip else dict()

for unit in relation.units:
unit_data = relation.data[unit]
if self.DB_BIND_ADDR_KEY in unit_data:
all_bind_addresses[unit.name] = unit_data[self.DB_BIND_ADDR_KEY]
agent_id = unit_data[self.AGENT_ID_KEY]
all_bind_addresses[agent_id] = unit_data[self.DB_BIND_ADDR_KEY]

if self._stored.all_bind_addresses == all_bind_addresses:
return
Expand Down
9 changes: 7 additions & 2 deletions tests/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,21 @@ def test_dbcluster_relation_changed_single_addr(
harness = self.harness
mock_get_binding.return_value = mockBinding(['192.168.1.17'])

# This unit's agent ID happends to correspond with the unit ID.
mock_get_agent_id.return_value = '0'

harness.set_leader()

# Have another unit enter the relation.
# Its bind address should end up in the application data bindings list.
# Note that the agent ID doesn not correspond with the unit's ID
relation_id = harness.add_relation('dbcluster', harness.charm.app)
harness.add_relation_unit(relation_id, 'juju-controller/1')
self.harness.update_relation_data(
relation_id, 'juju-controller/1', {'db-bind-address': '192.168.1.100'})
relation_id, 'juju-controller/1', {
'db-bind-address': '192.168.1.100',
'agent-id': '9',
})

mock_reload_config.assert_called_once()

Expand All @@ -176,7 +181,7 @@ def test_dbcluster_relation_changed_single_addr(
self.assertEqual(unit_data['agent-id'], '0')

app_data = harness.get_relation_data(relation_id, 'juju-controller')
exp = {"juju-controller/0": "192.168.1.17", "juju-controller/1": "192.168.1.100"}
exp = {'0': '192.168.1.17', '9': '192.168.1.100'}
self.assertEqual(json.loads(app_data['db-bind-addresses']), exp)

harness.evaluate_status()
Expand Down

0 comments on commit ede3f03

Please sign in to comment.