Get private-address for local unit from relation

Currently, the private-address for the local unit is queried using
unit_get, which can cause it to return an address from a different
binding. This patch changes it to always query from the relation.

Closes-bug: #2020669
Change-Id: I128420c572d5491b9af4cf34614f4534c787d02c
This commit is contained in:
Tiago Pasqualini 2023-05-23 12:14:25 -03:00
parent 0628e02983
commit 71100249ee
2 changed files with 13 additions and 8 deletions

View File

@ -460,7 +460,15 @@ def get_ha_nodes():
if config('prefer-ipv6'):
addr = get_ipv6_addr()
else:
addr = get_host_ip(unit_get('private-address'))
rids = relation_ids('hanode')
if len(rids) > 0:
addr = relation_get('ingress-address',
rid=rids[0],
unit=local_unit())
addr = get_host_ip(addr)
else:
addr = get_host_ip(unit_get('private-address'))
ha_nodes[corosync_id] = addr

View File

@ -129,17 +129,14 @@ class UtilsTestCase(unittest.TestCase):
@mock.patch.object(utils, 'get_corosync_id', lambda u: "%s-cid" % (u))
@mock.patch.object(utils, 'peer_ips', lambda *args, **kwargs:
{'hanode/1': '10.0.0.2', 'hanode/2': None})
@mock.patch.object(utils, 'unit_get')
@mock.patch.object(utils, 'relation_ids', lambda *args: [1])
@mock.patch.object(utils, 'relation_get',
lambda *args, **kwargs: '10.0.0.1')
@mock.patch.object(utils, 'config')
def test_get_ha_nodes(self, mock_config, mock_unit_get, mock_get_host_ip,
def test_get_ha_nodes(self, mock_config, mock_get_host_ip,
mock_get_ipv6_addr):
mock_get_host_ip.side_effect = lambda host: host
def unit_get(key):
return {'private-address': '10.0.0.1'}.get(key)
mock_unit_get.side_effect = unit_get
def config(key):
return {'prefer-ipv6': False}.get(key)