Skip to content

Commit d22c55a

Browse files
committed
UPSTREAM 2842: Fix instance deletion order
Delete the OpenStack instance before deleting its network ports. This prevents "Policy doesn't allow compute:detach primary port" errors that occur when attempting to delete a primary NIC while the instance still exists. Deleting the instance automatically detaches all interfaces, so the explicit detach call is no longer needed.
1 parent 61c49ec commit d22c55a

File tree

2 files changed

+10
-30
lines changed

2 files changed

+10
-30
lines changed

pkg/cloud/services/compute/instance.go

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -657,12 +657,13 @@ func (s *Service) DeleteInstance(openStackCluster *infrav1.OpenStackCluster, eve
657657
return err
658658
}
659659

660-
// get and delete trunks
661-
for _, port := range instanceInterfaces {
662-
if err = s.deleteAttachInterface(eventObject, instanceStatus.InstanceIdentifier(), port.PortID); err != nil {
663-
return err
664-
}
660+
// Delete the instance before ports, since openstack likely prevents the deletion of the primary NIC.
661+
// Deleting the instance also automatically detach the interfaces.
662+
if err := s.deleteInstance(eventObject, instanceStatus.InstanceIdentifier()); err != nil {
663+
return err
664+
}
665665

666+
for _, port := range instanceInterfaces {
666667
if trunkSupported {
667668
if err = networkingService.DeleteTrunk(eventObject, port.PortID); err != nil {
668669
return err
@@ -685,7 +686,7 @@ func (s *Service) DeleteInstance(openStackCluster *infrav1.OpenStackCluster, eve
685686
}
686687
}
687688

688-
return s.deleteInstance(eventObject, instanceStatus.InstanceIdentifier())
689+
return nil
689690
}
690691

691692
func (s *Service) deleteVolumes(instanceSpec *InstanceSpec) error {
@@ -740,26 +741,6 @@ func (s *Service) deletePorts(eventObject runtime.Object, nets []servers.Network
740741
return nil
741742
}
742743

743-
func (s *Service) deleteAttachInterface(eventObject runtime.Object, instance *InstanceIdentifier, portID string) error {
744-
err := s.getComputeClient().DeleteAttachedInterface(instance.ID, portID)
745-
if err != nil {
746-
if capoerrors.IsNotFound(err) {
747-
record.Eventf(eventObject, "SuccessfulDeleteAttachInterface", "Attach interface did not exist: instance %s, port %s", instance.ID, portID)
748-
return nil
749-
}
750-
if capoerrors.IsConflict(err) {
751-
// we don't want to block deletion because of Conflict
752-
// due to instance must be paused/active/shutoff in order to detach interface
753-
return nil
754-
}
755-
record.Warnf(eventObject, "FailedDeleteAttachInterface", "Failed to delete attach interface: instance %s, port %s: %v", instance.ID, portID, err)
756-
return err
757-
}
758-
759-
record.Eventf(eventObject, "SuccessfulDeleteAttachInterface", "Deleted attach interface: instance %s, port %s", instance.ID, portID)
760-
return nil
761-
}
762-
763744
func (s *Service) deleteInstance(eventObject runtime.Object, instance *InstanceIdentifier) error {
764745
err := s.getComputeClient().DeleteServer(instance.ID)
765746
if err != nil {

pkg/cloud/services/compute/instance_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,13 +1032,12 @@ func TestService_DeleteInstance(t *testing.T) {
10321032
Alias: "trunk",
10331033
},
10341034
}}, nil)
1035-
r.compute.DeleteAttachedInterface(instanceUUID, portUUID).Return(nil)
1035+
r.compute.DeleteServer(instanceUUID).Return(nil)
1036+
r.compute.GetServer(instanceUUID).Return(nil, gophercloud.ErrDefault404{})
1037+
10361038
// FIXME: Why we are looking for a trunk when we know the port is not trunked?
10371039
r.network.ListTrunk(trunks.ListOpts{PortID: portUUID}).Return([]trunks.Trunk{}, nil)
10381040
r.network.DeletePort(portUUID).Return(nil)
1039-
1040-
r.compute.DeleteServer(instanceUUID).Return(nil)
1041-
r.compute.GetServer(instanceUUID).Return(nil, gophercloud.ErrDefault404{})
10421041
},
10431042
wantErr: false,
10441043
},

0 commit comments

Comments
 (0)