Reconcile stale Manila share access rules
Cold migration and resize revoke the source host's Manila access rule inline during confirm/revert and swallow failures. If manila-api is unreachable the rule is left granted and never retried, so a CephFS cephx identity or an NFS ip rule can outlive the instance that needed it. Add a reconcile pass, run at host startup and on a periodic task. For this host's instances and the migrations that touched it, it recomputes the access rules each share still needs and revokes the rest. Any host can drain an orphaned CephFS 'nova-<hex>' identity; a host drains its own NFS ip rule only when no local instance needs it and it is not mounted. Shares whose instance is missing or in transit are left for a later pass. The periodic interval is [DEFAULT] stale_share_access_reconcile_interval (default 1800s; 0 or less runs only the start-up pass). Partially-implements: blueprint virtiofs-cold-migration Related-Bug: #2161761 Signed-off-by: Goutham Pacha Ravi <gouthampravi@gmail.com> Assisted-by: Claude Opus 4.8 Change-Id: I63a392a304e3a0a8e028528e3cbd4f2f1cbcf797
This commit is contained in:
1 parent
fa1d7431d5
commit
6b9fd51dc3
20 files changed
+1181
-12
No files matched your search
@@ -1668,6 +1668,16 @@ class ComputeManager(manager.Manager):
|
||||
# _sync_scheduler_instance_info periodic task will.
|
||||
self._update_scheduler_instance_info(context, instances)
|
||||
|
||||
# Revoke any Manila share access rules left behind by a cold
|
||||
# migration or resize whose confirm/revert failed to clean them up.
|
||||
# Non-fatal: manila may be down at startup and the periodic task
|
||||
# retries.
|
||||
try:
|
||||
self.share_manager.reconcile_stale_share_access(context)
|
||||
except Exception:
|
||||
LOG.exception(
|
||||
"Failed to reconcile stale share access rules at startup")
|
||||
|
||||
def _error_out_instances_whose_build_was_interrupted(
|
||||
self, context, already_handled_instances, node_uuids):
|
||||
"""If there are instances in BUILDING state that are not
|
||||
@@ -11801,6 +11811,17 @@ class ComputeManager(manager.Manager):
|
||||
instance=instance)
|
||||
break
|
||||
|
||||
@periodic_task.periodic_task(
|
||||
spacing=CONF.stale_share_access_reconcile_interval
|
||||
if CONF.stale_share_access_reconcile_interval != 0
|
||||
else -1)
|
||||
def _reconcile_stale_share_access(self, context):
|
||||
"""Periodically revoke Manila share access rules left behind by a
|
||||
cold migration or resize whose confirm/revert failed to clean them
|
||||
up.
|
||||
"""
|
||||
self.share_manager.reconcile_stale_share_access(context)
|
||||
|
||||
@messaging.expected_exceptions(exception.InstanceQuiesceNotSupported,
|
||||
exception.QemuGuestAgentNotEnabled,
|
||||
exception.NovaException,
|
||||
|
||||
@@ -585,6 +585,178 @@ class ShareManager:
|
||||
"ignoring", share_mapping.share_id,
|
||||
)
|
||||
|
||||
def reconcile_stale_share_access(self, context):
|
||||
"""Revoke Manila access rules left behind by failed cleanup."""
|
||||
if CONF.manila.auth_type is None:
|
||||
return
|
||||
|
||||
# A single conductor-side query returns every share this host might
|
||||
# hold a stale rule for (instances here now, plus instances whose
|
||||
# migration touched this host). _reconcile_share revalidates each
|
||||
# share under a lock, so a rule still needed by a live instance here
|
||||
# is preserved.
|
||||
share_ids = {
|
||||
sm.share_id
|
||||
for sm in objects.ShareMappingList.get_by_host_for_reconcile(
|
||||
context, CONF.host)
|
||||
}
|
||||
|
||||
if not share_ids:
|
||||
return
|
||||
|
||||
try:
|
||||
for share_id in share_ids:
|
||||
try:
|
||||
self._reconcile_share(context, share_id)
|
||||
except (
|
||||
exception.ManilaConnectionFailed,
|
||||
keystone_exception.MissingAuthPlugin,
|
||||
):
|
||||
# Manila is unreachable, or the [manila] section is not
|
||||
# configured on this compute: re-raise so the whole pass
|
||||
# bails out instead of the broad handler below swallowing
|
||||
# it and retrying every remaining share against an
|
||||
# endpoint we cannot reach.
|
||||
raise
|
||||
except Exception:
|
||||
LOG.exception(
|
||||
"Failed to reconcile stale access rules for share %s",
|
||||
share_id)
|
||||
except exception.ManilaConnectionFailed:
|
||||
LOG.warning(
|
||||
"Manila is unreachable, skipping stale share access "
|
||||
"reconcile; it will be retried on the next interval")
|
||||
except keystone_exception.MissingAuthPlugin:
|
||||
LOG.warning(
|
||||
"The [manila] section is not configured with credentials on "
|
||||
"this compute; skipping stale share access reconcile")
|
||||
|
||||
def _reconcile_share(self, context, share_id):
|
||||
@utils.synchronized(share_id)
|
||||
def _locked():
|
||||
# Re-read the share's mappings and their instances from the DB
|
||||
# under the lock, so the desired access rules are computed from
|
||||
# committed ground truth at the moment we diff against manila,
|
||||
# not from a snapshot taken at the top of the pass. This closes
|
||||
# the window where a migration completing, or another host
|
||||
# attaching the share, mid-pass would make us revoke a live grant.
|
||||
mappings = objects.ShareMappingList.get_by_share_id(
|
||||
context, share_id)
|
||||
if not mappings:
|
||||
return
|
||||
|
||||
instance_uuids = list({m.instance_uuid for m in mappings})
|
||||
# Not filtered by CONF.host: an instance that migrated away still
|
||||
# has a stale rule to drain here, and we need its *current* host
|
||||
# to know which per-host cephx identity is legitimate. Bounded to
|
||||
# this one share's instances (usually one), so it stays small.
|
||||
found = objects.InstanceList.get_by_filters(
|
||||
context, {'uuid': instance_uuids}, expected_attrs=[])
|
||||
instances_by_uuid = {inst.uuid: inst for inst in found}
|
||||
|
||||
# Conservative: if any of the share's instances is unknown
|
||||
# (deleted or a read race) or in transit (migrating/resizing,
|
||||
# when an off-host grant legitimately still exists), skip the
|
||||
# whole share and let a later pass retry once it is quiescent.
|
||||
instances = []
|
||||
for mapping in mappings:
|
||||
instance = instances_by_uuid.get(mapping.instance_uuid)
|
||||
if instance is None or self._instance_in_transit(instance):
|
||||
return
|
||||
instances.append(instance)
|
||||
|
||||
rules = self.manila_api.get_access_rules(context, share_id)
|
||||
if mappings[0].share_proto == fields.ShareMappingProto.CEPHFS:
|
||||
self._reconcile_cephfs_rules(
|
||||
context, share_id, mappings, instances, rules)
|
||||
else:
|
||||
self._reconcile_nfs_rules(
|
||||
context, share_id, mappings, instances, rules)
|
||||
|
||||
_locked()
|
||||
|
||||
def _reconcile_cephfs_rules(
|
||||
self, context, share_id, mappings, instances, rules
|
||||
):
|
||||
legit = {
|
||||
objects.ShareMapping._cephx_identity(
|
||||
mapping.instance_uuid, instance.host)
|
||||
for mapping, instance in zip(mappings, instances)
|
||||
}
|
||||
for rule in rules:
|
||||
# Only nova-managed per-instance identities are drained. Legacy
|
||||
# shared 'nova' grants and any non-nova cephx identity are left
|
||||
# untouched (operators drain those via the OSSN).
|
||||
if rule.access_type != 'cephx':
|
||||
continue
|
||||
if not rule.access_to.startswith('nova-'):
|
||||
continue
|
||||
if rule.access_to in legit:
|
||||
continue
|
||||
# Skip rules Manila is already processing to avoid noisy
|
||||
# repeated deny calls that generate no useful state change.
|
||||
if rule.state in ('queued_to_deny', 'denying'):
|
||||
continue
|
||||
self._revoke(context, share_id, 'cephx', rule.access_to)
|
||||
|
||||
def _reconcile_nfs_rules(
|
||||
self, context, share_id, mappings, instances, rules
|
||||
):
|
||||
# A host can only recognize its own NFS ip rule, and drains it only
|
||||
# once it no longer serves any instance on the share.
|
||||
if CONF.host in {instance.host for instance in instances}:
|
||||
return
|
||||
stale = [
|
||||
rule for rule in rules
|
||||
if rule.access_type == 'ip' and
|
||||
rule.access_to == CONF.my_shared_fs_storage_ip
|
||||
]
|
||||
if not stale:
|
||||
return
|
||||
# Never revoke while a hard NFS mount is still present on this host:
|
||||
# dropping access under a hard mount wedges I/O in uninterruptible
|
||||
# sleep and the umount itself then hangs. Leave it for the mount
|
||||
# cleanup follow-up, which unmounts first.
|
||||
if self.driver.is_share_mounted(mappings[0]):
|
||||
LOG.warning(
|
||||
"Stale NFS access rule for share %s is still mounted on this "
|
||||
"host; skipping revoke to avoid wedging a hard mount",
|
||||
share_id)
|
||||
return
|
||||
for rule in stale:
|
||||
# Skip rules Manila is already processing to avoid noisy
|
||||
# repeated deny calls that generate no useful state change.
|
||||
if rule.state not in ('queued_to_deny', 'denying'):
|
||||
self._revoke(context, share_id, 'ip', rule.access_to)
|
||||
|
||||
def _revoke(self, context, share_id, access_type, access_to):
|
||||
try:
|
||||
self.manila_api.deny(context, share_id, access_type, access_to)
|
||||
LOG.info(
|
||||
"Revoked stale Manila access rule '%s' (%s) on share %s",
|
||||
access_to, access_type, share_id)
|
||||
except (
|
||||
exception.ShareNotFound,
|
||||
exception.ShareAccessNotFound,
|
||||
):
|
||||
# Already gone (e.g. another host raced us). Nothing to do.
|
||||
pass
|
||||
except Exception:
|
||||
LOG.exception(
|
||||
"Failed to revoke stale access rule '%s' on share %s",
|
||||
access_to, share_id)
|
||||
|
||||
@staticmethod
|
||||
def _instance_in_transit(instance):
|
||||
# A migrating or resizing instance legitimately holds an access
|
||||
# rule on a host it is not currently "on" (the source grant
|
||||
# persists until confirm/revert). RESIZED covers the finished but
|
||||
# not-yet-confirmed window where task_state is already None.
|
||||
return (
|
||||
instance.task_state is not None or
|
||||
instance.vm_state == vm_states.RESIZED
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def set_mapping_status(share_mapping, status):
|
||||
share_mapping.status = status
|
||||
|
||||
@@ -1308,6 +1308,26 @@ Possible values:
|
||||
* 0: Will run at the default periodic interval.
|
||||
* Any value < 0: Disables the option.
|
||||
* Any positive integer in seconds.
|
||||
"""),
|
||||
cfg.IntOpt('stale_share_access_reconcile_interval',
|
||||
default=1800,
|
||||
help="""
|
||||
Interval for reconciling stale Manila share access rules.
|
||||
|
||||
Each compute periodically scans the cell's share mappings and revokes
|
||||
Manila access rules left behind when a cold migration or resize
|
||||
confirm/revert failed to clean them up (for example because manila-api
|
||||
was unreachable). A start-up reconcile always runs during host init;
|
||||
this option only controls the periodic task.
|
||||
|
||||
The scan is cell-wide, so in large deployments you may want to raise
|
||||
this interval to reduce load on the Manila API.
|
||||
|
||||
Possible values:
|
||||
|
||||
* Any positive integer in seconds.
|
||||
* 0 or any negative value disables the periodic task (the start-up
|
||||
reconcile still runs).
|
||||
""")
|
||||
]
|
||||
|
||||
|
||||
@@ -4907,6 +4907,14 @@ def share_mapping_get_by_instance_uuid(context, instance_uuid):
|
||||
filter_by(instance_uuid=instance_uuid).all()
|
||||
|
||||
|
||||
@require_context
|
||||
@pick_context_manager_reader
|
||||
def share_mapping_get_by_instance_uuids(context, instance_uuids):
|
||||
"""Get share_mapping records for a list of instances."""
|
||||
return context.session.query(models.ShareMapping).\
|
||||
filter(models.ShareMapping.instance_uuid.in_(instance_uuids)).all()
|
||||
|
||||
|
||||
@require_context
|
||||
@pick_context_manager_reader
|
||||
def share_mapping_get_by_instance_uuid_and_share_id(
|
||||
|
||||
@@ -17,6 +17,7 @@ import os
|
||||
import time
|
||||
|
||||
from oslo_log import log as logging
|
||||
import psutil
|
||||
|
||||
from nova import exception
|
||||
|
||||
@@ -81,6 +82,19 @@ def read_sys(path: str) -> str:
|
||||
raise exception.FileNotFound(file_path=path) from exc
|
||||
|
||||
|
||||
def is_mounted(mountpoint: str) -> bool:
|
||||
"""Return whether ``mountpoint`` is a mounted filesystem.
|
||||
|
||||
Uses psutil to read the mount table rather than os.path.ismount(), which
|
||||
stats the mountpoint and can hang on a wedged NFS mount. psutil parses
|
||||
the table without traversing the mounts, so it never blocks, and it works
|
||||
on platforms without procfs (e.g. macOS) so tests need not fake it.
|
||||
"""
|
||||
return any(
|
||||
part.mountpoint == mountpoint
|
||||
for part in psutil.disk_partitions(all=True))
|
||||
|
||||
|
||||
# NOTE(bauzas): this method is deliberately not wrapped in a privsep entrypoint
|
||||
# In order to correctly use it, you need to decorate the caller with a specific
|
||||
# privsep entrypoint.
|
||||
|
||||
@@ -167,22 +167,26 @@ class ShareMapping(base.NovaTimestampObject, base.NovaObject):
|
||||
)
|
||||
self.access_key = access.access_key
|
||||
|
||||
@staticmethod
|
||||
def _cephx_identity(instance_uuid, host):
|
||||
# Per-instance, per-host cephx identity. instance_uuid gives
|
||||
# per-instance isolation (a leaked key unlocks a single
|
||||
# instance's shares, not every CephFS share in the cloud); host
|
||||
# scopes the grant to a single host so a migration revoke on the
|
||||
# source does not break the grant on the destination.
|
||||
digest = hashlib.sha256(
|
||||
f'{instance_uuid}:{host}'.encode()
|
||||
).hexdigest()
|
||||
return 'nova-' + digest[:16]
|
||||
|
||||
def set_access_according_to_protocol(self):
|
||||
if self.share_proto == fields.ShareMappingProto.NFS:
|
||||
self.access_type = 'ip'
|
||||
self.access_to = CONF.my_shared_fs_storage_ip
|
||||
elif self.share_proto == fields.ShareMappingProto.CEPHFS:
|
||||
self.access_type = 'cephx'
|
||||
# Per-instance, per-host cephx identity. instance_uuid gives
|
||||
# per-instance isolation (a leaked key unlocks a single
|
||||
# instance's shares, not every CephFS share in the cloud);
|
||||
# CONF.host scopes the grant to this host so a migration
|
||||
# revoke on the source does not break the grant on the
|
||||
# destination.
|
||||
digest = hashlib.sha256(
|
||||
f'{self.instance_uuid}:{CONF.host}'.encode()
|
||||
).hexdigest()
|
||||
self.access_to = 'nova-' + digest[:16]
|
||||
self.access_to = self._cephx_identity(
|
||||
self.instance_uuid, CONF.host)
|
||||
else:
|
||||
raise exception.ShareProtocolNotSupported(
|
||||
share_proto=self.share_proto
|
||||
@@ -192,7 +196,8 @@ class ShareMapping(base.NovaTimestampObject, base.NovaObject):
|
||||
@base.NovaObjectRegistry.register
|
||||
class ShareMappingList(base.ObjectListBase, base.NovaObject):
|
||||
# Version 1.0: Initial version
|
||||
VERSION = '1.0'
|
||||
# Version 1.1: Add get_by_host_for_reconcile()
|
||||
VERSION = '1.1'
|
||||
fields = {
|
||||
'objects': fields.ListOfObjectsField('ShareMapping'),
|
||||
}
|
||||
@@ -204,6 +209,32 @@ class ShareMappingList(base.ObjectListBase, base.NovaObject):
|
||||
context, instance_uuid)
|
||||
return base.obj_make_list(context, cls(context), db_share_mappings)
|
||||
|
||||
@classmethod
|
||||
@base.remotable
|
||||
def get_by_host_for_reconcile(cls, context, host):
|
||||
"""Share mappings whose access rules may be stale on ``host``.
|
||||
|
||||
Gathered entirely conductor-side so reconcile costs a single
|
||||
compute->conductor round-trip instead of one query per instance and
|
||||
per migration. Includes every instance currently on ``host``, plus
|
||||
every instance whose migration touched ``host`` as source or
|
||||
destination (a failed cleanup there can leave a grant behind).
|
||||
Over-including is harmless: the caller revalidates each share under a
|
||||
lock before revoking anything.
|
||||
"""
|
||||
instance_uuids = set(
|
||||
db.instance_get_all_uuids_by_hosts(context, [host])[host])
|
||||
for migration in db.migration_get_all_by_filters(
|
||||
context,
|
||||
{'host': host,
|
||||
'status': ['confirmed', 'reverted', 'error']}):
|
||||
instance_uuids.add(migration.instance_uuid)
|
||||
if not instance_uuids:
|
||||
return base.obj_make_list(context, cls(context), [])
|
||||
db_share_mappings = db.share_mapping_get_by_instance_uuids(
|
||||
context, list(instance_uuids))
|
||||
return base.obj_make_list(context, cls(context), db_share_mappings)
|
||||
|
||||
@classmethod
|
||||
@base.remotable
|
||||
def get_by_share_id(cls, context, share_id):
|
||||
|
||||
@@ -251,6 +251,13 @@ class API(object):
|
||||
return Access.from_manila_access(access)
|
||||
return None
|
||||
|
||||
@translate_share_exception
|
||||
def get_access_rules(self, context, share_id):
|
||||
LOG.debug("Get all access rules for share id:'%s'", share_id)
|
||||
access_list = _manilaclient(
|
||||
context, admin=True).access_rules(share_id)
|
||||
return [Access.from_manila_access(access) for access in access_list]
|
||||
|
||||
@translate_allow_exception
|
||||
def allow(
|
||||
self,
|
||||
|
||||
Vendored
+12
@@ -72,6 +72,9 @@ class ManilaFixture(fixtures.Fixture):
|
||||
self.mock_deny = self.useFixture(fixtures.MockPatch(
|
||||
'nova.share.manila.API.deny',
|
||||
side_effect=self.fake_deny)).mock
|
||||
self.mock_get_access_rules = self.useFixture(fixtures.MockPatch(
|
||||
'nova.share.manila.API.get_access_rules',
|
||||
side_effect=self.fake_get_access_rules)).mock
|
||||
|
||||
def fake_get(self, context, share_id):
|
||||
manila_share = ManilaShare(share_id)
|
||||
@@ -130,3 +133,12 @@ class ManilaFixture(fixtures.Fixture):
|
||||
def fake_deny(self, context, share_id, access_type, access_to):
|
||||
self.share_access.discard((share_id, access_type, access_to))
|
||||
return 202
|
||||
|
||||
def fake_get_access_rules(self, context, share_id):
|
||||
rules = []
|
||||
for s_id, access_type, access_to in self.share_access:
|
||||
if s_id != share_id:
|
||||
continue
|
||||
access = ManilaAccess(access_type=access_type, access_to=access_to)
|
||||
rules.append(nova.share.manila.Access.from_manila_access(access))
|
||||
return rules
|
||||
@@ -44,6 +44,7 @@ class ServerSharesTestBase(base.ServersTestBase):
|
||||
|
||||
self.context = nova_context.get_admin_context()
|
||||
self.manila_fixture = self.useFixture(nova_fixtures.ManilaFixture())
|
||||
self.flags(auth_type='password', group='manila')
|
||||
self.flags(ram_allocation_ratio=1.0)
|
||||
self.flags(file_backed_memory=8192, group='libvirt')
|
||||
self.flags(reserved_host_memory_mb=0)
|
||||
@@ -274,6 +275,27 @@ class ServerSharesTest(ServerSharesTestBase):
|
||||
self._detach_share(server1, share_id)
|
||||
self.assertEqual({id2}, self._cephfs_grants(share_id))
|
||||
|
||||
def test_reconcile_removes_stale_cephfs_grant(self):
|
||||
server, share_id = self.test_server_cephfs_share_metadata()
|
||||
live = self._cephfs_grants(share_id)
|
||||
self.assertEqual(1, len(live))
|
||||
live_id = live.pop()
|
||||
|
||||
# Seed a stale orphan identity (as if a cold migration confirm
|
||||
# failed to revoke the source host's grant) and a legacy shared
|
||||
# 'nova' grant (drained only by operators, never by reconcile).
|
||||
self.manila_fixture.share_access.add(
|
||||
(share_id, 'cephx', 'nova-deadbeefdeadbeef'))
|
||||
self.manila_fixture.share_access.add((share_id, 'cephx', 'nova'))
|
||||
|
||||
self.computes[self.compute].manager._reconcile_stale_share_access(
|
||||
self.context)
|
||||
|
||||
grants = self._cephfs_grants(share_id)
|
||||
self.assertNotIn('nova-deadbeefdeadbeef', grants)
|
||||
self.assertIn(live_id, grants)
|
||||
self.assertIn('nova', grants)
|
||||
|
||||
def test_server_share_after_hard_reboot(self):
|
||||
"""Verify that share is still available after a reboot"""
|
||||
server, share_id = self.test_server_share_metadata()
|
||||
|
||||
@@ -1248,6 +1248,62 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase,
|
||||
mock.call.list_instance_uuids(),
|
||||
self.compute.driver.mock_calls[1])
|
||||
|
||||
def _init_host_scaffold(self, mock_get_nodes):
|
||||
mock_get_nodes.return_value = {
|
||||
uuids.cn_uuid1: objects.ComputeNode(
|
||||
uuid=uuids.cn_uuid1, hypervisor_hostname='node1',
|
||||
host=self.compute.host)}
|
||||
self.compute.driver = mock.Mock()
|
||||
self.compute.driver.capabilities = {'supports_evacuate': False}
|
||||
self.compute.driver.process_instances_at_startup.side_effect = (
|
||||
lambda context, instances: instances)
|
||||
self.compute.driver.list_instance_uuids.return_value = []
|
||||
|
||||
@mock.patch('nova.compute.manager.ComputeManager._get_nodes')
|
||||
@mock.patch('nova.compute.manager.ComputeManager.'
|
||||
'_error_out_instances_whose_build_was_interrupted')
|
||||
@mock.patch('nova.objects.InstanceList.get_by_host',
|
||||
return_value=objects.InstanceList())
|
||||
@mock.patch('nova.compute.manager.ComputeManager._init_instance',
|
||||
mock.NonCallableMock())
|
||||
@mock.patch('nova.compute.manager.ComputeManager.'
|
||||
'_update_scheduler_instance_info', mock.NonCallableMock())
|
||||
@mock.patch('nova.compute.share_management.ShareManager.'
|
||||
'reconcile_stale_share_access')
|
||||
def test_init_host_reconciles_stale_share_access(
|
||||
self, mock_reconcile, mock_get_by_host, mock_error_interrupted,
|
||||
mock_get_nodes):
|
||||
self._init_host_scaffold(mock_get_nodes)
|
||||
|
||||
self.compute.init_host(None)
|
||||
|
||||
# init_host kicks off a reconcile pass; the pass gathers its own
|
||||
# candidate shares conductor-side.
|
||||
mock_reconcile.assert_called_once_with(
|
||||
test.MatchType(context.RequestContext))
|
||||
|
||||
@mock.patch('nova.compute.manager.ComputeManager._get_nodes')
|
||||
@mock.patch('nova.compute.manager.ComputeManager.'
|
||||
'_error_out_instances_whose_build_was_interrupted')
|
||||
@mock.patch('nova.objects.InstanceList.get_by_host',
|
||||
return_value=objects.InstanceList())
|
||||
@mock.patch('nova.compute.manager.ComputeManager._init_instance',
|
||||
mock.NonCallableMock())
|
||||
@mock.patch('nova.compute.manager.ComputeManager.'
|
||||
'_update_scheduler_instance_info', mock.NonCallableMock())
|
||||
@mock.patch('nova.compute.share_management.ShareManager.'
|
||||
'reconcile_stale_share_access',
|
||||
side_effect=Exception('manila down'))
|
||||
def test_init_host_reconcile_failure_non_fatal(
|
||||
self, mock_reconcile, mock_get_by_host, mock_error_interrupted,
|
||||
mock_get_nodes):
|
||||
self._init_host_scaffold(mock_get_nodes)
|
||||
|
||||
# A failing reconcile must not prevent the host from coming up.
|
||||
self.compute.init_host(None)
|
||||
|
||||
mock_reconcile.assert_called_once()
|
||||
|
||||
def test_init_host_new_with_instances(self):
|
||||
"""Tests the case where we start up without an existing service_ref,
|
||||
indicating that we are a new service, but our hypervisor reports
|
||||
@@ -4264,6 +4320,603 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase,
|
||||
fields.ShareMappingStatus.ERROR)
|
||||
self.assertEqual(instance.vm_state, vm_states.ACTIVE)
|
||||
|
||||
def _fake_access_rule(self, access_type, access_to):
|
||||
return nova.share.manila.Access.from_dict({
|
||||
"access_level": "rw",
|
||||
"state": "active",
|
||||
"id": uuids.access_id,
|
||||
"access_type": access_type,
|
||||
"access_to": access_to,
|
||||
"access_key": None,
|
||||
})
|
||||
|
||||
# Helper to set up the standard reconcile mocks.
|
||||
# get_by_host_for_reconcile returns the share mappings for the whole
|
||||
# candidate set in one conductor-side query; get_by_share_id is used
|
||||
# inside _reconcile_share for the re-read-under-lock.
|
||||
def _setup_reconcile_mocks(
|
||||
self,
|
||||
mock_get_for_reconcile,
|
||||
mock_get_by_share_id,
|
||||
instances,
|
||||
share_mappings,
|
||||
):
|
||||
mock_get_for_reconcile.return_value = objects.ShareMappingList(
|
||||
objects=share_mappings)
|
||||
mock_get_by_share_id.return_value = objects.ShareMappingList(
|
||||
objects=share_mappings)
|
||||
|
||||
@mock.patch('nova.share.manila.API.deny')
|
||||
@mock.patch('nova.share.manila.API.get_access_rules')
|
||||
@mock.patch('nova.objects.InstanceList.get_by_filters')
|
||||
@mock.patch('nova.objects.share_mapping.ShareMappingList.get_by_share_id')
|
||||
@mock.patch(
|
||||
'nova.objects.share_mapping.ShareMappingList.'
|
||||
'get_by_host_for_reconcile')
|
||||
def test_reconcile_stale_share_access_cephfs_revokes_orphan(
|
||||
self, mock_get_for_reconcile,
|
||||
mock_get_by_share_id, mock_get_by_filters,
|
||||
mock_get_rules, mock_deny
|
||||
):
|
||||
self.flags(auth_type='password', group='manila')
|
||||
share_mapping = self.get_fake_share_mapping_cephfs()
|
||||
instance = fake_instance.fake_instance_obj(
|
||||
self.context, uuid=uuids.instance, host='dest-host',
|
||||
vm_state=vm_states.ACTIVE, task_state=None)
|
||||
self._setup_reconcile_mocks(
|
||||
mock_get_for_reconcile,
|
||||
mock_get_by_share_id, [instance], [share_mapping])
|
||||
mock_get_by_filters.return_value = objects.InstanceList(
|
||||
objects=[instance])
|
||||
live_id = objects.ShareMapping._cephx_identity(
|
||||
uuids.instance, 'dest-host')
|
||||
mock_get_rules.return_value = [
|
||||
self._fake_access_rule('cephx', live_id),
|
||||
self._fake_access_rule('cephx', 'nova-deadbeefdeadbeef'),
|
||||
self._fake_access_rule('cephx', 'nova'),
|
||||
self._fake_access_rule('cephx', 'client.someoneelse'),
|
||||
self._fake_access_rule('ip', '192.168.0.1'),
|
||||
]
|
||||
|
||||
self.compute.share_manager.reconcile_stale_share_access(self.context)
|
||||
|
||||
mock_deny.assert_called_once_with(
|
||||
self.context, share_mapping.share_id, 'cephx',
|
||||
'nova-deadbeefdeadbeef')
|
||||
|
||||
@mock.patch('nova.share.manila.API.deny')
|
||||
@mock.patch('nova.share.manila.API.get_access_rules')
|
||||
@mock.patch('nova.objects.InstanceList.get_by_filters')
|
||||
@mock.patch('nova.objects.share_mapping.ShareMappingList.get_by_share_id')
|
||||
@mock.patch(
|
||||
'nova.objects.share_mapping.ShareMappingList.'
|
||||
'get_by_host_for_reconcile')
|
||||
def test_reconcile_stale_share_access_cephfs_skips_queued_to_deny(
|
||||
self, mock_get_for_reconcile,
|
||||
mock_get_by_share_id, mock_get_by_filters,
|
||||
mock_get_rules, mock_deny
|
||||
):
|
||||
# An orphan cephx identity already in queued_to_deny/denying must not
|
||||
# trigger a second deny call - Manila is already processing it.
|
||||
self.flags(auth_type='password', group='manila')
|
||||
share_mapping = self.get_fake_share_mapping_cephfs()
|
||||
instance = fake_instance.fake_instance_obj(
|
||||
self.context, uuid=uuids.instance, host='dest-host',
|
||||
vm_state=vm_states.ACTIVE, task_state=None)
|
||||
self._setup_reconcile_mocks(
|
||||
mock_get_for_reconcile,
|
||||
mock_get_by_share_id, [instance], [share_mapping])
|
||||
mock_get_by_filters.return_value = objects.InstanceList(
|
||||
objects=[instance])
|
||||
live_id = objects.ShareMapping._cephx_identity(
|
||||
uuids.instance, 'dest-host')
|
||||
mock_get_rules.return_value = [
|
||||
self._fake_access_rule('cephx', live_id),
|
||||
nova.share.manila.Access.from_dict({
|
||||
"access_level": "rw",
|
||||
"state": "queued_to_deny",
|
||||
"id": uuids.access_id,
|
||||
"access_type": "cephx",
|
||||
"access_to": "nova-deadbeefdeadbeef",
|
||||
"access_key": None,
|
||||
}),
|
||||
]
|
||||
|
||||
self.compute.share_manager.reconcile_stale_share_access(self.context)
|
||||
|
||||
mock_deny.assert_not_called()
|
||||
|
||||
@mock.patch('nova.share.manila.API.deny')
|
||||
@mock.patch('nova.share.manila.API.get_access_rules')
|
||||
@mock.patch('nova.objects.InstanceList.get_by_filters')
|
||||
@mock.patch('nova.objects.share_mapping.ShareMappingList.get_by_share_id')
|
||||
@mock.patch(
|
||||
'nova.objects.share_mapping.ShareMappingList.'
|
||||
'get_by_host_for_reconcile')
|
||||
def test_reconcile_stale_share_access_nfs_revokes_own_ip(
|
||||
self, mock_get_for_reconcile,
|
||||
mock_get_by_share_id, mock_get_by_filters,
|
||||
mock_get_rules, mock_deny
|
||||
):
|
||||
self.flags(host='source-host')
|
||||
self.flags(auth_type='password', group='manila')
|
||||
self.flags(my_shared_fs_storage_ip='192.168.0.1')
|
||||
share_mapping = self.get_fake_share_mapping()
|
||||
instance = fake_instance.fake_instance_obj(
|
||||
self.context, uuid=uuids.instance, host='dest-host',
|
||||
vm_state=vm_states.ACTIVE, task_state=None)
|
||||
self._setup_reconcile_mocks(
|
||||
mock_get_for_reconcile,
|
||||
mock_get_by_share_id, [instance], [share_mapping])
|
||||
mock_get_by_filters.return_value = objects.InstanceList(
|
||||
objects=[instance])
|
||||
mock_get_rules.return_value = [
|
||||
self._fake_access_rule('ip', '192.168.0.1'),
|
||||
self._fake_access_rule('ip', '192.168.9.9'),
|
||||
self._fake_access_rule('cephx', 'nova-deadbeef'),
|
||||
]
|
||||
|
||||
self.compute.share_manager.reconcile_stale_share_access(self.context)
|
||||
|
||||
mock_deny.assert_called_once_with(
|
||||
self.context, share_mapping.share_id, 'ip', '192.168.0.1')
|
||||
|
||||
@mock.patch('nova.share.manila.API.deny')
|
||||
@mock.patch('nova.share.manila.API.get_access_rules')
|
||||
@mock.patch('nova.objects.InstanceList.get_by_filters')
|
||||
@mock.patch('nova.objects.share_mapping.ShareMappingList.get_by_share_id')
|
||||
@mock.patch(
|
||||
'nova.objects.share_mapping.ShareMappingList.'
|
||||
'get_by_host_for_reconcile')
|
||||
def test_reconcile_stale_share_access_nfs_keeps_ip_when_serving(
|
||||
self, mock_get_for_reconcile,
|
||||
mock_get_by_share_id, mock_get_by_filters,
|
||||
mock_get_rules, mock_deny
|
||||
):
|
||||
self.flags(host='this-host')
|
||||
self.flags(auth_type='password', group='manila')
|
||||
self.flags(my_shared_fs_storage_ip='192.168.0.1')
|
||||
share_mapping = self.get_fake_share_mapping()
|
||||
instance = fake_instance.fake_instance_obj(
|
||||
self.context, uuid=uuids.instance, host='this-host',
|
||||
vm_state=vm_states.ACTIVE, task_state=None)
|
||||
self._setup_reconcile_mocks(
|
||||
mock_get_for_reconcile,
|
||||
mock_get_by_share_id, [instance], [share_mapping])
|
||||
mock_get_by_filters.return_value = objects.InstanceList(
|
||||
objects=[instance])
|
||||
mock_get_rules.return_value = [
|
||||
self._fake_access_rule('ip', '192.168.0.1'),
|
||||
]
|
||||
|
||||
self.compute.share_manager.reconcile_stale_share_access(self.context)
|
||||
|
||||
mock_deny.assert_not_called()
|
||||
|
||||
@mock.patch('nova.share.manila.API.deny')
|
||||
@mock.patch('nova.share.manila.API.get_access_rules')
|
||||
@mock.patch('nova.objects.InstanceList.get_by_filters')
|
||||
@mock.patch('nova.objects.share_mapping.ShareMappingList.get_by_share_id')
|
||||
@mock.patch(
|
||||
'nova.objects.share_mapping.ShareMappingList.'
|
||||
'get_by_host_for_reconcile')
|
||||
def test_reconcile_stale_share_access_nfs_leaves_other_host_ip(
|
||||
self, mock_get_for_reconcile,
|
||||
mock_get_by_share_id, mock_get_by_filters,
|
||||
mock_get_rules, mock_deny
|
||||
):
|
||||
self.flags(host='this-host')
|
||||
self.flags(auth_type='password', group='manila')
|
||||
self.flags(my_shared_fs_storage_ip='192.168.0.1')
|
||||
share_mapping = self.get_fake_share_mapping()
|
||||
instance = fake_instance.fake_instance_obj(
|
||||
self.context, uuid=uuids.instance, host='dest-host',
|
||||
vm_state=vm_states.ACTIVE, task_state=None)
|
||||
self._setup_reconcile_mocks(
|
||||
mock_get_for_reconcile,
|
||||
mock_get_by_share_id, [instance], [share_mapping])
|
||||
mock_get_by_filters.return_value = objects.InstanceList(
|
||||
objects=[instance])
|
||||
mock_get_rules.return_value = [
|
||||
self._fake_access_rule('ip', '192.168.9.9'),
|
||||
]
|
||||
|
||||
self.compute.share_manager.reconcile_stale_share_access(self.context)
|
||||
|
||||
mock_deny.assert_not_called()
|
||||
|
||||
@mock.patch('nova.share.manila.API.deny')
|
||||
@mock.patch('nova.share.manila.API.get_access_rules')
|
||||
@mock.patch('nova.objects.InstanceList.get_by_filters')
|
||||
@mock.patch('nova.objects.share_mapping.ShareMappingList.get_by_share_id')
|
||||
@mock.patch(
|
||||
'nova.objects.share_mapping.ShareMappingList.'
|
||||
'get_by_host_for_reconcile')
|
||||
def test_reconcile_stale_share_access_nfs_skips_when_mounted(
|
||||
self, mock_get_for_reconcile,
|
||||
mock_get_by_share_id, mock_get_by_filters,
|
||||
mock_get_rules, mock_deny
|
||||
):
|
||||
self.flags(host='source-host')
|
||||
self.flags(auth_type='password', group='manila')
|
||||
self.flags(my_shared_fs_storage_ip='192.168.0.1')
|
||||
share_mapping = self.get_fake_share_mapping()
|
||||
instance = fake_instance.fake_instance_obj(
|
||||
self.context, uuid=uuids.instance, host='dest-host',
|
||||
vm_state=vm_states.ACTIVE, task_state=None)
|
||||
self._setup_reconcile_mocks(
|
||||
mock_get_for_reconcile,
|
||||
mock_get_by_share_id, [instance], [share_mapping])
|
||||
mock_get_by_filters.return_value = objects.InstanceList(
|
||||
objects=[instance])
|
||||
mock_get_rules.return_value = [
|
||||
self._fake_access_rule('ip', '192.168.0.1'),
|
||||
]
|
||||
|
||||
with mock.patch.object(
|
||||
self.compute.share_manager.driver, 'is_share_mounted',
|
||||
return_value=True
|
||||
) as mock_mounted:
|
||||
self.compute.share_manager.reconcile_stale_share_access(
|
||||
self.context)
|
||||
|
||||
# Still mounted here, so the ip rule is left in place.
|
||||
mock_deny.assert_not_called()
|
||||
mock_mounted.assert_called_once_with(share_mapping)
|
||||
|
||||
@mock.patch('nova.share.manila.API.deny')
|
||||
@mock.patch('nova.share.manila.API.get_access_rules')
|
||||
@mock.patch('nova.objects.InstanceList.get_by_filters')
|
||||
@mock.patch('nova.objects.share_mapping.ShareMappingList.get_by_share_id')
|
||||
@mock.patch(
|
||||
'nova.objects.share_mapping.ShareMappingList.'
|
||||
'get_by_host_for_reconcile')
|
||||
def test_reconcile_stale_share_access_nfs_skips_queued_to_deny(
|
||||
self, mock_get_for_reconcile,
|
||||
mock_get_by_share_id, mock_get_by_filters,
|
||||
mock_get_rules, mock_deny
|
||||
):
|
||||
# Rules already in queued_to_deny/denying must not trigger a second
|
||||
# deny call — Manila is already processing them.
|
||||
self.flags(host='source-host')
|
||||
self.flags(auth_type='password', group='manila')
|
||||
self.flags(my_shared_fs_storage_ip='192.168.0.1')
|
||||
share_mapping = self.get_fake_share_mapping()
|
||||
instance = fake_instance.fake_instance_obj(
|
||||
self.context, uuid=uuids.instance, host='dest-host',
|
||||
vm_state=vm_states.ACTIVE, task_state=None)
|
||||
self._setup_reconcile_mocks(
|
||||
mock_get_for_reconcile,
|
||||
mock_get_by_share_id, [instance], [share_mapping])
|
||||
mock_get_by_filters.return_value = objects.InstanceList(
|
||||
objects=[instance])
|
||||
mock_get_rules.return_value = [
|
||||
nova.share.manila.Access.from_dict({
|
||||
"access_level": "rw",
|
||||
"state": "queued_to_deny",
|
||||
"id": uuids.access_id,
|
||||
"access_type": "ip",
|
||||
"access_to": "192.168.0.1",
|
||||
"access_key": None,
|
||||
}),
|
||||
]
|
||||
|
||||
self.compute.share_manager.reconcile_stale_share_access(self.context)
|
||||
|
||||
mock_deny.assert_not_called()
|
||||
|
||||
@mock.patch('nova.share.manila.API.deny')
|
||||
@mock.patch('nova.share.manila.API.get_access_rules')
|
||||
@mock.patch('nova.objects.InstanceList.get_by_filters')
|
||||
@mock.patch('nova.objects.share_mapping.ShareMappingList.get_by_share_id')
|
||||
@mock.patch(
|
||||
'nova.objects.share_mapping.ShareMappingList.'
|
||||
'get_by_host_for_reconcile')
|
||||
def test_reconcile_stale_share_access_skips_in_transit_task_state(
|
||||
self, mock_get_for_reconcile,
|
||||
mock_get_by_share_id, mock_get_by_filters,
|
||||
mock_get_rules, mock_deny
|
||||
):
|
||||
self.flags(auth_type='password', group='manila')
|
||||
share_mapping = self.get_fake_share_mapping_cephfs()
|
||||
instance = fake_instance.fake_instance_obj(
|
||||
self.context, uuid=uuids.instance, host='dest-host',
|
||||
vm_state=vm_states.ACTIVE,
|
||||
task_state=task_states.RESIZE_MIGRATING)
|
||||
self._setup_reconcile_mocks(
|
||||
mock_get_for_reconcile,
|
||||
mock_get_by_share_id, [instance], [share_mapping])
|
||||
mock_get_by_filters.return_value = objects.InstanceList(
|
||||
objects=[instance])
|
||||
|
||||
self.compute.share_manager.reconcile_stale_share_access(self.context)
|
||||
|
||||
mock_get_rules.assert_not_called()
|
||||
mock_deny.assert_not_called()
|
||||
|
||||
@mock.patch('nova.share.manila.API.deny')
|
||||
@mock.patch('nova.share.manila.API.get_access_rules')
|
||||
@mock.patch('nova.objects.InstanceList.get_by_filters')
|
||||
@mock.patch('nova.objects.share_mapping.ShareMappingList.get_by_share_id')
|
||||
@mock.patch(
|
||||
'nova.objects.share_mapping.ShareMappingList.'
|
||||
'get_by_host_for_reconcile')
|
||||
def test_reconcile_stale_share_access_skips_in_transit_resized(
|
||||
self, mock_get_for_reconcile,
|
||||
mock_get_by_share_id, mock_get_by_filters,
|
||||
mock_get_rules, mock_deny
|
||||
):
|
||||
self.flags(auth_type='password', group='manila')
|
||||
share_mapping = self.get_fake_share_mapping_cephfs()
|
||||
instance = fake_instance.fake_instance_obj(
|
||||
self.context, uuid=uuids.instance, host='dest-host',
|
||||
vm_state=vm_states.RESIZED, task_state=None)
|
||||
self._setup_reconcile_mocks(
|
||||
mock_get_for_reconcile,
|
||||
mock_get_by_share_id, [instance], [share_mapping])
|
||||
mock_get_by_filters.return_value = objects.InstanceList(
|
||||
objects=[instance])
|
||||
|
||||
self.compute.share_manager.reconcile_stale_share_access(self.context)
|
||||
|
||||
mock_get_rules.assert_not_called()
|
||||
mock_deny.assert_not_called()
|
||||
|
||||
@mock.patch('nova.share.manila.API.deny')
|
||||
@mock.patch('nova.share.manila.API.get_access_rules')
|
||||
@mock.patch('nova.objects.InstanceList.get_by_filters')
|
||||
@mock.patch('nova.objects.share_mapping.ShareMappingList.get_by_share_id')
|
||||
@mock.patch(
|
||||
'nova.objects.share_mapping.ShareMappingList.'
|
||||
'get_by_host_for_reconcile')
|
||||
def test_reconcile_stale_share_access_skips_missing_instance(
|
||||
self, mock_get_for_reconcile,
|
||||
mock_get_by_share_id, mock_get_by_filters,
|
||||
mock_get_rules, mock_deny
|
||||
):
|
||||
self.flags(auth_type='password', group='manila')
|
||||
share_mapping = self.get_fake_share_mapping_cephfs()
|
||||
instance = fake_instance.fake_instance_obj(
|
||||
self.context, uuid=uuids.instance, host='dest-host',
|
||||
vm_state=vm_states.ACTIVE, task_state=None)
|
||||
self._setup_reconcile_mocks(
|
||||
mock_get_for_reconcile,
|
||||
mock_get_by_share_id, [instance], [share_mapping])
|
||||
# _reconcile_share re-reads instances; simulate missing instance
|
||||
mock_get_by_filters.return_value = objects.InstanceList(objects=[])
|
||||
|
||||
self.compute.share_manager.reconcile_stale_share_access(self.context)
|
||||
|
||||
mock_get_rules.assert_not_called()
|
||||
mock_deny.assert_not_called()
|
||||
|
||||
@mock.patch('nova.share.manila.API.deny')
|
||||
@mock.patch('nova.share.manila.API.get_access_rules')
|
||||
@mock.patch('nova.objects.InstanceList.get_by_filters')
|
||||
@mock.patch('nova.objects.share_mapping.ShareMappingList.get_by_share_id')
|
||||
@mock.patch(
|
||||
'nova.objects.share_mapping.ShareMappingList.'
|
||||
'get_by_host_for_reconcile')
|
||||
def test_reconcile_stale_share_access_deny_error_swallowed(
|
||||
self, mock_get_for_reconcile,
|
||||
mock_get_by_share_id, mock_get_by_filters,
|
||||
mock_get_rules, mock_deny
|
||||
):
|
||||
self.flags(auth_type='password', group='manila')
|
||||
share_mapping = self.get_fake_share_mapping_cephfs()
|
||||
instance = fake_instance.fake_instance_obj(
|
||||
self.context, uuid=uuids.instance, host='dest-host',
|
||||
vm_state=vm_states.ACTIVE, task_state=None)
|
||||
self._setup_reconcile_mocks(
|
||||
mock_get_for_reconcile,
|
||||
mock_get_by_share_id, [instance], [share_mapping])
|
||||
mock_get_by_filters.return_value = objects.InstanceList(
|
||||
objects=[instance])
|
||||
mock_get_rules.return_value = [
|
||||
self._fake_access_rule('cephx', 'nova-deadbeefdeadbeef'),
|
||||
]
|
||||
mock_deny.side_effect = exception.ShareAccessRemovalError(
|
||||
share_id=share_mapping.share_id, reason='boom')
|
||||
|
||||
# Must not raise.
|
||||
self.compute.share_manager.reconcile_stale_share_access(self.context)
|
||||
|
||||
mock_deny.assert_called_once()
|
||||
|
||||
@mock.patch('nova.share.manila.API.deny')
|
||||
@mock.patch('nova.share.manila.API.get_access_rules')
|
||||
@mock.patch('nova.objects.InstanceList.get_by_filters')
|
||||
@mock.patch('nova.objects.share_mapping.ShareMappingList.get_by_share_id')
|
||||
@mock.patch(
|
||||
'nova.objects.share_mapping.ShareMappingList.'
|
||||
'get_by_host_for_reconcile')
|
||||
def test_reconcile_stale_share_access_get_rules_not_found(
|
||||
self, mock_get_for_reconcile,
|
||||
mock_get_by_share_id, mock_get_by_filters,
|
||||
mock_get_rules, mock_deny
|
||||
):
|
||||
self.flags(auth_type='password', group='manila')
|
||||
share_mapping = self.get_fake_share_mapping_cephfs()
|
||||
instance = fake_instance.fake_instance_obj(
|
||||
self.context, uuid=uuids.instance, host='dest-host',
|
||||
vm_state=vm_states.ACTIVE, task_state=None)
|
||||
self._setup_reconcile_mocks(
|
||||
mock_get_for_reconcile,
|
||||
mock_get_by_share_id, [instance], [share_mapping])
|
||||
mock_get_by_filters.return_value = objects.InstanceList(
|
||||
objects=[instance])
|
||||
mock_get_rules.side_effect = exception.ShareNotFound(
|
||||
share_id=share_mapping.share_id)
|
||||
|
||||
# Must not raise.
|
||||
self.compute.share_manager.reconcile_stale_share_access(self.context)
|
||||
|
||||
mock_deny.assert_not_called()
|
||||
|
||||
@mock.patch('nova.share.manila.API.deny')
|
||||
@mock.patch('nova.share.manila.API.get_access_rules')
|
||||
@mock.patch(
|
||||
'nova.objects.share_mapping.ShareMappingList.'
|
||||
'get_by_host_for_reconcile')
|
||||
def test_reconcile_stale_share_access_no_shares(
|
||||
self, mock_get_for_reconcile,
|
||||
mock_get_rules, mock_deny
|
||||
):
|
||||
self.flags(auth_type='password', group='manila')
|
||||
mock_get_for_reconcile.return_value = objects.ShareMappingList(
|
||||
objects=[])
|
||||
|
||||
self.compute.share_manager.reconcile_stale_share_access(self.context)
|
||||
|
||||
mock_get_rules.assert_not_called()
|
||||
mock_deny.assert_not_called()
|
||||
|
||||
@mock.patch('nova.share.manila.API.deny')
|
||||
@mock.patch('nova.share.manila.API.get_access_rules')
|
||||
@mock.patch('nova.objects.InstanceList.get_by_filters')
|
||||
@mock.patch('nova.objects.share_mapping.ShareMappingList.get_by_share_id')
|
||||
@mock.patch(
|
||||
'nova.objects.share_mapping.ShareMappingList.'
|
||||
'get_by_host_for_reconcile')
|
||||
def test_reconcile_stale_share_access_manila_down(
|
||||
self, mock_get_for_reconcile,
|
||||
mock_get_by_share_id, mock_get_by_filters,
|
||||
mock_get_rules, mock_deny
|
||||
):
|
||||
self.flags(auth_type='password', group='manila')
|
||||
share_mapping = self.get_fake_share_mapping_cephfs()
|
||||
instance = fake_instance.fake_instance_obj(
|
||||
self.context, uuid=uuids.instance, host='dest-host',
|
||||
vm_state=vm_states.ACTIVE, task_state=None)
|
||||
self._setup_reconcile_mocks(
|
||||
mock_get_for_reconcile,
|
||||
mock_get_by_share_id, [instance], [share_mapping])
|
||||
mock_get_by_filters.return_value = objects.InstanceList(
|
||||
objects=[instance])
|
||||
mock_get_rules.side_effect = exception.ManilaConnectionFailed(
|
||||
reason='down')
|
||||
|
||||
# Must not raise; the whole pass bails out.
|
||||
self.compute.share_manager.reconcile_stale_share_access(self.context)
|
||||
|
||||
mock_deny.assert_not_called()
|
||||
|
||||
@mock.patch(
|
||||
'nova.objects.share_mapping.ShareMappingList.'
|
||||
'get_by_host_for_reconcile')
|
||||
def test_reconcile_stale_share_access_manila_not_configured(
|
||||
self, mock_get_for_reconcile
|
||||
):
|
||||
# When [manila] auth_type is None the method returns immediately
|
||||
# without touching anything.
|
||||
self.compute.share_manager.reconcile_stale_share_access(self.context)
|
||||
|
||||
mock_get_for_reconcile.assert_not_called()
|
||||
|
||||
@mock.patch('nova.share.manila.API.deny')
|
||||
@mock.patch('nova.share.manila.API.get_access_rules')
|
||||
@mock.patch('nova.objects.InstanceList.get_by_filters')
|
||||
@mock.patch('nova.objects.share_mapping.ShareMappingList.get_by_share_id')
|
||||
@mock.patch(
|
||||
'nova.objects.share_mapping.ShareMappingList.'
|
||||
'get_by_host_for_reconcile')
|
||||
def test_reconcile_stale_share_access_source_migration(
|
||||
self, mock_get_for_reconcile,
|
||||
mock_get_by_share_id, mock_get_by_filters,
|
||||
mock_get_rules, mock_deny
|
||||
):
|
||||
# This host was the source of a confirmed migration; the NFS ip rule
|
||||
# left on this host must be drained even though the instance is gone.
|
||||
self.flags(host='source-host')
|
||||
self.flags(auth_type='password', group='manila')
|
||||
self.flags(my_shared_fs_storage_ip='192.168.0.1')
|
||||
share_mapping = self.get_fake_share_mapping()
|
||||
instance = fake_instance.fake_instance_obj(
|
||||
self.context, uuid=uuids.instance, host='dest-host',
|
||||
vm_state=vm_states.ACTIVE, task_state=None)
|
||||
# No instances on this host now, but a migration touched it, so the
|
||||
# conductor-side collection still surfaces this share mapping.
|
||||
mock_get_for_reconcile.return_value = objects.ShareMappingList(
|
||||
objects=[share_mapping])
|
||||
mock_get_by_share_id.return_value = objects.ShareMappingList(
|
||||
objects=[share_mapping])
|
||||
mock_get_by_filters.return_value = objects.InstanceList(
|
||||
objects=[instance])
|
||||
mock_get_rules.return_value = [
|
||||
self._fake_access_rule('ip', '192.168.0.1'),
|
||||
]
|
||||
|
||||
self.compute.share_manager.reconcile_stale_share_access(self.context)
|
||||
|
||||
# Source's stale NFS rule must be revoked (the fake driver reports the
|
||||
# share unmounted here).
|
||||
mock_deny.assert_called_once_with(
|
||||
self.context, share_mapping.share_id, 'ip', '192.168.0.1')
|
||||
|
||||
@mock.patch('nova.share.manila.API.deny')
|
||||
@mock.patch('nova.share.manila.API.get_access_rules')
|
||||
@mock.patch('nova.objects.InstanceList.get_by_filters')
|
||||
@mock.patch('nova.objects.share_mapping.ShareMappingList.get_by_share_id')
|
||||
@mock.patch(
|
||||
'nova.objects.share_mapping.ShareMappingList.'
|
||||
'get_by_host_for_reconcile')
|
||||
def test_reconcile_stale_share_access_dest_revert(
|
||||
self, mock_get_for_reconcile,
|
||||
mock_get_by_share_id, mock_get_by_filters,
|
||||
mock_get_rules, mock_deny
|
||||
):
|
||||
# This host was the destination of a reverted migration; our ip rule
|
||||
# must be drained even though the instance went back to the source.
|
||||
self.flags(host='dest-host')
|
||||
self.flags(auth_type='password', group='manila')
|
||||
self.flags(my_shared_fs_storage_ip='192.168.0.2')
|
||||
share_mapping = self.get_fake_share_mapping()
|
||||
instance = fake_instance.fake_instance_obj(
|
||||
self.context, uuid=uuids.instance, host='source-host',
|
||||
vm_state=vm_states.ACTIVE, task_state=None)
|
||||
# No instances on this host now, but a migration touched it, so the
|
||||
# conductor-side collection still surfaces this share mapping.
|
||||
mock_get_for_reconcile.return_value = objects.ShareMappingList(
|
||||
objects=[share_mapping])
|
||||
mock_get_by_share_id.return_value = objects.ShareMappingList(
|
||||
objects=[share_mapping])
|
||||
mock_get_by_filters.return_value = objects.InstanceList(
|
||||
objects=[instance])
|
||||
mock_get_rules.return_value = [
|
||||
self._fake_access_rule('ip', '192.168.0.2'),
|
||||
]
|
||||
|
||||
self.compute.share_manager.reconcile_stale_share_access(self.context)
|
||||
|
||||
# Dest host's stale NFS rule must be revoked (the fake driver reports
|
||||
# the share unmounted here).
|
||||
mock_deny.assert_called_once_with(
|
||||
self.context, share_mapping.share_id, 'ip', '192.168.0.2')
|
||||
|
||||
@mock.patch(
|
||||
'nova.objects.share_mapping.ShareMappingList.'
|
||||
'get_by_host_for_reconcile')
|
||||
def test_reconcile_stale_share_access_single_bulk_query(
|
||||
self, mock_get_for_reconcile
|
||||
):
|
||||
# The candidate shares are collected in a single conductor-side query
|
||||
# rather than one lookup per instance and per migration.
|
||||
self.flags(auth_type='password', group='manila')
|
||||
mock_get_for_reconcile.return_value = objects.ShareMappingList(
|
||||
objects=[])
|
||||
|
||||
self.compute.share_manager.reconcile_stale_share_access(self.context)
|
||||
|
||||
mock_get_for_reconcile.assert_called_once_with(
|
||||
self.context, CONF.host)
|
||||
|
||||
def test_reconcile_stale_share_access_periodic(self):
|
||||
with mock.patch.object(
|
||||
self.compute.share_manager, 'reconcile_stale_share_access'
|
||||
) as mock_reconcile:
|
||||
self.compute._reconcile_stale_share_access(self.context)
|
||||
mock_reconcile.assert_called_once_with(self.context)
|
||||
|
||||
@mock.patch('nova.compute.share_management.ShareManager.umount_all')
|
||||
@mock.patch('nova.compute.share_management.ShareManager.get_share_info')
|
||||
@mock.patch('nova.virt.fake.FakeDriver.power_off')
|
||||
|
||||
@@ -4378,6 +4378,29 @@ class ShareMappingDBApiTestCase(test.TestCase):
|
||||
expected_share_mappings[share_mappings[0].share_id]
|
||||
)
|
||||
|
||||
def test_share_mapping_get_by_instance_uuids(self):
|
||||
ctxt = context.get_admin_context()
|
||||
expected_share_mappings = self.create_test_data(ctxt)
|
||||
|
||||
share_mappings = db.share_mapping_get_by_instance_uuids(
|
||||
ctxt, ['fake-instance-uuid1', 'fake-instance-uuid2'])
|
||||
self.assertEqual(len(share_mappings), 2)
|
||||
for share_mapping in share_mappings:
|
||||
self._compare(
|
||||
share_mapping,
|
||||
expected_share_mappings[share_mapping.share_id])
|
||||
|
||||
def test_share_mapping_get_by_instance_uuids_subset(self):
|
||||
ctxt = context.get_admin_context()
|
||||
expected_share_mappings = self.create_test_data(ctxt)
|
||||
|
||||
share_mappings = db.share_mapping_get_by_instance_uuids(
|
||||
ctxt, ['fake-instance-uuid1'])
|
||||
self.assertEqual(len(share_mappings), 1)
|
||||
self._compare(
|
||||
share_mappings[0],
|
||||
expected_share_mappings[share_mappings[0].share_id])
|
||||
|
||||
def test_share_mapping_get_by_instance_uuid_and_share_id(self):
|
||||
ctxt = context.get_admin_context()
|
||||
expected_share_mappings = self.create_test_data(ctxt)
|
||||
|
||||
@@ -1170,7 +1170,7 @@ object_data = {
|
||||
'Service': '1.22-8a740459ab9bf258a19c8fcb875c2d9a',
|
||||
'ServiceList': '1.19-5325bce13eebcbf22edc9678285270cc',
|
||||
'ShareMapping': '1.2-ae6ba712dc8022d08c4de34fb8b6e015',
|
||||
'ShareMappingList': '1.0-634980d5efdf3656e28c8dec3d862ab9',
|
||||
'ShareMappingList': '1.1-4b834bee6431b8ea64ac3d7f0874675f',
|
||||
'ShareMetadata': '1.0-09f69ac0bd47371417b5477a277e43af',
|
||||
'Tag': '1.1-8b8d7d5b48887651a0e01241672e2963',
|
||||
'TagList': '1.1-55231bdb671ecf7641d6a2e9109b5d8e',
|
||||
|
||||
@@ -294,6 +294,21 @@ class _TestShareMapping(object):
|
||||
self.assertEqual('cephx', share_mapping.access_type)
|
||||
self.assertEqual(expected, share_mapping.access_to)
|
||||
|
||||
def test_cephx_identity(self):
|
||||
identity = objects.ShareMapping._cephx_identity(
|
||||
uuids.instance, 'compute-1')
|
||||
expected = 'nova-' + hashlib.sha256(
|
||||
f'{uuids.instance}:compute-1'.encode()).hexdigest()[:16]
|
||||
self.assertEqual(expected, identity)
|
||||
self.assertTrue(identity.startswith('nova-'))
|
||||
# Deterministic and host-scoped.
|
||||
self.assertEqual(
|
||||
identity,
|
||||
objects.ShareMapping._cephx_identity(uuids.instance, 'compute-1'))
|
||||
self.assertNotEqual(
|
||||
identity,
|
||||
objects.ShareMapping._cephx_identity(uuids.instance, 'compute-2'))
|
||||
|
||||
def test_set_access_according_to_protocol_cephfs_per_instance_per_host(
|
||||
self
|
||||
):
|
||||
@@ -345,6 +360,54 @@ class _TestShareMappingList(object):
|
||||
self.assertEqual(1, len(share_mappings))
|
||||
self.assertIsInstance(share_mappings[0], sm.ShareMapping)
|
||||
|
||||
@mock.patch.object(db, 'share_mapping_get_by_instance_uuids')
|
||||
@mock.patch.object(db, 'migration_get_all_by_filters')
|
||||
@mock.patch.object(db, 'instance_get_all_uuids_by_hosts')
|
||||
def test_get_by_host_for_reconcile(
|
||||
self, mock_hosts, mock_migs, mock_share_mappings
|
||||
):
|
||||
mock_hosts.return_value = {'fake-host': [uuids.instance]}
|
||||
mock_migs.return_value = [mock.Mock(instance_uuid=uuids.instance2)]
|
||||
mock_share_mappings.return_value = [fake_share_mapping]
|
||||
|
||||
share_mappings = sm.ShareMappingList.get_by_host_for_reconcile(
|
||||
self.context, 'fake-host'
|
||||
)
|
||||
|
||||
mock_hosts.assert_called_once_with(self.context, ['fake-host'])
|
||||
mock_migs.assert_called_once_with(
|
||||
self.context,
|
||||
{'host': 'fake-host',
|
||||
'status': ['confirmed', 'reverted', 'error']})
|
||||
# The on-host instance and the migration's instance are gathered
|
||||
# into a single bulk share-mapping query.
|
||||
called_context, called_uuids = mock_share_mappings.call_args[0]
|
||||
self.assertEqual(self.context, called_context)
|
||||
self.assertEqual(
|
||||
{uuids.instance, uuids.instance2}, set(called_uuids))
|
||||
self.assertIsInstance(share_mappings, sm.ShareMappingList)
|
||||
self.assertEqual(1, len(share_mappings))
|
||||
self.assertIsInstance(share_mappings[0], sm.ShareMapping)
|
||||
|
||||
@mock.patch.object(db, 'share_mapping_get_by_instance_uuids')
|
||||
@mock.patch.object(db, 'migration_get_all_by_filters')
|
||||
@mock.patch.object(db, 'instance_get_all_uuids_by_hosts')
|
||||
def test_get_by_host_for_reconcile_no_candidates(
|
||||
self, mock_hosts, mock_migs, mock_share_mappings
|
||||
):
|
||||
# No instances on the host and no migrations touching it: skip the
|
||||
# share-mapping query entirely and return an empty list.
|
||||
mock_hosts.return_value = {'fake-host': []}
|
||||
mock_migs.return_value = []
|
||||
|
||||
share_mappings = sm.ShareMappingList.get_by_host_for_reconcile(
|
||||
self.context, 'fake-host'
|
||||
)
|
||||
|
||||
mock_share_mappings.assert_not_called()
|
||||
self.assertIsInstance(share_mappings, sm.ShareMappingList)
|
||||
self.assertEqual(0, len(share_mappings))
|
||||
|
||||
def test_get_by_share_id(self):
|
||||
with mock.patch.object(db, 'share_mapping_get_by_share_id') as get:
|
||||
get.return_value = [fake_share_mapping]
|
||||
|
||||
@@ -103,3 +103,19 @@ class TestFSCommon(test.NoDBTestCase):
|
||||
(filesystem.RETRY_LIMIT + 1))
|
||||
self.assertRaises(
|
||||
exception.DeviceBusy, filesystem.write_sys, 'foo', 'bar')
|
||||
|
||||
@mock.patch('psutil.disk_partitions')
|
||||
def test_is_mounted_true(self, mock_partitions):
|
||||
mock_partitions.return_value = [
|
||||
mock.Mock(mountpoint='/mnt/other'),
|
||||
mock.Mock(mountpoint='/mnt/foo'),
|
||||
]
|
||||
self.assertTrue(filesystem.is_mounted('/mnt/foo'))
|
||||
mock_partitions.assert_called_once_with(all=True)
|
||||
|
||||
@mock.patch('psutil.disk_partitions')
|
||||
def test_is_mounted_false(self, mock_partitions):
|
||||
mock_partitions.return_value = [
|
||||
mock.Mock(mountpoint='/mnt/other'),
|
||||
]
|
||||
self.assertFalse(filesystem.is_mounted('/mnt/foo'))
|
||||
@@ -415,6 +415,45 @@ class ManilaTestCase(BaseManilaTestCase, test.NoDBTestCase):
|
||||
|
||||
self.assertIsNone(access)
|
||||
|
||||
@mock.patch(
|
||||
'nova.utils.get_sdk_adapter', side_effect=nova.utils.get_sdk_adapter)
|
||||
def test_get_access_rules(self, mock_get_sdk_adapter):
|
||||
"""Tests that we manage to list every access rule of a share.
|
||||
"""
|
||||
access_rules = self.api.get_access_rules(self.context, '1234')
|
||||
mock_get_sdk_adapter.assert_called_once_with(
|
||||
"shared-file-system",
|
||||
admin=True,
|
||||
check_service=True,
|
||||
context=self.context,
|
||||
shared_file_system_api_version="2.82",
|
||||
global_request_id=self.context.global_id,
|
||||
)
|
||||
self.assertEqual(1, len(access_rules))
|
||||
access = access_rules[0]
|
||||
self.assertIsInstance(access, manila.Access)
|
||||
self.assertEqual('a25b2df3-90bd-4add-afa6-5f0dbbd50452', access.id)
|
||||
self.assertEqual('ip', access.access_type)
|
||||
self.assertEqual('0.0.0.0/0', access.access_to)
|
||||
|
||||
def test_get_access_rules_empty(self):
|
||||
"""Tests that a share with no access rules returns an empty list.
|
||||
"""
|
||||
access_rules = self.api.get_access_rules(self.context, '4567')
|
||||
self.assertEqual([], access_rules)
|
||||
|
||||
def test_get_access_rules_fails_non_existing_share(self):
|
||||
"""Tests that we fail when listing rules of a non existing share.
|
||||
"""
|
||||
exc = self.assertRaises(
|
||||
exception.ShareNotFound,
|
||||
self.api.get_access_rules,
|
||||
self.context,
|
||||
"nonexisting",
|
||||
)
|
||||
|
||||
self.assertIn("Share nonexisting could not be found.", exc.message)
|
||||
|
||||
def test_allow_access_fails_non_existing_share(self):
|
||||
"""Tests that we fail if trying to allow an
|
||||
non existing share.
|
||||
|
||||
@@ -19006,6 +19006,33 @@ class LibvirtConnTestCase(test.NoDBTestCase,
|
||||
processutils.ProcessExecutionError,
|
||||
)
|
||||
|
||||
@mock.patch('nova.filesystem.is_mounted')
|
||||
def test_is_share_mounted(self, mock_is_mounted):
|
||||
mock_is_mounted.return_value = True
|
||||
drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
|
||||
share_mapping = objects.ShareMapping(self.context)
|
||||
share_mapping.share_id = uuids.share
|
||||
share_mapping.export_location = '192.168.122.152:/manila/share'
|
||||
share_mapping.share_proto = fields.ShareMappingProto.NFS
|
||||
# The check keys off the deterministic mountpoint, not the NFS
|
||||
# server:/path device string the kernel may canonicalize.
|
||||
expected_path = drvr._get_share_driver_manager(
|
||||
CONF.host, share_mapping.share_proto)._get_mount_path(
|
||||
drvr._get_share_connection_info(share_mapping))
|
||||
|
||||
self.assertTrue(drvr.is_share_mounted(share_mapping))
|
||||
mock_is_mounted.assert_called_once_with(expected_path)
|
||||
|
||||
@mock.patch('nova.filesystem.is_mounted', return_value=False)
|
||||
def test_is_share_mounted_not_mounted(self, mock_is_mounted):
|
||||
drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
|
||||
share_mapping = objects.ShareMapping(self.context)
|
||||
share_mapping.share_id = uuids.share
|
||||
share_mapping.export_location = '192.168.122.152:/manila/share'
|
||||
share_mapping.share_proto = fields.ShareMappingProto.NFS
|
||||
|
||||
self.assertFalse(drvr.is_share_mounted(share_mapping))
|
||||
|
||||
@mock.patch('nova.objects.instance.Instance.save',
|
||||
return_value=None)
|
||||
@mock.patch('nova.virt.libvirt.LibvirtDriver._build_device_metadata',
|
||||
|
||||
@@ -1151,6 +1151,15 @@ class ComputeDriver(object):
|
||||
"""
|
||||
raise NotImplementedError()
|
||||
|
||||
def is_share_mounted(self, share_mapping):
|
||||
"""Return whether a manila share is mounted on this compute node.
|
||||
|
||||
:param share_mapping: nova.objects.share_mapping.ShareMapping object
|
||||
that define the share
|
||||
:returns: True if the share's mountpoint is present on this host
|
||||
"""
|
||||
raise NotImplementedError()
|
||||
|
||||
def power_update_event(self, instance, target_power_state):
|
||||
"""Update power, vm and task states of the specified instance.
|
||||
|
||||
|
||||
@@ -304,6 +304,9 @@ class FakeDriver(driver.ComputeDriver):
|
||||
def umount_share(self, context, instance, share_mapping):
|
||||
pass
|
||||
|
||||
def is_share_mounted(self, share_mapping):
|
||||
return False
|
||||
|
||||
def trigger_crash_dump(self, instance):
|
||||
pass
|
||||
|
||||
|
||||
@@ -85,6 +85,7 @@ from nova.console import type as ctype
|
||||
from nova import context as nova_context
|
||||
from nova import crypto
|
||||
from nova import exception
|
||||
from nova import filesystem
|
||||
from nova.i18n import _
|
||||
from nova.image import glance
|
||||
from nova.network import model as network_model
|
||||
@@ -4557,6 +4558,16 @@ class LibvirtDriver(driver.ComputeDriver):
|
||||
reason=exc
|
||||
)
|
||||
|
||||
def is_share_mounted(self, share_mapping):
|
||||
# The mountpoint is a deterministic hash of the export location, so
|
||||
# matching on it sidesteps how the kernel canonicalizes the NFS
|
||||
# server:/path device string (short name vs FQDN vs IP).
|
||||
drv = self._get_share_driver_manager(
|
||||
CONF.host, share_mapping.share_proto)
|
||||
mount_path = drv._get_mount_path(
|
||||
self._get_share_connection_info(share_mapping))
|
||||
return filesystem.is_mounted(mount_path)
|
||||
|
||||
def trigger_crash_dump(self, instance):
|
||||
"""Trigger crash dump by injecting an NMI to the specified instance."""
|
||||
try:
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
---
|
||||
features:
|
||||
- |
|
||||
Nova compute now reconciles stale Manila share access rules. When the
|
||||
source host's share access is not revoked during a cold migration or
|
||||
resize confirm/revert (for example because manila-api is unreachable), the
|
||||
orphaned rule remains granted with nothing to retry the cleanup. Each
|
||||
compute now revokes such orphaned rules at start-up and periodically. The
|
||||
interval is controlled by the new
|
||||
``[DEFAULT] stale_share_access_reconcile_interval`` option (default 1800
|
||||
seconds); setting it to ``0`` or a negative value disables the periodic
|
||||
task while leaving the start-up reconcile in place.
|
||||
upgrade:
|
||||
- |
|
||||
The new stale share access reconcile does not revoke legacy shared
|
||||
``nova`` CephFS grants, non-nova cephx identities, or access rules for
|
||||
shares that have no remaining share mappings. Draining those pre-existing
|
||||
grants remains an operator task.
|
||||
Reference in new issue
Block a user