Pass valid IP address to os-brick

While fetching connector information the cinder driver of
glance-store is passing 'hostname' as IP address whereas it
actually needs IP address.

This path fetches the IPV4 or IPV6 address from the available
'hostname' and passes it to os-brick.

This worked for other cinder backends which don't rely specifically
on IP address for initializing connection like fibre channel, nfs etc
and even for some iscsi backends since, in some environments,
the hostname is same as the ip address. This is not the case always
and we should pass the correct IPv4/IPv6 address of the host to
avoid this issue.

Closes-Bug: #1955668

Change-Id: Ic79815972e654a8bfe2775f57c68cfa0bf115e2f
(cherry picked from commit 8b4d97a6f7)
(cherry picked from commit 83d9c5dc2e)
Conflicts:
	glance_store/_drivers/cinder.py
	glance_store/tests/unit/test_cinder_store.py
	glance_store/tests/unit/test_multistore_cinder.py
This commit is contained in:
whoami-rajat 2021-12-23 12:46:32 +00:00
parent b93facf1d7
commit 2bc17c0a9e
4 changed files with 43 additions and 7 deletions

View File

@ -602,6 +602,12 @@ class Store(glance_store.driver.Store):
"""
return os.path.join(mount_point_base, self.get_hash_str(share))
def _get_host_ip(self, host):
try:
return socket.getaddrinfo(host, None, socket.AF_INET6)[0][4][0]
except socket.gaierror:
return socket.getaddrinfo(host, None, socket.AF_INET)[0][4][0]
@contextlib.contextmanager
def _open_cinder_volume(self, client, volume, mode):
attach_mode = 'rw' if mode == 'wb' else 'ro'
@ -609,12 +615,13 @@ class Store(glance_store.driver.Store):
root_helper = self.get_root_helper()
priv_context.init(root_helper=shlex.split(root_helper))
host = socket.gethostname()
my_ip = self._get_host_ip(host)
use_multipath = self.store_conf.cinder_use_multipath
enforce_multipath = self.store_conf.cinder_enforce_multipath
mount_point_base = self.store_conf.cinder_mount_point_base
properties = connector.get_connector_properties(
root_helper, host, use_multipath, enforce_multipath)
root_helper, my_ip, use_multipath, enforce_multipath, host=host)
try:
volume.reserve(volume)

View File

@ -189,7 +189,18 @@ class TestCinderStore(base.StoreBaseTest,
mock.patch.object(cinder.Store, 'get_root_helper',
return_value=root_helper), \
mock.patch.object(connector.InitiatorConnector, 'factory',
side_effect=fake_factory) as fake_conn_obj:
side_effect=fake_factory
) as fake_conn_obj, \
mock.patch.object(socket,
'gethostname') as mock_get_host, \
mock.patch.object(socket,
'getaddrinfo') as mock_get_host_ip:
fake_host = 'fake_host'
fake_addr_info = [[0, 1, 2, 3, ['127.0.0.1']]]
fake_ip = fake_addr_info[0][4][0]
mock_get_host.return_value = fake_host
mock_get_host_ip.return_value = fake_addr_info
with mock.patch.object(connector,
'get_connector_properties') as mock_conn:
@ -214,8 +225,9 @@ class TestCinderStore(base.StoreBaseTest,
if not encrypted_nfs:
mock_conn.assert_called_once_with(
root_helper, socket.gethostname(),
multipath_supported, enforce_multipath)
root_helper, fake_ip,
multipath_supported, enforce_multipath,
host=fake_host)
fake_connector.connect_volume.assert_called_once_with(
mock.ANY)
fake_connector.disconnect_volume.assert_called_once_with(

View File

@ -219,7 +219,18 @@ class TestMultiCinderStore(base.MultiStoreBaseTest,
mock.patch.object(cinder.Store, 'get_root_helper',
return_value=root_helper), \
mock.patch.object(connector.InitiatorConnector, 'factory',
side_effect=fake_factory) as fake_conn_obj:
side_effect=fake_factory
) as fake_conn_obj, \
mock.patch.object(socket,
'gethostname') as mock_get_host, \
mock.patch.object(socket,
'getaddrinfo') as mock_get_host_ip:
fake_host = 'fake_host'
fake_addr_info = [[0, 1, 2, 3, ['127.0.0.1']]]
fake_ip = fake_addr_info[0][4][0]
mock_get_host.return_value = fake_host
mock_get_host_ip.return_value = fake_addr_info
with mock.patch.object(connector,
'get_connector_properties') as mock_conn:
@ -229,8 +240,8 @@ class TestMultiCinderStore(base.MultiStoreBaseTest,
do_open()
mock_conn.assert_called_once_with(
root_helper, socket.gethostname(), multipath_supported,
enforce_multipath)
root_helper, fake_ip, multipath_supported,
enforce_multipath, host=fake_host)
fake_connector.connect_volume.assert_called_once_with(mock.ANY)
fake_connector.disconnect_volume.assert_called_once_with(
mock.ANY, fake_devinfo)

View File

@ -0,0 +1,6 @@
---
fixes:
- |
`Bug #1955668 <https://bugs.launchpad.net/glance-store/+bug/1955668>`_:
Fixed issue with glance cinder store passing hostname instead of IP
address to os-brick while getting connector information.