Merge "add a new funtion to return IP address(es) of a host machine"

This commit is contained in:
Zuul 2024-08-13 12:04:49 +00:00 committed by Gerrit Code Review
commit 1ebf2098c6
3 changed files with 70 additions and 2 deletions

View File

@ -24,6 +24,8 @@ import traceback
from oslo_concurrency import processutils as putils from oslo_concurrency import processutils as putils
from oslo_log import log as logging from oslo_log import log as logging
from oslo_utils import netutils
import psutil
from os_brick import exception from os_brick import exception
from os_brick.i18n import _ from os_brick.i18n import _
@ -62,6 +64,37 @@ class LightOSConnector(base.BaseLinuxConnector):
self.message_queue = message_queue self.message_queue = message_queue
self.DISCOVERY_DIR_PATH = '/etc/discovery-client/discovery.d/' self.DISCOVERY_DIR_PATH = '/etc/discovery-client/discovery.d/'
@staticmethod
def get_ip_addresses():
"""Find all IPs for the host machine, return list of IP addresses."""
def get_ip_addresses_psutil():
ip_addresses = []
for interface, snics in psutil.net_if_addrs().items():
for snic in snics:
# Collect each (interface, address) tuple
ip_addresses.append((interface, snic.address))
return ip_addresses
loop_back = ['lo']
ips = []
is_ipv6_enabled = netutils.is_ipv6_enabled()
iface_with_ips = get_ip_addresses_psutil()
for iface_ip_tuple in iface_with_ips:
iface = iface_ip_tuple[0]
ip = iface_ip_tuple[1]
if iface in loop_back:
continue
if ip == "":
continue
if is_ipv6_enabled and netutils.is_valid_ipv6(ip):
parts = ip.split("%")
ip = parts[0]
ips.append(ip)
elif netutils.is_valid_ipv4(ip):
ips.append(ip)
return ips
@staticmethod @staticmethod
def get_connector_properties(root_helper, *args, **kwargs): def get_connector_properties(root_helper, *args, **kwargs):
"""The LightOS connector properties.""" """The LightOS connector properties."""
@ -71,6 +104,9 @@ class LightOSConnector(base.BaseLinuxConnector):
execute=kwargs.get('execute')) execute=kwargs.get('execute'))
hostnqn = utils.get_host_nqn() hostnqn = utils.get_host_nqn()
found_dsc = lightos_connector.find_dsc() found_dsc = lightos_connector.find_dsc()
host_ips = lightos_connector.get_ip_addresses()
LOG.info('Current host hostNQN %s and IP(s) are %s ', hostnqn,
host_ips)
if not found_dsc: if not found_dsc:
LOG.debug('LIGHTOS: did not find dsc, continuing anyway.') LOG.debug('LIGHTOS: did not find dsc, continuing anyway.')
@ -80,6 +116,7 @@ class LightOSConnector(base.BaseLinuxConnector):
hostnqn, found_dsc) hostnqn, found_dsc)
props['nqn'] = hostnqn props['nqn'] = hostnqn
props['found_dsc'] = found_dsc props['found_dsc'] = found_dsc
props['host_ips'] = host_ips
else: else:
LOG.debug('LIGHTOS: no hostnqn found.') LOG.debug('LIGHTOS: no hostnqn found.')

View File

@ -18,6 +18,8 @@ import queue
from unittest import mock from unittest import mock
from unittest.mock import mock_open from unittest.mock import mock_open
import psutil
from os_brick import exception from os_brick import exception
from os_brick.initiator.connectors import lightos from os_brick.initiator.connectors import lightos
from os_brick.initiator import linuxscsi from os_brick.initiator import linuxscsi
@ -26,6 +28,9 @@ from os_brick.tests.initiator import test_connector
from os_brick import utils from os_brick import utils
FAKE_NQN = "nqn.fake.qnq" FAKE_NQN = "nqn.fake.qnq"
FAKE_HOST_IPS = [
"1234:5678:9abc:def0:1234:5678:9abc:def0",
"1234:5678:0:42::8a2e:370:7334", '172.17.0.1']
FAKE_LIGHTOS_CLUSTER_NODES = { FAKE_LIGHTOS_CLUSTER_NODES = {
"nodes": [ "nodes": [
@ -80,13 +85,16 @@ class LightosConnectorTestCase(test_connector.ConnectorTestCase):
lightos_nodes=lightos_nodes lightos_nodes=lightos_nodes
) )
@mock.patch.object(lightos.LightOSConnector, 'get_ip_addresses',
return_value=FAKE_HOST_IPS)
@mock.patch.object(utils, 'get_host_nqn', @mock.patch.object(utils, 'get_host_nqn',
return_value=FAKE_NQN) return_value=FAKE_NQN)
@mock.patch.object(lightos.LightOSConnector, 'find_dsc', @mock.patch.object(lightos.LightOSConnector, 'find_dsc',
return_value=True) return_value=True)
def test_get_connector_properties(self, mock_nqn, mock_dsc): def test_get_connector_properties(self, mock_nqn, mock_dsc, mock_host_ips):
props = self.connector.get_connector_properties(None) props = self.connector.get_connector_properties(None)
expected_props = {"nqn": FAKE_NQN, "found_dsc": True} expected_props = {"nqn": FAKE_NQN, "found_dsc": True,
"host_ips": FAKE_HOST_IPS}
self.assertEqual(expected_props, props) self.assertEqual(expected_props, props)
@mock.patch.object(lightos.http.client.HTTPConnection, "request", @mock.patch.object(lightos.http.client.HTTPConnection, "request",
@ -202,6 +210,28 @@ class LightosConnectorTestCase(test_connector.ConnectorTestCase):
self.assertEqual(self.connector.extend_volume(connection_properties), self.assertEqual(self.connector.extend_volume(connection_properties),
NUM_BLOCKS_IN_GIB * BLOCK_SIZE) NUM_BLOCKS_IN_GIB * BLOCK_SIZE)
def mock_net_if_addr():
class MockSnicAdd:
def __init__(self, address):
self.address = address
return {
'lo': [
MockSnicAdd(address='127.0.0.1'),
MockSnicAdd(address='::1')
],
'wlp0s20f3': [
MockSnicAdd(address=FAKE_HOST_IPS[2]),
MockSnicAdd(address=FAKE_HOST_IPS[1]),
MockSnicAdd(address=f'{FAKE_HOST_IPS[0]}%wlp0s20f3')
]
}
@mock.patch.object(psutil, 'net_if_addrs', new=mock_net_if_addr)
def test_get_ips(self):
self.assertEqual(sorted(self.connector.get_ip_addresses()),
sorted(FAKE_HOST_IPS))
def test_monitor_message_queue_delete(self): def test_monitor_message_queue_delete(self):
message_queue = queue.Queue() message_queue = queue.Queue()
connection = {"uuid": "123"} connection = {"uuid": "123"}

View File

@ -15,3 +15,4 @@ oslo.utils>=6.0.0 # Apache-2.0
requests>=2.25.1 # Apache-2.0 requests>=2.25.1 # Apache-2.0
tenacity>=6.3.1 # Apache-2.0 tenacity>=6.3.1 # Apache-2.0
os-win>=5.7.0 # Apache-2.0 os-win>=5.7.0 # Apache-2.0
psutil>=5.7.2 # BSD