Handle Invalid exceptions as expected in attach_interface

The bug prompting this is a tempest test which is requesting
a port attachment to a server but not specifying a port or
network to use, so nova-compute looks for a valid network
and finds there are two and raises NetworkAmbiguous. This
is treated as a 400 error in the API but because this is a
synchronous RPC call from nova-api to nova-compute,
oslo.messaging logs an exception traceback for the unexpected
error. That traceback is pretty gross in the compute logs for
something that is a user error and the cloud operator has
nothing to do to fix it.

We can handle the traceback by registering our expected
exceptions for the attach_interface method with oslo.messaging,
which is what this change does.

While looking to just add NetworkAmbiguous it became clear that
lots of different user errors can be raised from this method
and none of those should result in a traceback, so this change
just expects Invalid and its subclasses.

The one exception is InterfaceAttachFailed which is raised when
something in allocate_port_for_instance or driver.attach_interface
fails. That is an unexpected situation so the parent class for
InterfaceAttachFailed is changed from Invalid to NovaException so
it continues to be traced in the logs as an exception.
InterfaceAttachFailedNoNetwork is kept as Invalid since it is a
user error (trying to attach an interface when the user has no
access to any networks).

test_tagged_attach_interface_raises is adjusted to show the
ExpectedException handling for one of the Invalid cases.

Change-Id: I927ff1d8c8f45405833d6012b7d7af37b98b10a0
Closes-Bug: #1823198
(cherry picked from commit ade6c93936)
This commit is contained in:
Matt Riedemann 2019-04-04 13:04:33 -04:00
parent 7a3a8f325e
commit e9261764c9
3 changed files with 21 additions and 4 deletions

View File

@ -6196,6 +6196,20 @@ class ComputeManager(manager.Manager):
{'port_id': port_id, 'error': ex},
instance=instance)
# TODO(mriedem): There are likely race failures which can result in
# NotFound and QuotaError exceptions getting traced as well.
@messaging.expected_exceptions(
# Do not log a traceback for user errors. We use Invalid generically
# since this method can raise lots of different exceptions:
# AttachInterfaceNotSupported
# NetworkInterfaceTaggedAttachNotSupported
# NetworkAmbiguous
# PortNotUsable
# PortInUse
# PortNotUsableDNS
# AttachSRIOVPortNotSupported
# NetworksWithQoSPolicyNotSupported
exception.Invalid)
@wrap_exception()
@wrap_instance_event(prefix='compute')
@wrap_instance_fault

View File

@ -1608,12 +1608,12 @@ class ConfigDriveNotFound(NotFound):
"does not exist.")
class InterfaceAttachFailed(Invalid):
class InterfaceAttachFailed(NovaException):
msg_fmt = _("Failed to attach network adapter device to "
"%(instance_uuid)s")
class InterfaceAttachFailedNoNetwork(InterfaceAttachFailed):
class InterfaceAttachFailedNoNetwork(Invalid):
msg_fmt = _("No specific network was requested and none are available "
"for project '%(project_id)s'.")

View File

@ -10534,10 +10534,13 @@ class ComputeAPITestCase(BaseTestCase):
with mock.patch.dict(self.compute.driver.capabilities,
supports_attach_interface=True,
supports_tagged_attach_interface=False):
self.assertRaises(
exception.NetworkInterfaceTaggedAttachNotSupported,
expected_exception = self.assertRaises(
messaging.ExpectedException,
self.compute.attach_interface, self.context, instance,
'fake-network-id', 'fake-port-id', 'fake-req-ip', tag='foo')
wrapped_exc = expected_exception.exc_info[1]
self.assertIsInstance(
wrapped_exc, exception.NetworkInterfaceTaggedAttachNotSupported)
def test_attach_interface_failed(self):
new_type = flavors.get_flavor_by_flavor_id('4')