Biser Milanov | ec3bf98 | 2024-11-27 17:42:17 +0200 | [diff] [blame] | 1 | From 2acfdddf0794754aa0e32a56800e77387f75ce38 Mon Sep 17 00:00:00 2001 |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 2 | From: Peter Penchev <openstack-dev@storpool.com> |
Biser Milanov | 8323a8c | 2024-11-20 10:53:27 +0200 | [diff] [blame] | 3 | Date: Mon, 12 Mar 2018 12:00:10 +0200 |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 4 | Subject: [PATCH] Add iSCSI export support to the StorPool driver |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 5 | |
| 6 | Add four new driver options: |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 7 | - storpool_iscsi_cinder_volume: use StorPool iSCSI attachments whenever |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 8 | the cinder-volume service needs to attach a volume to the controller, |
| 9 | e.g. for copying an image to a volume or vice versa |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 10 | - storpool_iscsi_export_to: |
| 11 | - an empty string to use the StorPool native protocol for exporting |
| 12 | volumes |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 13 | - the string "*" to always use iSCSI for exporting volumes |
| 14 | - an experimental, not fully supported list of IQN patterns to export |
| 15 | volumes to using iSCSI; this results in a Cinder driver that exports |
| 16 | different volumes using different storage protocols |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 17 | - storpool_iscsi_portal_group: the name of the iSCSI portal group |
| 18 | defined in the StorPool configuration to use for these export |
| 19 | - storpool_iscsi_learn_initiator_iqns: automatically create StorPool |
| 20 | configuration records for an initiator when a volume is first exported |
| 21 | to it |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 22 | |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 23 | When exporting volumes via iSCSI, report the storage protocol as |
| 24 | "iSCSI". |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 25 | |
| 26 | Change-Id: I9de64306e0e6976268df782053b0651dd1cca96f |
| 27 | --- |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 28 | .../unit/volume/drivers/test_storpool.py | 521 +++++++++++++++++- |
| 29 | cinder/volume/drivers/storpool.py | 379 ++++++++++++- |
| 30 | .../drivers/storpool-volume-driver.rst | 68 ++- |
| 31 | .../storpool-iscsi-cefcfe590a07c5c7.yaml | 15 + |
| 32 | 4 files changed, 972 insertions(+), 11 deletions(-) |
| 33 | create mode 100644 releasenotes/notes/storpool-iscsi-cefcfe590a07c5c7.yaml |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 34 | |
Peter Pentchev | acaaa38 | 2023-02-28 11:26:13 +0200 | [diff] [blame] | 35 | diff --git a/cinder/tests/unit/volume/drivers/test_storpool.py b/cinder/tests/unit/volume/drivers/test_storpool.py |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 36 | index 44707d0b8..d6347e1d5 100644 |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 37 | --- a/cinder/tests/unit/volume/drivers/test_storpool.py |
| 38 | +++ b/cinder/tests/unit/volume/drivers/test_storpool.py |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 39 | @@ -14,14 +14,24 @@ |
| 40 | # under the License. |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 41 | |
| 42 | |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 43 | +from __future__ import annotations |
| 44 | + |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 45 | +import dataclasses |
| 46 | import itertools |
| 47 | import re |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 48 | import sys |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 49 | +from typing import Any, NamedTuple, TYPE_CHECKING # noqa: H301 |
| 50 | from unittest import mock |
| 51 | |
| 52 | import ddt |
| 53 | from oslo_utils import units |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 54 | |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 55 | +if TYPE_CHECKING: |
| 56 | + if sys.version_info >= (3, 11): |
| 57 | + from typing import Self |
| 58 | + else: |
| 59 | + from typing_extensions import Self |
| 60 | + |
| 61 | |
| 62 | fakeStorPool = mock.Mock() |
| 63 | fakeStorPool.spopenstack = mock.Mock() |
| 64 | @@ -31,6 +41,7 @@ fakeStorPool.sptypes = mock.Mock() |
| 65 | sys.modules['storpool'] = fakeStorPool |
| 66 | |
| 67 | |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 68 | +from cinder.common import constants |
| 69 | from cinder import exception |
Biser Milanov | ec3bf98 | 2024-11-27 17:42:17 +0200 | [diff] [blame] | 70 | from cinder.tests.unit import fake_constants |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 71 | from cinder.tests.unit import test |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 72 | @@ -38,6 +49,13 @@ from cinder.volume import configuration as conf |
| 73 | from cinder.volume.drivers import storpool as driver |
| 74 | |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 75 | |
| 76 | +_ISCSI_IQN_OURS = 'beleriand' |
| 77 | +_ISCSI_IQN_OTHER = 'rohan' |
| 78 | +_ISCSI_IQN_THIRD = 'gondor' |
| 79 | +_ISCSI_PAT_OTHER = 'roh*' |
| 80 | +_ISCSI_PAT_BOTH = '*riand roh*' |
| 81 | +_ISCSI_PORTAL_GROUP = 'openstack_pg' |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 82 | + |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 83 | volume_types = { |
Biser Milanov | ec3bf98 | 2024-11-27 17:42:17 +0200 | [diff] [blame] | 84 | fake_constants.VOLUME_TYPE_ID: {}, |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 85 | fake_constants.VOLUME_TYPE2_ID: {'storpool_template': 'ssd'}, |
| 86 | @@ -71,6 +89,10 @@ def snapshotName(vtype, vid): |
| 87 | return 'os--snap--{t}--{id}'.format(t=vtype, id=vid) |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 88 | |
| 89 | |
| 90 | +def targetName(vid): |
| 91 | + return 'iqn.2012-11.storpool:{id}'.format(id=vid) |
| 92 | + |
| 93 | + |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 94 | class MockDisk(object): |
| 95 | def __init__(self, diskId): |
| 96 | self.id = diskId |
| 97 | @@ -195,6 +217,315 @@ def MockVolumeUpdateDesc(size): |
| 98 | return {'size': size} |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 99 | |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 100 | |
| 101 | +@dataclasses.dataclass(frozen=True) |
| 102 | +class MockIscsiNetwork: |
| 103 | + """Mock a StorPool IP CIDR network definition (partially).""" |
| 104 | + |
| 105 | + address: str |
| 106 | + |
| 107 | + |
| 108 | +@dataclasses.dataclass(frozen=True) |
| 109 | +class MockIscsiPortalGroup: |
| 110 | + """Mock a StorPool iSCSI portal group definition (partially).""" |
| 111 | + |
| 112 | + name: str |
| 113 | + networks: list[MockIscsiNetwork] |
| 114 | + |
| 115 | + |
| 116 | +@dataclasses.dataclass(frozen=True) |
| 117 | +class MockIscsiExport: |
| 118 | + """Mock a StorPool iSCSI exported volume/target definition.""" |
| 119 | + |
| 120 | + portalGroup: str |
| 121 | + target: str |
| 122 | + |
| 123 | + |
| 124 | +@dataclasses.dataclass(frozen=True) |
| 125 | +class MockIscsiInitiator: |
| 126 | + """Mock a StorPool iSCSI initiator definition.""" |
| 127 | + |
| 128 | + name: str |
| 129 | + exports: list[MockIscsiExport] |
| 130 | + |
| 131 | + |
| 132 | +@dataclasses.dataclass(frozen=True) |
| 133 | +class MockIscsiTarget: |
| 134 | + """Mock a StorPool iSCSI volume-to-target mapping definition.""" |
| 135 | + |
| 136 | + name: str |
| 137 | + volume: str |
| 138 | + |
| 139 | + |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 140 | +class IscsiTestCase(NamedTuple): |
| 141 | + """A single test case for the iSCSI config and export methods.""" |
| 142 | + |
| 143 | + initiator: str | None |
| 144 | + volume: str | None |
| 145 | + exported: bool |
| 146 | + commands_count: int |
| 147 | + |
| 148 | + |
| 149 | +@dataclasses.dataclass(frozen=True) |
| 150 | +class MockIscsiConfig: |
| 151 | + """Mock the structure returned by the "get current config" query.""" |
| 152 | + |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 153 | + portalGroups: dict[str, MockIscsiPortalGroup] |
| 154 | + initiators: dict[str, MockIscsiInitiator] |
| 155 | + targets: dict[str, MockIscsiTarget] |
| 156 | + |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 157 | + @classmethod |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 158 | + def build(cls, tcase: IscsiTestCase) -> Self: |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 159 | + """Build a test config structure.""" |
| 160 | + initiators = { |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 161 | + '0': MockIscsiInitiator(name=_ISCSI_IQN_OTHER, exports=[]), |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 162 | + } |
| 163 | + if tcase.initiator is not None: |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 164 | + initiators['1'] = MockIscsiInitiator( |
| 165 | + name=tcase.initiator, |
| 166 | + exports=( |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 167 | + [ |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 168 | + MockIscsiExport( |
| 169 | + portalGroup=_ISCSI_PORTAL_GROUP, |
| 170 | + target=targetName(tcase.volume), |
| 171 | + ), |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 172 | + ] |
| 173 | + if tcase.exported |
| 174 | + else [] |
| 175 | + ), |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 176 | + ) |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 177 | + |
| 178 | + targets = { |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 179 | + '0': MockIscsiTarget( |
| 180 | + name=targetName(fake_constants.VOLUME2_ID), |
| 181 | + volume=volumeName(fake_constants.VOLUME2_ID), |
| 182 | + ), |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 183 | + } |
| 184 | + if tcase.volume is not None: |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 185 | + targets['1'] = MockIscsiTarget( |
| 186 | + name=targetName(tcase.volume), |
| 187 | + volume=volumeName(tcase.volume), |
| 188 | + ) |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 189 | + |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 190 | + return cls( |
| 191 | + portalGroups={ |
| 192 | + '0': MockIscsiPortalGroup( |
| 193 | + name=_ISCSI_PORTAL_GROUP + '-not', |
| 194 | + networks=[], |
| 195 | + ), |
| 196 | + '1': MockIscsiPortalGroup( |
| 197 | + name=_ISCSI_PORTAL_GROUP, |
| 198 | + networks=[ |
| 199 | + MockIscsiNetwork(address="192.0.2.0"), |
| 200 | + MockIscsiNetwork(address="195.51.100.0"), |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 201 | + ], |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 202 | + ), |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 203 | + }, |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 204 | + initiators=initiators, |
| 205 | + targets=targets, |
| 206 | + ) |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 207 | + |
| 208 | + |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 209 | +@dataclasses.dataclass(frozen=True) |
| 210 | +class MockIscsiConfigTop: |
| 211 | + """Mock the top level of the "get the iSCSI configuration" response.""" |
| 212 | + |
| 213 | + iscsi: MockIscsiConfig |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 214 | + |
| 215 | + |
| 216 | +class MockIscsiAPI: |
| 217 | + """Mock only the iSCSI-related calls of the StorPool API bindings.""" |
| 218 | + |
| 219 | + _asrt: test.TestCase |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 220 | + _configs: list[MockIscsiConfig] |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 221 | + |
| 222 | + def __init__( |
| 223 | + self, |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 224 | + configs: list[MockIscsiConfig], |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 225 | + asrt: test.TestCase, |
| 226 | + ) -> None: |
| 227 | + """Store the reference to the list of iSCSI config objects.""" |
| 228 | + self._asrt = asrt |
| 229 | + self._configs = configs |
| 230 | + |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 231 | + def iSCSIConfig(self) -> MockIscsiConfigTop: |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 232 | + """Return the last version of the iSCSI configuration.""" |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 233 | + return MockIscsiConfigTop(iscsi=self._configs[-1]) |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 234 | + |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 235 | + def _handle_export( |
| 236 | + self, |
| 237 | + cfg: MockIscsiConfig, cmd: dict[str, Any], |
| 238 | + ) -> MockIscsiConfig: |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 239 | + """Add an export for an initiator.""" |
| 240 | + self._asrt.assertDictEqual( |
| 241 | + cmd, |
| 242 | + { |
| 243 | + 'initiator': _ISCSI_IQN_OURS, |
| 244 | + 'portalGroup': _ISCSI_PORTAL_GROUP, |
Biser Milanov | ec3bf98 | 2024-11-27 17:42:17 +0200 | [diff] [blame] | 245 | + 'volumeName': volumeName(fake_constants.VOLUME_ID), |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 246 | + }, |
| 247 | + ) |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 248 | + self._asrt.assertEqual(cfg.initiators['1'].name, cmd['initiator']) |
| 249 | + self._asrt.assertListEqual(cfg.initiators['1'].exports, []) |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 250 | + |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 251 | + return dataclasses.replace( |
| 252 | + cfg, |
| 253 | + initiators={ |
| 254 | + **cfg.initiators, |
| 255 | + '1': dataclasses.replace( |
| 256 | + cfg.initiators['1'], |
| 257 | + exports=[ |
| 258 | + MockIscsiExport( |
| 259 | + portalGroup=cmd['portalGroup'], |
| 260 | + target=targetName(fake_constants.VOLUME_ID), |
| 261 | + ), |
| 262 | + ], |
| 263 | + ), |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 264 | + }, |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 265 | + ) |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 266 | + |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 267 | + def _handle_delete_export( |
| 268 | + self, |
| 269 | + cfg: MockIscsiConfig, |
| 270 | + cmd: dict[str, Any], |
| 271 | + ) -> MockIscsiConfig: |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 272 | + """Delete an export for an initiator.""" |
| 273 | + self._asrt.assertDictEqual( |
| 274 | + cmd, |
| 275 | + { |
| 276 | + 'initiator': _ISCSI_IQN_OURS, |
| 277 | + 'portalGroup': _ISCSI_PORTAL_GROUP, |
Biser Milanov | ec3bf98 | 2024-11-27 17:42:17 +0200 | [diff] [blame] | 278 | + 'volumeName': volumeName(fake_constants.VOLUME_ID), |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 279 | + }, |
| 280 | + ) |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 281 | + self._asrt.assertEqual(cfg.initiators['1'].name, cmd['initiator']) |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 282 | + self._asrt.assertListEqual( |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 283 | + cfg.initiators['1'].exports, |
| 284 | + [MockIscsiExport(portalGroup=_ISCSI_PORTAL_GROUP, |
| 285 | + target=cfg.targets['1'].name)]) |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 286 | + |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 287 | + updated_initiators = cfg.initiators |
| 288 | + del updated_initiators['1'] |
| 289 | + return dataclasses.replace(cfg, initiators=updated_initiators) |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 290 | + |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 291 | + def _handle_create_initiator( |
| 292 | + self, |
| 293 | + cfg: MockIscsiConfig, |
| 294 | + cmd: dict[str, Any], |
| 295 | + ) -> MockIscsiConfig: |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 296 | + """Add a whole new initiator.""" |
| 297 | + self._asrt.assertDictEqual( |
| 298 | + cmd, |
| 299 | + { |
| 300 | + 'name': _ISCSI_IQN_OURS, |
| 301 | + 'username': '', |
| 302 | + 'secret': '', |
| 303 | + }, |
| 304 | + ) |
| 305 | + self._asrt.assertNotIn( |
| 306 | + cmd['name'], |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 307 | + [init.name for init in cfg.initiators.values()], |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 308 | + ) |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 309 | + self._asrt.assertListEqual(sorted(cfg.initiators), ['0']) |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 310 | + |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 311 | + return dataclasses.replace( |
| 312 | + cfg, |
| 313 | + initiators={ |
| 314 | + **cfg.initiators, |
| 315 | + '1': MockIscsiInitiator(name=cmd['name'], exports=[]), |
| 316 | + }, |
| 317 | + ) |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 318 | + |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 319 | + def _handle_create_target( |
| 320 | + self, |
| 321 | + cfg: MockIscsiConfig, |
| 322 | + cmd: dict[str, Any], |
| 323 | + ) -> MockIscsiConfig: |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 324 | + """Add a target for a volume so that it may be exported.""" |
| 325 | + self._asrt.assertDictEqual( |
| 326 | + cmd, |
Biser Milanov | ec3bf98 | 2024-11-27 17:42:17 +0200 | [diff] [blame] | 327 | + {'volumeName': volumeName(fake_constants.VOLUME_ID)}, |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 328 | + ) |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 329 | + self._asrt.assertListEqual(sorted(cfg.targets), ['0']) |
| 330 | + return dataclasses.replace( |
| 331 | + cfg, |
| 332 | + targets={ |
| 333 | + **cfg.targets, |
| 334 | + '1': MockIscsiTarget( |
| 335 | + name=targetName(fake_constants.VOLUME_ID), |
| 336 | + volume=volumeName(fake_constants.VOLUME_ID), |
| 337 | + ), |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 338 | + }, |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 339 | + ) |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 340 | + |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 341 | + def _handle_delete_target( |
| 342 | + self, |
| 343 | + cfg: MockIscsiConfig, |
| 344 | + cmd: dict[str, Any] |
| 345 | + ) -> MockIscsiConfig: |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 346 | + """Remove a target for a volume.""" |
| 347 | + self._asrt.assertDictEqual( |
| 348 | + cmd, |
Biser Milanov | ec3bf98 | 2024-11-27 17:42:17 +0200 | [diff] [blame] | 349 | + {'volumeName': volumeName(fake_constants.VOLUME_ID)}, |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 350 | + ) |
| 351 | + |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 352 | + self._asrt.assertListEqual(sorted(cfg.targets), ['0', '1']) |
| 353 | + updated_targets = cfg.targets |
| 354 | + del updated_targets['1'] |
| 355 | + return dataclasses.replace(cfg, targets=updated_targets) |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 356 | + |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 357 | + def _handle_initiator_add_network( |
| 358 | + self, |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 359 | + cfg: MockIscsiConfig, |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 360 | + cmd: dict[str, Any], |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 361 | + ) -> MockIscsiConfig: |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 362 | + """Add a network that an initiator is allowed to log in from.""" |
| 363 | + self._asrt.assertDictEqual( |
| 364 | + cmd, |
| 365 | + { |
| 366 | + 'initiator': _ISCSI_IQN_OURS, |
| 367 | + 'net': '0.0.0.0/0', |
| 368 | + }, |
| 369 | + ) |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 370 | + return dataclasses.replace(cfg) |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 371 | + |
| 372 | + _CMD_HANDLERS = { |
| 373 | + 'createInitiator': _handle_create_initiator, |
| 374 | + 'createTarget': _handle_create_target, |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 375 | + 'deleteTarget': _handle_delete_target, |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 376 | + 'export': _handle_export, |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 377 | + 'exportDelete': _handle_delete_export, |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 378 | + 'initiatorAddNetwork': _handle_initiator_add_network, |
| 379 | + } |
| 380 | + |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 381 | + def iSCSIConfigChange( |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 382 | + self, |
| 383 | + commands: dict[str, list[dict[str, dict[str, Any]]]], |
| 384 | + ) -> None: |
| 385 | + """Apply the requested changes to the iSCSI configuration. |
| 386 | + |
| 387 | + This method adds a new config object to the configs list, |
| 388 | + making a shallow copy of the last one and applying the changes |
| 389 | + specified in the list of commands. |
| 390 | + """ |
| 391 | + self._asrt.assertListEqual(sorted(commands), ['commands']) |
| 392 | + self._asrt.assertGreater(len(commands['commands']), 0) |
| 393 | + for cmd in commands['commands']: |
| 394 | + keys = sorted(cmd.keys()) |
| 395 | + cmd_name = keys[0] |
| 396 | + self._asrt.assertListEqual(keys, [cmd_name]) |
| 397 | + handler = self._CMD_HANDLERS[cmd_name] |
| 398 | + new_cfg = handler(self, self._configs[-1], cmd[cmd_name]) |
| 399 | + self._configs.append(new_cfg) |
| 400 | + |
| 401 | + |
| 402 | +_ISCSI_TEST_CASES = [ |
| 403 | + IscsiTestCase(None, None, False, 4), |
| 404 | + IscsiTestCase(_ISCSI_IQN_OURS, None, False, 2), |
Biser Milanov | ec3bf98 | 2024-11-27 17:42:17 +0200 | [diff] [blame] | 405 | + IscsiTestCase(_ISCSI_IQN_OURS, fake_constants.VOLUME_ID, False, 1), |
| 406 | + IscsiTestCase(_ISCSI_IQN_OURS, fake_constants.VOLUME_ID, True, 0), |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 407 | +] |
| 408 | + |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 409 | + |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 410 | def MockSPConfig(section = 's01'): |
| 411 | res = {} |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 412 | m = re.match('^s0*([A-Za-z0-9]+)$', section) |
| 413 | @@ -237,7 +568,15 @@ class StorPoolTestCase(test.TestCase): |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 414 | self.cfg.volume_backend_name = 'storpool_test' |
| 415 | self.cfg.storpool_template = None |
| 416 | self.cfg.storpool_replication = 3 |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 417 | + self.cfg.storpool_iscsi_cinder_volume = False |
| 418 | + self.cfg.storpool_iscsi_export_to = '' |
| 419 | + self.cfg.storpool_iscsi_learn_initiator_iqns = True |
| 420 | + self.cfg.storpool_iscsi_portal_group = _ISCSI_PORTAL_GROUP |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 421 | |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 422 | + self._setup_test_driver() |
| 423 | + |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 424 | + def _setup_test_driver(self): |
| 425 | + """Initialize a StorPool driver as per the current configuration.""" |
| 426 | mock_exec = mock.Mock() |
| 427 | mock_exec.return_value = ('', '') |
| 428 | |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 429 | @@ -246,7 +585,7 @@ class StorPoolTestCase(test.TestCase): |
| 430 | self.driver.check_for_setup_error() |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 431 | |
| 432 | @ddt.data( |
| 433 | - (5, TypeError), |
| 434 | + (5, (TypeError, AttributeError)), |
| 435 | ({'no-host': None}, KeyError), |
| 436 | ({'host': 'sbad'}, driver.StorPoolConfigurationInvalid), |
| 437 | ({'host': 's01'}, None), |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 438 | @@ -262,7 +601,7 @@ class StorPoolTestCase(test.TestCase): |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 439 | conn) |
| 440 | |
| 441 | @ddt.data( |
| 442 | - (5, TypeError), |
| 443 | + (5, (TypeError, AttributeError)), |
| 444 | ({'no-host': None}, KeyError), |
| 445 | ({'host': 'sbad'}, driver.StorPoolConfigurationInvalid), |
| 446 | ) |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 447 | @@ -301,7 +640,7 @@ class StorPoolTestCase(test.TestCase): |
Peter Pentchev | 5a9f8a6 | 2023-12-06 10:40:18 +0200 | [diff] [blame] | 448 | self.assertEqual(21, pool['total_capacity_gb']) |
| 449 | self.assertEqual(5, int(pool['free_capacity_gb'])) |
| 450 | |
| 451 | - self.assertTrue(pool['multiattach']) |
| 452 | + self.assertFalse(pool['multiattach']) |
| 453 | self.assertFalse(pool['QoS_support']) |
| 454 | self.assertFalse(pool['thick_provisioning_support']) |
| 455 | self.assertTrue(pool['thin_provisioning_support']) |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 456 | @@ -720,3 +1059,179 @@ class StorPoolTestCase(test.TestCase): |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 457 | 'No such volume', |
| 458 | self.driver.revert_to_snapshot, None, |
| 459 | {'id': vol_id}, {'id': snap_id}) |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 460 | + |
| 461 | + @ddt.data( |
| 462 | + # The default values |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 463 | + ('', False, constants.STORPOOL, _ISCSI_IQN_OURS, False), |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 464 | + |
| 465 | + # Export to all |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 466 | + ('*', True, constants.ISCSI, _ISCSI_IQN_OURS, True), |
| 467 | + ('*', True, constants.ISCSI, _ISCSI_IQN_OURS, True), |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 468 | + |
| 469 | + # Only export to the controller |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 470 | + ('', False, constants.STORPOOL, _ISCSI_IQN_OURS, False), |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 471 | + |
| 472 | + # Some of the not-fully-supported pattern lists |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 473 | + (_ISCSI_PAT_OTHER, False, constants.STORPOOL, _ISCSI_IQN_OURS, False), |
| 474 | + (_ISCSI_PAT_OTHER, False, constants.STORPOOL, _ISCSI_IQN_OTHER, True), |
| 475 | + (_ISCSI_PAT_BOTH, False, constants.STORPOOL, _ISCSI_IQN_OURS, True), |
| 476 | + (_ISCSI_PAT_BOTH, False, constants.STORPOOL, _ISCSI_IQN_OTHER, True), |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 477 | + ) |
| 478 | + @ddt.unpack |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 479 | + def test_wants_iscsi(self, storpool_iscsi_export_to, use_iscsi, |
| 480 | + storage_protocol, hostname, expected): |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 481 | + """Check the "should this export use iSCSI?" detection.""" |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 482 | + self.cfg.storpool_iscsi_export_to = storpool_iscsi_export_to |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 483 | + self._setup_test_driver() |
| 484 | + self.assertEqual(self.driver._use_iscsi, use_iscsi) |
| 485 | + |
| 486 | + # Make sure the driver reports the correct protocol in the stats |
| 487 | + self.driver._update_volume_stats() |
| 488 | + self.assertEqual(self.driver._stats["vendor_name"], "StorPool") |
| 489 | + self.assertEqual(self.driver._stats["storage_protocol"], |
| 490 | + storage_protocol) |
| 491 | + |
| 492 | + def check(conn, forced, expected): |
| 493 | + """Pass partially or completely valid connector info.""" |
| 494 | + for initiator in (None, hostname): |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 495 | + for host in (None, _ISCSI_IQN_THIRD): |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 496 | + self.assertEqual( |
| 497 | + self.driver._connector_wants_iscsi({ |
| 498 | + "host": host, |
| 499 | + "initiator": initiator, |
| 500 | + **conn, |
| 501 | + }), |
| 502 | + expected if initiator is not None and host is not None |
| 503 | + else forced) |
| 504 | + |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 505 | + # If storpool_iscsi_cinder_volume is set and this is the controller, |
| 506 | + # then yes. |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 507 | + check({"storpool_wants_iscsi": True}, True, True) |
| 508 | + |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 509 | + # If storpool_iscsi_cinder_volume is not set or this is not the |
| 510 | + # controller, then look at the specified expected value. |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 511 | + check({"storpool_wants_iscsi": False}, use_iscsi, expected) |
| 512 | + check({}, use_iscsi, expected) |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 513 | + |
| 514 | + def _validate_iscsi_config( |
| 515 | + self, |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 516 | + cfg: MockIscsiConfig, |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 517 | + res: dict[str, Any], |
| 518 | + tcase: IscsiTestCase, |
| 519 | + ) -> None: |
| 520 | + """Make sure the returned structure makes sense.""" |
| 521 | + initiator = res['initiator'] |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 522 | + cfg_initiator = cfg.initiators.get('1') |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 523 | + |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 524 | + self.assertIs(res['cfg'].iscsi, cfg) |
| 525 | + self.assertEqual(res['pg'].name, _ISCSI_PORTAL_GROUP) |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 526 | + |
| 527 | + if tcase.initiator is None: |
| 528 | + self.assertIsNone(initiator) |
| 529 | + else: |
| 530 | + self.assertIsNotNone(initiator) |
| 531 | + self.assertEqual(initiator, cfg_initiator) |
| 532 | + |
| 533 | + if tcase.volume is None: |
| 534 | + self.assertIsNone(res['target']) |
| 535 | + else: |
| 536 | + self.assertIsNotNone(res['target']) |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 537 | + self.assertEqual(res['target'], cfg.targets.get('1')) |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 538 | + |
| 539 | + if tcase.initiator is None: |
| 540 | + self.assertIsNone(cfg_initiator) |
| 541 | + self.assertIsNone(res['export']) |
| 542 | + else: |
| 543 | + self.assertIsNotNone(cfg_initiator) |
| 544 | + if tcase.exported: |
| 545 | + self.assertIsNotNone(res['export']) |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 546 | + self.assertEqual(res['export'], cfg_initiator.exports[0]) |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 547 | + else: |
| 548 | + self.assertIsNone(res['export']) |
| 549 | + |
| 550 | + @ddt.data(*_ISCSI_TEST_CASES) |
| 551 | + def test_iscsi_get_config(self, tcase: IscsiTestCase) -> None: |
| 552 | + """Make sure the StorPool iSCSI configuration is parsed correctly.""" |
| 553 | + cfg_orig = MockIscsiConfig.build(tcase) |
| 554 | + configs = [cfg_orig] |
| 555 | + iapi = MockIscsiAPI(configs, self) |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 556 | + with mock.patch.object(self.driver._attach, 'api', new=lambda: iapi): |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 557 | + res = self.driver._get_iscsi_config( |
| 558 | + _ISCSI_IQN_OURS, |
Biser Milanov | ec3bf98 | 2024-11-27 17:42:17 +0200 | [diff] [blame] | 559 | + fake_constants.VOLUME_ID, |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 560 | + ) |
| 561 | + |
| 562 | + self._validate_iscsi_config(cfg_orig, res, tcase) |
| 563 | + |
| 564 | + @ddt.data(*_ISCSI_TEST_CASES) |
| 565 | + def test_iscsi_create_export(self, tcase: IscsiTestCase) -> None: |
| 566 | + """Make sure _create_iscsi_export() makes the right API calls.""" |
| 567 | + cfg_orig = MockIscsiConfig.build(tcase) |
| 568 | + configs = [cfg_orig] |
| 569 | + iapi = MockIscsiAPI(configs, self) |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 570 | + with mock.patch.object(self.driver._attach, 'api', new=lambda: iapi): |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 571 | + self.driver._create_iscsi_export( |
| 572 | + { |
Biser Milanov | ec3bf98 | 2024-11-27 17:42:17 +0200 | [diff] [blame] | 573 | + 'id': fake_constants.VOLUME_ID, |
| 574 | + 'display_name': fake_constants.VOLUME_NAME, |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 575 | + }, |
| 576 | + { |
| 577 | + # Yeah, okay, so we cheat a little bit here... |
| 578 | + 'host': _ISCSI_IQN_OURS + '.hostname', |
| 579 | + 'initiator': _ISCSI_IQN_OURS, |
| 580 | + }, |
| 581 | + ) |
| 582 | + |
| 583 | + self.assertEqual(len(configs), tcase.commands_count + 1) |
| 584 | + cfg_final = configs[-1] |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 585 | + self.assertEqual(cfg_final.initiators['1'].name, _ISCSI_IQN_OURS) |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 586 | + self.assertEqual( |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 587 | + cfg_final.initiators['1'].exports[0].target, |
Biser Milanov | ec3bf98 | 2024-11-27 17:42:17 +0200 | [diff] [blame] | 588 | + targetName(fake_constants.VOLUME_ID), |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 589 | + ) |
| 590 | + self.assertEqual( |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 591 | + cfg_final.targets['1'].volume, |
Biser Milanov | ec3bf98 | 2024-11-27 17:42:17 +0200 | [diff] [blame] | 592 | + volumeName(fake_constants.VOLUME_ID), |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 593 | + ) |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 594 | + |
| 595 | + @ddt.data(*_ISCSI_TEST_CASES) |
| 596 | + def test_remove_iscsi_export(self, tcase: IscsiTestCase): |
| 597 | + cfg_orig = MockIscsiConfig.build(tcase) |
| 598 | + configs = [cfg_orig] |
| 599 | + iapi = MockIscsiAPI(configs, self) |
| 600 | + |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 601 | + def _target_exists(cfg: MockIscsiConfig, volume: str) -> bool: |
| 602 | + for name, target in cfg.targets.items(): |
| 603 | + if target.volume == volumeName(volume): |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 604 | + return True |
| 605 | + return False |
| 606 | + |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 607 | + def _export_exists(cfg: MockIscsiConfig, volume: str) -> bool: |
| 608 | + for name, initiator in cfg.initiators.items(): |
| 609 | + for export in initiator.exports: |
| 610 | + if export.target == targetName(volume): |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 611 | + return True |
| 612 | + return False |
| 613 | + |
| 614 | + if tcase.exported: |
| 615 | + self.assertTrue( |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 616 | + _target_exists(iapi.iSCSIConfig().iscsi, tcase.volume)) |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 617 | + self.assertTrue( |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 618 | + _export_exists(iapi.iSCSIConfig().iscsi, tcase.volume)) |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 619 | + |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 620 | + with mock.patch.object(self.driver._attach, 'api', new=lambda: iapi): |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 621 | + self.driver._remove_iscsi_export( |
| 622 | + { |
Biser Milanov | ec3bf98 | 2024-11-27 17:42:17 +0200 | [diff] [blame] | 623 | + 'id': fake_constants.VOLUME_ID, |
| 624 | + 'display_name': fake_constants.VOLUME_NAME, |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 625 | + }, |
| 626 | + { |
| 627 | + 'host': _ISCSI_IQN_OURS + '.hostname', |
| 628 | + 'initiator': _ISCSI_IQN_OURS, |
| 629 | + }, |
| 630 | + ) |
| 631 | + |
| 632 | + self.assertFalse( |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 633 | + _target_exists(iapi.iSCSIConfig().iscsi, tcase.volume)) |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 634 | + self.assertFalse( |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 635 | + _export_exists(iapi.iSCSIConfig().iscsi, tcase.volume)) |
Peter Pentchev | acaaa38 | 2023-02-28 11:26:13 +0200 | [diff] [blame] | 636 | diff --git a/cinder/volume/drivers/storpool.py b/cinder/volume/drivers/storpool.py |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 637 | index a8200a7f1..d931200f6 100644 |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 638 | --- a/cinder/volume/drivers/storpool.py |
| 639 | +++ b/cinder/volume/drivers/storpool.py |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 640 | @@ -15,6 +15,7 @@ |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 641 | |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 642 | """StorPool block device driver""" |
| 643 | |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 644 | +import fnmatch |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 645 | import platform |
Biser Milanov | 90b5a14 | 2024-11-28 11:33:39 +0200 | [diff] [blame] | 646 | |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 647 | from oslo_config import cfg |
| 648 | @@ -43,6 +44,32 @@ if storpool: |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 649 | |
| 650 | |
| 651 | storpool_opts = [ |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 652 | + cfg.BoolOpt('storpool_iscsi_cinder_volume', |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 653 | + default=False, |
| 654 | + help='Let the cinder-volume service use iSCSI instead of ' |
| 655 | + 'the StorPool block device driver for accessing ' |
| 656 | + 'StorPool volumes, e.g. when creating a volume from ' |
| 657 | + 'an image or vice versa.'), |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 658 | + cfg.StrOpt('storpool_iscsi_export_to', |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 659 | + default='', |
| 660 | + help='Whether to export volumes using iSCSI. ' |
| 661 | + 'An empty string (the default) makes the driver export ' |
| 662 | + 'all volumes using the StorPool native network protocol. ' |
| 663 | + 'The value "*" makes the driver export all volumes using ' |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 664 | + 'iSCSI (see the Cinder StorPool driver documentation for ' |
| 665 | + 'how this option and ``storpool_iscsi_cinder_volume`` ' |
| 666 | + 'interact). Any other value leads to an experimental ' |
| 667 | + 'not fully supported configuration and is interpreted as ' |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 668 | + 'a whitespace-separated list of patterns for IQNs for ' |
| 669 | + 'hosts that need volumes to be exported via iSCSI, e.g. ' |
| 670 | + '"iqn.1991-05.com.microsoft:\\*" for Windows hosts.'), |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 671 | + cfg.BoolOpt('storpool_iscsi_learn_initiator_iqns', |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 672 | + default=True, |
| 673 | + help='Create a StorPool record for a new initiator as soon as ' |
| 674 | + 'Cinder asks for a volume to be exported to it.'), |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 675 | + cfg.StrOpt('storpool_iscsi_portal_group', |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 676 | + default=None, |
| 677 | + help='The portal group to export volumes via iSCSI in.'), |
| 678 | cfg.StrOpt('storpool_template', |
| 679 | default=None, |
| 680 | help='The StorPool template for volumes with no type.'), |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 681 | @@ -93,9 +120,10 @@ class StorPoolDriver(driver.VolumeDriver): |
| 682 | add ignore_errors to the internal _detach_volume() method |
| 683 | 1.2.3 - Advertise some more driver capabilities. |
| 684 | 2.0.0 - Implement revert_to_snapshot(). |
| 685 | + 2.1.0 - Add iSCSI export support. |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 686 | """ |
| 687 | |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 688 | - VERSION = '2.0.0' |
| 689 | + VERSION = '2.1.0' |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 690 | CI_WIKI_NAME = 'StorPool_distributed_storage_CI' |
| 691 | |
| 692 | def __init__(self, *args, **kwargs): |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 693 | @@ -105,6 +133,7 @@ class StorPoolDriver(driver.VolumeDriver): |
| 694 | self._ourId = None |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 695 | self._ourIdInt = None |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 696 | self._attach = None |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 697 | + self._use_iscsi = False |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 698 | |
| 699 | @staticmethod |
| 700 | def get_driver_options(): |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 701 | @@ -159,10 +188,327 @@ class StorPoolDriver(driver.VolumeDriver): |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 702 | raise StorPoolConfigurationInvalid( |
| 703 | section=hostname, param='SP_OURID', error=e) |
| 704 | |
| 705 | + def _connector_wants_iscsi(self, connector): |
| 706 | + """Should we do this export via iSCSI? |
| 707 | + |
| 708 | + Check the configuration to determine whether this connector is |
| 709 | + expected to provide iSCSI exports as opposed to native StorPool |
| 710 | + protocol ones. Match the initiator's IQN against the list of |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 711 | + patterns supplied in the "storpool_iscsi_export_to" configuration |
| 712 | + setting. |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 713 | + """ |
| 714 | + if connector is None: |
| 715 | + return False |
| 716 | + if self._use_iscsi: |
| 717 | + LOG.debug(' - forcing iSCSI for all exported volumes') |
| 718 | + return True |
| 719 | + if connector.get('storpool_wants_iscsi'): |
| 720 | + LOG.debug(' - forcing iSCSI for the controller') |
| 721 | + return True |
| 722 | + |
| 723 | + try: |
| 724 | + iqn = connector.get('initiator') |
| 725 | + except Exception: |
| 726 | + iqn = None |
| 727 | + try: |
| 728 | + host = connector.get('host') |
| 729 | + except Exception: |
| 730 | + host = None |
| 731 | + if iqn is None or host is None: |
| 732 | + LOG.debug(' - this connector certainly does not want iSCSI') |
| 733 | + return False |
| 734 | + |
| 735 | + LOG.debug(' - check whether %(host)s (%(iqn)s) wants iSCSI', |
| 736 | + { |
| 737 | + 'host': host, |
| 738 | + 'iqn': iqn, |
| 739 | + }) |
| 740 | + |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 741 | + export_to = self.configuration.storpool_iscsi_export_to |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 742 | + if export_to is None: |
| 743 | + return False |
| 744 | + |
| 745 | + for pat in export_to.split(): |
| 746 | + LOG.debug(' - matching against %(pat)s', {'pat': pat}) |
| 747 | + if fnmatch.fnmatch(iqn, pat): |
| 748 | + LOG.debug(' - got it!') |
| 749 | + return True |
| 750 | + LOG.debug(' - nope') |
| 751 | + return False |
| 752 | + |
| 753 | def validate_connector(self, connector): |
| 754 | + if self._connector_wants_iscsi(connector): |
| 755 | + return True |
| 756 | return self._storpool_client_id(connector) >= 0 |
| 757 | |
| 758 | + def _get_iscsi_config(self, iqn, volume_id): |
| 759 | + """Get the StorPool iSCSI config items pertaining to this volume. |
| 760 | + |
| 761 | + Find the elements of the StorPool iSCSI configuration tree that |
| 762 | + will be needed to create, ensure, or remove the iSCSI export of |
| 763 | + the specified volume to the specified initiator. |
| 764 | + """ |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 765 | + cfg = self._attach.api().iSCSIConfig() |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 766 | + |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 767 | + pg_name = self.configuration.storpool_iscsi_portal_group |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 768 | + pg_found = [ |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 769 | + pg for pg in cfg.iscsi.portalGroups.values() if pg.name == pg_name |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 770 | + ] |
| 771 | + if not pg_found: |
| 772 | + raise Exception('StorPool Cinder iSCSI configuration error: ' |
| 773 | + 'no portal group "{pg}"'.format(pg=pg_name)) |
| 774 | + pg = pg_found[0] |
| 775 | + |
| 776 | + # Do we know about this initiator? |
| 777 | + i_found = [ |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 778 | + init for init in cfg.iscsi.initiators.values() if init.name == iqn |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 779 | + ] |
| 780 | + if i_found: |
| 781 | + initiator = i_found[0] |
| 782 | + else: |
| 783 | + initiator = None |
| 784 | + |
| 785 | + # Is this volume already being exported? |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 786 | + volname = self._attach.volumeName(volume_id) |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 787 | + t_found = [ |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 788 | + tgt for tgt in cfg.iscsi.targets.values() if tgt.volume == volname |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 789 | + ] |
| 790 | + if t_found: |
| 791 | + target = t_found[0] |
| 792 | + else: |
| 793 | + target = None |
| 794 | + |
| 795 | + # OK, so is this volume being exported to this initiator? |
| 796 | + export = None |
| 797 | + if initiator is not None and target is not None: |
| 798 | + e_found = [ |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 799 | + exp for exp in initiator.exports |
| 800 | + if exp.portalGroup == pg.name and exp.target == target.name |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 801 | + ] |
| 802 | + if e_found: |
| 803 | + export = e_found[0] |
| 804 | + |
| 805 | + return { |
| 806 | + 'cfg': cfg, |
| 807 | + 'pg': pg, |
| 808 | + 'initiator': initiator, |
| 809 | + 'target': target, |
| 810 | + 'export': export, |
| 811 | + 'volume_name': volname, |
| 812 | + 'volume_id': volume_id, |
| 813 | + } |
| 814 | + |
| 815 | + def _create_iscsi_export(self, volume, connector): |
| 816 | + """Create (if needed) an iSCSI export for the StorPool volume.""" |
| 817 | + LOG.debug( |
| 818 | + '_create_iscsi_export() invoked for volume ' |
| 819 | + '"%(vol_name)s" (%(vol_id)s) connector %(connector)s', |
| 820 | + { |
| 821 | + 'vol_name': volume['display_name'], |
| 822 | + 'vol_id': volume['id'], |
| 823 | + 'connector': connector, |
| 824 | + } |
| 825 | + ) |
| 826 | + iqn = connector['initiator'] |
| 827 | + try: |
| 828 | + cfg = self._get_iscsi_config(iqn, volume['id']) |
| 829 | + except Exception as exc: |
| 830 | + LOG.error( |
| 831 | + 'Could not fetch the iSCSI config: %(exc)s', {'exc': exc} |
| 832 | + ) |
| 833 | + raise |
| 834 | + |
| 835 | + if cfg['initiator'] is None: |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 836 | + if not (self.configuration.storpool_iscsi_learn_initiator_iqns or |
| 837 | + self.configuration.storpool_iscsi_cinder_volume and |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 838 | + connector.get('storpool_wants_iscsi')): |
| 839 | + raise Exception('The "{iqn}" initiator IQN for the "{host}" ' |
| 840 | + 'host is not defined in the StorPool ' |
| 841 | + 'configuration.' |
| 842 | + .format(iqn=iqn, host=connector['host'])) |
| 843 | + else: |
| 844 | + LOG.info('Creating a StorPool iSCSI initiator ' |
| 845 | + 'for "{host}s" ({iqn}s)', |
| 846 | + {'host': connector['host'], 'iqn': iqn}) |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 847 | + self._attach.api().iSCSIConfigChange({ |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 848 | + 'commands': [ |
| 849 | + { |
| 850 | + 'createInitiator': { |
| 851 | + 'name': iqn, |
| 852 | + 'username': '', |
| 853 | + 'secret': '', |
| 854 | + }, |
| 855 | + }, |
| 856 | + { |
| 857 | + 'initiatorAddNetwork': { |
| 858 | + 'initiator': iqn, |
| 859 | + 'net': '0.0.0.0/0', |
| 860 | + }, |
| 861 | + }, |
| 862 | + ] |
| 863 | + }) |
| 864 | + |
| 865 | + if cfg['target'] is None: |
| 866 | + LOG.info( |
| 867 | + 'Creating a StorPool iSCSI target ' |
| 868 | + 'for the "%(vol_name)s" volume (%(vol_id)s)', |
| 869 | + { |
| 870 | + 'vol_name': volume['display_name'], |
| 871 | + 'vol_id': volume['id'], |
| 872 | + } |
| 873 | + ) |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 874 | + self._attach.api().iSCSIConfigChange({ |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 875 | + 'commands': [ |
| 876 | + { |
| 877 | + 'createTarget': { |
| 878 | + 'volumeName': cfg['volume_name'], |
| 879 | + }, |
| 880 | + }, |
| 881 | + ] |
| 882 | + }) |
| 883 | + cfg = self._get_iscsi_config(iqn, volume['id']) |
| 884 | + |
| 885 | + if cfg['export'] is None: |
| 886 | + LOG.info('Creating a StorPool iSCSI export ' |
| 887 | + 'for the "{vol_name}s" volume ({vol_id}s) ' |
| 888 | + 'to the "{host}s" initiator ({iqn}s) ' |
| 889 | + 'in the "{pg}s" portal group', |
| 890 | + { |
| 891 | + 'vol_name': volume['display_name'], |
| 892 | + 'vol_id': volume['id'], |
| 893 | + 'host': connector['host'], |
| 894 | + 'iqn': iqn, |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 895 | + 'pg': cfg['pg'].name |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 896 | + }) |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 897 | + self._attach.api().iSCSIConfigChange({ |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 898 | + 'commands': [ |
| 899 | + { |
| 900 | + 'export': { |
| 901 | + 'initiator': iqn, |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 902 | + 'portalGroup': cfg['pg'].name, |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 903 | + 'volumeName': cfg['volume_name'], |
| 904 | + }, |
| 905 | + }, |
| 906 | + ] |
| 907 | + }) |
| 908 | + |
Peter Pentchev | c53e6c0 | 2023-02-08 15:13:56 +0200 | [diff] [blame] | 909 | + target_portals = [ |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 910 | + "{addr}:3260".format(addr=net.address) |
| 911 | + for net in cfg['pg'].networks |
Peter Pentchev | c53e6c0 | 2023-02-08 15:13:56 +0200 | [diff] [blame] | 912 | + ] |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 913 | + target_iqns = [cfg['target'].name] * len(target_portals) |
Peter Pentchev | c53e6c0 | 2023-02-08 15:13:56 +0200 | [diff] [blame] | 914 | + target_luns = [0] * len(target_portals) |
| 915 | + if connector.get('multipath', False): |
| 916 | + multipath_settings = { |
| 917 | + 'target_iqns': target_iqns, |
| 918 | + 'target_portals': target_portals, |
| 919 | + 'target_luns': target_luns, |
| 920 | + } |
| 921 | + else: |
| 922 | + multipath_settings = {} |
| 923 | + |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 924 | + res = { |
| 925 | + 'driver_volume_type': 'iscsi', |
| 926 | + 'data': { |
Peter Pentchev | c53e6c0 | 2023-02-08 15:13:56 +0200 | [diff] [blame] | 927 | + **multipath_settings, |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 928 | + 'target_discovered': False, |
Peter Pentchev | c53e6c0 | 2023-02-08 15:13:56 +0200 | [diff] [blame] | 929 | + 'target_iqn': target_iqns[0], |
| 930 | + 'target_portal': target_portals[0], |
| 931 | + 'target_lun': target_luns[0], |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 932 | + 'volume_id': volume['id'], |
| 933 | + 'discard': True, |
| 934 | + }, |
| 935 | + } |
| 936 | + LOG.debug('returning %(res)s', {'res': res}) |
| 937 | + return res |
| 938 | + |
| 939 | + def _remove_iscsi_export(self, volume, connector): |
| 940 | + """Remove an iSCSI export for the specified StorPool volume.""" |
| 941 | + LOG.debug( |
| 942 | + '_remove_iscsi_export() invoked for volume ' |
| 943 | + '"%(vol_name)s" (%(vol_id)s) connector %(conn)s', |
| 944 | + { |
| 945 | + 'vol_name': volume['display_name'], |
| 946 | + 'vol_id': volume['id'], |
| 947 | + 'conn': connector, |
| 948 | + } |
| 949 | + ) |
| 950 | + try: |
| 951 | + cfg = self._get_iscsi_config(connector['initiator'], volume['id']) |
| 952 | + except Exception as exc: |
| 953 | + LOG.error( |
| 954 | + 'Could not fetch the iSCSI config: %(exc)s', {'exc': exc} |
| 955 | + ) |
| 956 | + raise |
| 957 | + |
| 958 | + if cfg['export'] is not None: |
| 959 | + LOG.info('Removing the StorPool iSCSI export ' |
| 960 | + 'for the "%(vol_name)s" volume (%(vol_id)s) ' |
| 961 | + 'to the "%(host)s" initiator (%(iqn)s) ' |
| 962 | + 'in the "%(pg)s" portal group', |
| 963 | + { |
| 964 | + 'vol_name': volume['display_name'], |
| 965 | + 'vol_id': volume['id'], |
| 966 | + 'host': connector['host'], |
| 967 | + 'iqn': connector['initiator'], |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 968 | + 'pg': cfg['pg'].name, |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 969 | + }) |
| 970 | + try: |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 971 | + self._attach.api().iSCSIConfigChange({ |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 972 | + 'commands': [ |
| 973 | + { |
| 974 | + 'exportDelete': { |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 975 | + 'initiator': cfg['initiator'].name, |
| 976 | + 'portalGroup': cfg['pg'].name, |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 977 | + 'volumeName': cfg['volume_name'], |
| 978 | + }, |
| 979 | + }, |
| 980 | + ] |
| 981 | + }) |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 982 | + except spapi.ApiError as e: |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 983 | + if e.name not in ('objectExists', 'objectDoesNotExist'): |
| 984 | + raise |
| 985 | + LOG.info('Looks like somebody beat us to it') |
| 986 | + |
| 987 | + if cfg['target'] is not None: |
| 988 | + last = True |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 989 | + for initiator in cfg['cfg'].iscsi.initiators.values(): |
| 990 | + if initiator.name == cfg['initiator'].name: |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 991 | + continue |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 992 | + for exp in initiator.exports: |
| 993 | + if exp.target == cfg['target'].name: |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 994 | + last = False |
| 995 | + break |
| 996 | + if not last: |
| 997 | + break |
| 998 | + |
| 999 | + if last: |
| 1000 | + LOG.info( |
| 1001 | + 'Removing the StorPool iSCSI target ' |
| 1002 | + 'for the "{vol_name}s" volume ({vol_id}s)', |
| 1003 | + { |
| 1004 | + 'vol_name': volume['display_name'], |
| 1005 | + 'vol_id': volume['id'], |
| 1006 | + } |
| 1007 | + ) |
| 1008 | + try: |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 1009 | + self._attach.api().iSCSIConfigChange({ |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 1010 | + 'commands': [ |
| 1011 | + { |
| 1012 | + 'deleteTarget': { |
| 1013 | + 'volumeName': cfg['volume_name'], |
| 1014 | + }, |
| 1015 | + }, |
| 1016 | + ] |
| 1017 | + }) |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 1018 | + except spapi.ApiError as e: |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 1019 | + if e.name not in ('objectDoesNotExist', 'invalidParam'): |
| 1020 | + raise |
| 1021 | + LOG.info('Looks like somebody beat us to it') |
| 1022 | + |
| 1023 | def initialize_connection(self, volume, connector): |
| 1024 | + if self._connector_wants_iscsi(connector): |
| 1025 | + return self._create_iscsi_export(volume, connector) |
| 1026 | return {'driver_volume_type': 'storpool', |
| 1027 | 'data': { |
| 1028 | 'client_id': self._storpool_client_id(connector), |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 1029 | @@ -171,6 +517,9 @@ class StorPoolDriver(driver.VolumeDriver): |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 1030 | }} |
| 1031 | |
| 1032 | def terminate_connection(self, volume, connector, **kwargs): |
| 1033 | + if self._connector_wants_iscsi(connector): |
| 1034 | + LOG.debug('- removing an iSCSI export') |
| 1035 | + self._remove_iscsi_export(volume, connector) |
| 1036 | pass |
| 1037 | |
| 1038 | def create_snapshot(self, snapshot): |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 1039 | @@ -272,11 +621,20 @@ class StorPoolDriver(driver.VolumeDriver): |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 1040 | ) |
| 1041 | |
| 1042 | def create_export(self, context, volume, connector): |
| 1043 | - pass |
| 1044 | + if self._connector_wants_iscsi(connector): |
| 1045 | + LOG.debug('- creating an iSCSI export') |
| 1046 | + self._create_iscsi_export(volume, connector) |
| 1047 | |
| 1048 | def remove_export(self, context, volume): |
| 1049 | pass |
| 1050 | |
| 1051 | + def _attach_volume(self, context, volume, properties, remote=False): |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 1052 | + if self.configuration.storpool_iscsi_cinder_volume and not remote: |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 1053 | + LOG.debug('- adding the "storpool_wants_iscsi" flag') |
| 1054 | + properties['storpool_wants_iscsi'] = True |
| 1055 | + |
| 1056 | + return super()._attach_volume(context, volume, properties, remote) |
| 1057 | + |
| 1058 | def delete_volume(self, volume): |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 1059 | name = self._attach.volumeName(volume['id']) |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 1060 | try: |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 1061 | @@ -313,6 +671,17 @@ class StorPoolDriver(driver.VolumeDriver): |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 1062 | LOG.error("StorPoolDriver API initialization failed: %s", e) |
| 1063 | raise |
| 1064 | |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 1065 | + export_to = self.configuration.storpool_iscsi_export_to |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 1066 | + export_to_set = export_to is not None and export_to.split() |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 1067 | + vol_iscsi = self.configuration.storpool_iscsi_cinder_volume |
| 1068 | + pg_name = self.configuration.storpool_iscsi_portal_group |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 1069 | + if (export_to_set or vol_iscsi) and pg_name is None: |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 1070 | + msg = _('The "storpool_iscsi_portal_group" option is required if ' |
| 1071 | + 'any patterns are listed in "storpool_iscsi_export_to"') |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 1072 | + raise exception.VolumeDriverException(message=msg) |
| 1073 | + |
| 1074 | + self._use_iscsi = export_to == "*" |
| 1075 | + |
| 1076 | def _update_volume_stats(self): |
| 1077 | try: |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 1078 | dl = self._attach.api().disksList() |
| 1079 | @@ -338,7 +707,7 @@ class StorPoolDriver(driver.VolumeDriver): |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 1080 | 'total_capacity_gb': total / units.Gi, |
| 1081 | 'free_capacity_gb': free / units.Gi, |
| 1082 | 'reserved_percentage': 0, |
| 1083 | - 'multiattach': True, |
Peter Pentchev | 5a9f8a6 | 2023-12-06 10:40:18 +0200 | [diff] [blame] | 1084 | + 'multiattach': self._use_iscsi, |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 1085 | 'QoS_support': False, |
| 1086 | 'thick_provisioning_support': False, |
| 1087 | 'thin_provisioning_support': True, |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 1088 | @@ -357,7 +726,9 @@ class StorPoolDriver(driver.VolumeDriver): |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 1089 | 'volume_backend_name') or 'storpool', |
| 1090 | 'vendor_name': 'StorPool', |
| 1091 | 'driver_version': self.VERSION, |
| 1092 | - 'storage_protocol': constants.STORPOOL, |
| 1093 | + 'storage_protocol': ( |
| 1094 | + constants.ISCSI if self._use_iscsi else constants.STORPOOL |
| 1095 | + ), |
Peter Pentchev | acaaa38 | 2023-02-28 11:26:13 +0200 | [diff] [blame] | 1096 | # Driver capabilities |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 1097 | 'clone_across_pools': True, |
| 1098 | 'sparse_copy_volume': True, |
Peter Pentchev | acaaa38 | 2023-02-28 11:26:13 +0200 | [diff] [blame] | 1099 | diff --git a/doc/source/configuration/block-storage/drivers/storpool-volume-driver.rst b/doc/source/configuration/block-storage/drivers/storpool-volume-driver.rst |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 1100 | index d2c5895a9..936e83675 100644 |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 1101 | --- a/doc/source/configuration/block-storage/drivers/storpool-volume-driver.rst |
| 1102 | +++ b/doc/source/configuration/block-storage/drivers/storpool-volume-driver.rst |
Peter Pentchev | acaaa38 | 2023-02-28 11:26:13 +0200 | [diff] [blame] | 1103 | @@ -19,12 +19,15 @@ Prerequisites |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 1104 | * The controller and all the compute nodes must have access to the StorPool |
| 1105 | API service. |
| 1106 | |
| 1107 | -* All nodes where StorPool-backed volumes will be attached must have access to |
| 1108 | +* If iSCSI is not being used as a transport (see below), all nodes where |
| 1109 | + StorPool-backed volumes will be attached must have access to |
| 1110 | the StorPool data network and run the ``storpool_block`` service. |
| 1111 | |
| 1112 | -* If StorPool-backed Cinder volumes need to be created directly from Glance |
| 1113 | - images, then the node running the ``cinder-volume`` service must also have |
| 1114 | - access to the StorPool data network and run the ``storpool_block`` service. |
| 1115 | +* If Glance uses Cinder as its image store, or if StorPool-backed Cinder |
| 1116 | + volumes need to be created directly from Glance images, and iSCSI is not |
| 1117 | + being used as a transport, then the node running the ``cinder-volume`` |
| 1118 | + service must also have access to the StorPool data network and run |
| 1119 | + the ``storpool_block`` service. |
| 1120 | |
| 1121 | * All nodes that need to access the StorPool API (the compute nodes and |
| 1122 | the node running the ``cinder-volume`` service) must have the following |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 1123 | @@ -34,6 +37,34 @@ Prerequisites |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 1124 | * the storpool Python bindings package |
| 1125 | * the storpool.spopenstack Python helper package |
| 1126 | |
| 1127 | +Using iSCSI as the transport protocol |
| 1128 | +------------------------------------- |
| 1129 | + |
| 1130 | +The StorPool distributed storage system uses its own, highly optimized and |
| 1131 | +tailored for its specifics, network protocol for communication between |
| 1132 | +the storage servers and the clients (the OpenStack cluster nodes where |
| 1133 | +StorPool-backed volumes will be attached). There are cases when granting |
| 1134 | +various nodes access to the StorPool data network or installing and |
| 1135 | +running the ``storpool_block`` client service on them may pose difficulties. |
| 1136 | +The StorPool servers may also expose the user-created volumes and snapshots |
| 1137 | +using the standard iSCSI protocol that only requires TCP routing and |
| 1138 | +connectivity between the storage servers and the StorPool clients. |
| 1139 | +The StorPool Cinder driver may be configured to export volumes and |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 1140 | +snapshots via iSCSI using the ``storpool_iscsi_export_to`` and |
| 1141 | +``storpool_iscsi_portal_group`` configuration options. |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 1142 | + |
| 1143 | +Additionally, even if e.g. the hypervisor nodes running Nova will use |
| 1144 | +the StorPool network protocol and run the ``storpool_block`` service |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 1145 | +(so the ``storpool_iscsi_export_to`` option has its default empty string |
| 1146 | +value), the ``storpool_iscsi_cinder_volume`` option configures the |
| 1147 | +StorPool Cinder driver so that only the ``cinder-volume`` service will |
| 1148 | +use the iSCSI protocol when attaching volumes and snapshots to transfer |
| 1149 | +data to and from Glance images. |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 1150 | + |
| 1151 | +Multiattach support for StorPool is only enabled if iSCSI is used: |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 1152 | +``storpool_iscsi_export_to`` is set to ``*``, that is, when all StorPool |
| 1153 | +volumes will be exported via iSCSI to all initiators. |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 1154 | + |
| 1155 | Configuring the StorPool volume driver |
| 1156 | -------------------------------------- |
| 1157 | |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 1158 | @@ -55,6 +86,35 @@ volume backend definition) and per volume type: |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 1159 | with the default placement constraints for the StorPool cluster. |
| 1160 | The default value for the chain replication is 3. |
| 1161 | |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 1162 | +In addition, if the iSCSI protocol is used to access the StorPool cluster as |
| 1163 | +described in the previous section, the following options may be defined in |
| 1164 | +the ``cinder.conf`` volume backend definition: |
| 1165 | + |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 1166 | +- ``storpool_iscsi_export_to``: if set to the value ``*``, the StorPool |
| 1167 | + Cinder driver will export volumes and snapshots using the iSCSI |
| 1168 | + protocol instead of the StorPool network protocol. The |
| 1169 | + ``storpool_iscsi_portal_group`` option must also be specified. |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 1170 | + |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 1171 | +- ``storpool_iscsi_portal_group``: if the ``storpool_iscsi_export_to`` |
| 1172 | + option is set to the value ``*`` or the |
| 1173 | + ``storpool_iscsi_cinder_volume`` option is turned on, this option |
| 1174 | + specifies the name of the iSCSI portal group that Cinder volumes will |
| 1175 | + be exported to. |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 1176 | + |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 1177 | +- ``storpool_iscsi_cinder_volume``: if enabled, even if the |
| 1178 | + ``storpool_iscsi_export_to`` option has its default empty value, the |
| 1179 | + ``cinder-volume`` service will use iSCSI to attach the volumes and |
| 1180 | + snapshots for transferring data to and from Glance images if Glance is |
| 1181 | + configured to use the Cinder glance_store. |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 1182 | + |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 1183 | +- ``storpool_iscsi_learn_initiator_iqns``: if enabled, the StorPool |
| 1184 | + Cinder driver will automatically use the StorPool API to create |
| 1185 | + definitions for new initiators in the StorPool cluster's |
| 1186 | + configuration. This is the default behavior of the driver; it may be |
| 1187 | + disabled in the rare case if, e.g. because of site policy, OpenStack |
| 1188 | + iSCSI initiators (e.g. Nova hypervisors) need to be explicitly allowed |
| 1189 | + to use the StorPool iSCSI targets. |
Peter Pentchev | ea35446 | 2023-07-18 11:15:56 +0300 | [diff] [blame] | 1190 | + |
Peter Pentchev | 9c24be9 | 2022-09-26 22:35:24 +0300 | [diff] [blame] | 1191 | Using the StorPool volume driver |
| 1192 | -------------------------------- |
| 1193 | |
Biser Milanov | da1b068 | 2024-11-29 09:37:10 +0200 | [diff] [blame^] | 1194 | diff --git a/releasenotes/notes/storpool-iscsi-cefcfe590a07c5c7.yaml b/releasenotes/notes/storpool-iscsi-cefcfe590a07c5c7.yaml |
| 1195 | new file mode 100644 |
| 1196 | index 000000000..3863e4099 |
| 1197 | --- /dev/null |
| 1198 | +++ b/releasenotes/notes/storpool-iscsi-cefcfe590a07c5c7.yaml |
| 1199 | @@ -0,0 +1,15 @@ |
| 1200 | +features: |
| 1201 | + - | |
| 1202 | + StorPool driver: Added support for exporting the StorPool-backed |
| 1203 | + volumes using the iSCSI protocol, so that the Cinder volume service |
| 1204 | + and/or the Nova or Glance consumers do not need to have the StorPool |
| 1205 | + block device third-party service installed. See the StorPool driver |
| 1206 | + section in the Cinder documentation for more information on the |
| 1207 | + ``storpool_iscsi_export_to``, ``storpool_iscsi_portal_group``, |
| 1208 | + ``storpool_iscsi_cinder_volume``, and |
| 1209 | + ``storpool_iscsi_learn_initiator_iqns`` options. |
| 1210 | + |
| 1211 | + .. note:: |
| 1212 | + Multiattach support for StorPool is now only enabled if |
| 1213 | + ``storpool_iscsi_export_to`` is set to ``*``, that is, when all |
| 1214 | + StorPool volumes will be exported via iSCSI to all initiators. |
Peter Pentchev | acaaa38 | 2023-02-28 11:26:13 +0200 | [diff] [blame] | 1215 | -- |
Biser Milanov | d684c1c | 2024-11-22 12:12:59 +0200 | [diff] [blame] | 1216 | 2.43.0 |
Peter Pentchev | acaaa38 | 2023-02-28 11:26:13 +0200 | [diff] [blame] | 1217 | |