Skip to content

Commit

Permalink
Uses a JSON dictionary of unit name to bind address, instead of a list.
Browse files Browse the repository at this point in the history
This allows us to associate units with addresses when dealing with all
bind addresses across peers.
  • Loading branch information
manadart committed Jan 9, 2024
1 parent d730129 commit b665c0f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
14 changes: 7 additions & 7 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Licensed under the GPLv3, see LICENSE file for details.

import controlsocket
import json
import logging
import secrets
import urllib.parse
Expand Down Expand Up @@ -33,7 +34,7 @@ def __init__(self, *args):
self.framework.observe(
self.on.website_relation_joined, self._on_website_relation_joined)

self._stored.set_default(db_bind_address='', last_bind_addresses=[], all_bind_addresses=[])
self._stored.set_default(db_bind_address='', last_bind_addresses=[], all_bind_addresses=dict())
self.framework.observe(
self.on.dbcluster_relation_changed, self._on_dbcluster_relation_changed)

Expand Down Expand Up @@ -79,7 +80,7 @@ def _on_website_relation_joined(self, event):
try:
api_port = self.api_port()
except AgentConfException as e:
logger.error('cannot read controller API port from agent configuration: {}', e)
logger.error('cannot read controller API port from agent configuration: %s', e)
return

address = None
Expand All @@ -102,7 +103,7 @@ def _on_metrics_endpoint_relation_created(self, event: RelationJoinedEvent):
try:
api_port = self.api_port()
except AgentConfException as e:
logger.error('cannot read controller API port from agent configuration: {}', e)
logger.error('cannot read controller API port from agent configuration: %s', e)
return

metrics_endpoint = MetricsEndpointProvider(
Expand Down Expand Up @@ -143,18 +144,17 @@ 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 = [ip] if ip else []
all_bind_addresses = {self.unit.name: ip} if ip else dict()

for unit in event.relation.units:
unit_data = event.relation.data[unit]
if self.DB_BIND_ADDR_KEY in unit_data:
all_bind_addresses.append(unit_data[self.DB_BIND_ADDR_KEY])
all_bind_addresses[unit.name] = unit_data[self.DB_BIND_ADDR_KEY]

all_bind_addresses.sort()
if self._stored.all_bind_addresses == all_bind_addresses:
return

event.relation.data[self.app]['db-bind-addresses'] = ','.join(all_bind_addresses)
event.relation.data[self.app]['db-bind-addresses'] = json.dumps(all_bind_addresses)
self._stored.all_bind_addresses = all_bind_addresses

def _ensure_db_bind_address(self, event):
Expand Down
4 changes: 3 additions & 1 deletion tests/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Licensed under the GPLv3, see LICENSE file for details.

import ipaddress
import json
import os
import unittest
from charm import JujuControllerCharm, AgentConfException
Expand Down Expand Up @@ -170,7 +171,8 @@ def test_dbcluster_relation_changed_single_addr(self, binding, _):
self.assertEqual(unit_data['db-bind-address'], '192.168.1.17')

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

harness.evaluate_status()
self.assertIsInstance(harness.charm.unit.status, ActiveStatus)
Expand Down

0 comments on commit b665c0f

Please sign in to comment.