From d9ee86297eb8ccbb1ac921d76869c1c0d44d96ab Mon Sep 17 00:00:00 2001 From: Felipe Reyes Date: Mon, 27 Mar 2017 18:47:51 -0300 Subject: [PATCH] Handle list of addresses in ceph-public-address charmhelpers' CephContext class is capable of handling a list of IP addresses in the ceph-public-address attribute, this allows ceph-proxy to hand the list of monitor hosts. Closes-Bug: 1642430 Change-Id: I2bbff167fce2c75a3a619b658f0c569c6d5be3d5 --- hooks/ceph_radosgw_context.py | 29 ++++++++-------- unit_tests/test_ceph_radosgw_context.py | 44 +++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 16 deletions(-) diff --git a/hooks/ceph_radosgw_context.py b/hooks/ceph_radosgw_context.py index 2fa1c05d..3ba6b781 100644 --- a/hooks/ceph_radosgw_context.py +++ b/hooks/ceph_radosgw_context.py @@ -35,7 +35,6 @@ from charmhelpers.core.hookenv import ( ) from charmhelpers.contrib.network.ip import ( format_ipv6_addr, - get_host_ip, get_ipv6_addr, ) from charmhelpers.contrib.storage.linux.ceph import CephConfContext @@ -125,7 +124,7 @@ def ensure_host_resolvable_v6(hostname): shutil.rmtree(dtmp) -class MonContext(context.OSContextGenerator): +class MonContext(context.CephContext): interfaces = ['ceph-radosgw'] def __call__(self): @@ -133,17 +132,21 @@ class MonContext(context.OSContextGenerator): return {} mon_hosts = [] auths = [] - for relid in relation_ids('mon'): - for unit in related_units(relid): - ceph_public_addr = relation_get('ceph-public-address', unit, - relid) - if ceph_public_addr: - host_ip = format_ipv6_addr(ceph_public_addr) or \ - get_host_ip(ceph_public_addr) - mon_hosts.append('{}:6789'.format(host_ip)) - _auth = relation_get('auth', unit, relid) - if _auth: - auths.append(_auth) + + for rid in relation_ids('mon'): + for unit in related_units(rid): + _auth = relation_get('auth', rid=rid, unit=unit) + if _auth: + auths.append(_auth) + + ceph_pub_addr = relation_get('ceph-public-address', rid=rid, + unit=unit) + unit_priv_addr = relation_get('private-address', rid=rid, + unit=unit) + ceph_addr = ceph_pub_addr or unit_priv_addr + ceph_addr = format_ipv6_addr(ceph_addr) or ceph_addr + if ceph_addr: + mon_hosts.append(ceph_addr) if len(set(auths)) != 1: e = ("Inconsistent or absent auth returned by mon units. Setting " diff --git a/unit_tests/test_ceph_radosgw_context.py b/unit_tests/test_ceph_radosgw_context.py index e55249c3..e0f91367 100644 --- a/unit_tests/test_ceph_radosgw_context.py +++ b/unit_tests/test_ceph_radosgw_context.py @@ -190,7 +190,7 @@ class MonContextTest(CharmTestCase): expect = { 'auth_supported': 'cephx', 'hostname': 'testhost', - 'mon_hosts': '10.5.4.1:6789 10.5.4.2:6789 10.5.4.3:6789', + 'mon_hosts': '10.5.4.1 10.5.4.2 10.5.4.3', 'old_auth': False, 'use_syslog': 'false', 'loglevel': 1, @@ -208,6 +208,44 @@ class MonContextTest(CharmTestCase): self.assertEqual(expect, mon_ctxt()) self.assertTrue(mock_ensure_rsv_v6.called) + @patch.object(ceph, 'config', lambda *args: + '{"client.radosgw.gateway": {"rgw init timeout": 60}}') + @patch.object(context, 'ensure_host_resolvable_v6') + def test_list_of_addresses_from_ceph_proxy(self, mock_ensure_rsv_v6): + self.socket.gethostname.return_value = 'testhost' + mon_ctxt = context.MonContext() + addresses = ['10.5.4.1 10.5.4.2 10.5.4.3'] + + def _relation_get(attr, unit, rid): + if attr == 'ceph-public-address': + return addresses.pop() + elif attr == 'auth': + return 'cephx' + + self.relation_get.side_effect = _relation_get + self.relation_ids.return_value = ['mon:6'] + self.related_units.return_value = ['ceph-proxy/0'] + expect = { + 'auth_supported': 'cephx', + 'hostname': 'testhost', + 'mon_hosts': '10.5.4.1 10.5.4.2 10.5.4.3', + 'old_auth': False, + 'use_syslog': 'false', + 'loglevel': 1, + 'port': 70, + 'client_radosgw_gateway': {'rgw init timeout': 60}, + 'ipv6': False + } + self.assertEqual(expect, mon_ctxt()) + self.assertFalse(mock_ensure_rsv_v6.called) + + self.test_config.set('prefer-ipv6', True) + addresses = ['10.5.4.1 10.5.4.2 10.5.4.3'] + expect['ipv6'] = True + expect['port'] = "[::]:%s" % (70) + self.assertEqual(expect, mon_ctxt()) + self.assertTrue(mock_ensure_rsv_v6.called) + @patch.object(ceph, 'config', lambda *args: '{"client.radosgw.gateway": {"rgw init timeout": 60}}') def test_ctxt_missing_data(self): @@ -237,7 +275,7 @@ class MonContextTest(CharmTestCase): expect = { 'auth_supported': 'none', 'hostname': 'testhost', - 'mon_hosts': '10.5.4.1:6789 10.5.4.2:6789 10.5.4.3:6789', + 'mon_hosts': '10.5.4.1 10.5.4.2 10.5.4.3', 'old_auth': False, 'use_syslog': 'false', 'loglevel': 1, @@ -266,7 +304,7 @@ class MonContextTest(CharmTestCase): expect = { 'auth_supported': 'cephx', 'hostname': 'testhost', - 'mon_hosts': '10.5.4.1:6789 10.5.4.2:6789 10.5.4.3:6789', + 'mon_hosts': '10.5.4.1 10.5.4.2 10.5.4.3', 'old_auth': False, 'use_syslog': 'false', 'loglevel': 1,