Detach volumes when deleting a BFV server pre-scheduling

If the user creates a volume-backed server from an existing
volume, the API reserves the volume by creating an attachment
against it. This puts the volume into 'attaching' status.

If the user then deletes the server before it's created in a
cell, by deleting the build request, the attached volume is
orphaned and requires admin intervention in the block storage
service.

This change simply pulls the BDMs off the BuildRequest when
we delete the server via the build request and does the same
local cleanup of those volumes as we would in a "normal" local
delete scenario that the instance was created in a cell but
doesn't have a host.

We don't have to worry about ports in this scenario since
ports are created on the compute, in a cell, and if we're
deleting a build request then we never got far enough to
create ports.

Change-Id: I1a576bdb16befabe06a9728d7adf008fc0667077
Partial-Bug: #1404867
This commit is contained in:
Matt Riedemann 2018-02-15 16:33:56 -05:00
parent 08f0f71a83
commit 0652e4ab3d
2 changed files with 12 additions and 6 deletions

View File

@ -1772,6 +1772,11 @@ class API(base.Base):
# instance is now in a cell and the delete needs to proceed
# normally.
return False
# We need to detach from any volumes so they aren't orphaned.
self._local_cleanup_bdm_volumes(
build_req.block_device_mappings, instance, context)
return True
def _delete(self, context, instance, delete_type, cb, **instance_attrs):
@ -2044,7 +2049,12 @@ class API(base.Base):
except Exception as exc:
LOG.warning("Ignoring volume cleanup failure due to %s",
exc, instance=instance)
bdm.destroy()
# If we're cleaning up volumes from an instance that wasn't yet
# created in a cell, i.e. the user deleted the server while
# the BuildRequest still existed, then the BDM doesn't actually
# exist in the DB to destroy it.
if 'id' in bdm:
bdm.destroy()
def _local_delete(self, context, instance, bdms, delete_type, cb):
if instance.vm_state == vm_states.SHELVED_OFFLOADED:

View File

@ -275,8 +275,4 @@ class ServersPreSchedulingTestCase(test.TestCase,
# The volume should no longer have any attachments as instance delete
# should have removed them.
# self.assertNotIn(volume_id, cinder.attachments[server['id']])
# FIXME(mriedem): This is part of bug 1404867 where the BDMs aren't
# processed when the build request is deleted. Uncomment the above
# and remove the below when this is fixed.
self.assertIn(volume_id, cinder.attachments[server['id']])
self.assertNotIn(volume_id, cinder.attachments[server['id']])