@@ -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
691692func (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-
763744func (s * Service ) deleteInstance (eventObject runtime.Object , instance * InstanceIdentifier ) error {
764745 err := s .getComputeClient ().DeleteServer (instance .ID )
765746 if err != nil {
0 commit comments