Skip to content

Commit b389ac5

Browse files
authored
Merge pull request #2092 from shiftstack/issue2091
[release-0.9] Fix port cleanup of servers in ERROR state
2 parents bea0e55 + af9386c commit b389ac5

File tree

3 files changed

+65
-10
lines changed

3 files changed

+65
-10
lines changed

pkg/cloud/services/compute/instance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ func (s *Service) DeleteInstance(openStackCluster *infrav1.OpenStackCluster, eve
680680
return err
681681
}
682682

683-
if err := networkingService.GarbageCollectErrorInstancesPort(eventObject, instanceSpec.Name, portOpts); err != nil {
683+
if err := networkingService.GarbageCollectErrorInstancesPort(eventObject, instanceSpec.Name, portOpts, trunkSupported); err != nil {
684684
return err
685685
}
686686
}

pkg/cloud/services/networking/port.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ func (s *Service) DeletePorts(openStackCluster *infrav1.OpenStackCluster) error
295295
return nil
296296
}
297297

298-
func (s *Service) GarbageCollectErrorInstancesPort(eventObject runtime.Object, instanceName string, portOpts []infrav1.PortOpts) error {
298+
func (s *Service) GarbageCollectErrorInstancesPort(eventObject runtime.Object, instanceName string, portOpts []infrav1.PortOpts, trunkSupported bool) error {
299299
for i := range portOpts {
300300
portOpt := &portOpts[i]
301301

@@ -315,8 +315,15 @@ func (s *Service) GarbageCollectErrorInstancesPort(eventObject runtime.Object, i
315315
return fmt.Errorf("garbage collection of port %s failed, found %d ports with the same name", portName, len(portList))
316316
}
317317

318-
if err := s.DeletePort(eventObject, portList[0].ID); err != nil {
319-
return err
318+
for _, port := range portList {
319+
if trunkSupported {
320+
if err := s.DeleteTrunk(eventObject, port.ID); err != nil {
321+
return err
322+
}
323+
}
324+
if err := s.DeletePort(eventObject, port.ID); err != nil {
325+
return err
326+
}
320327
}
321328
}
322329

pkg/cloud/services/networking/port_test.go

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -537,12 +537,12 @@ func Test_GetOrCreatePort(t *testing.T) {
537537
}
538538

539539
func Test_GarbageCollectErrorInstancesPort(t *testing.T) {
540-
mockCtrl := gomock.NewController(t)
541-
defer mockCtrl.Finish()
542-
543-
instanceName := "foo"
544-
portID1 := "dc6e0ae3-dad6-4240-a9cb-e541916f20d3"
545-
portID2 := "a38ab1cb-c2cc-4c1b-9d1d-696ec73356d2"
540+
const (
541+
instanceName = "foo"
542+
portID1 = "dc6e0ae3-dad6-4240-a9cb-e541916f20d3"
543+
portID2 = "a38ab1cb-c2cc-4c1b-9d1d-696ec73356d2"
544+
trunkID1 = "6ecfc5c2-7ee7-41b3-bba7-874b9b1423b7"
545+
)
546546
portName1 := GetPortName(instanceName, nil, 0)
547547
portName2 := GetPortName(instanceName, nil, 1)
548548

@@ -553,6 +553,8 @@ func Test_GarbageCollectErrorInstancesPort(t *testing.T) {
553553
expect func(m *mock.MockNetworkClientMockRecorder)
554554
// portOpts defines the instance ports as defined in the OSM spec.
555555
portOpts []infrav1.PortOpts
556+
// trunkSupported indicates whether we should check for trunk ports
557+
trunkSupported bool
556558
// wantErr defines whether the test is supposed to fail.
557559
wantErr bool
558560
}{
@@ -589,12 +591,57 @@ func Test_GarbageCollectErrorInstancesPort(t *testing.T) {
589591
},
590592
wantErr: false,
591593
},
594+
{
595+
name: "succeed if there are no ports to be cleaned up",
596+
expect: func(m *mock.MockNetworkClientMockRecorder) {
597+
o1 := ports.ListOpts{
598+
Name: portName1,
599+
}
600+
m.ListPort(o1).Return([]ports.Port{}, nil)
601+
},
602+
portOpts: []infrav1.PortOpts{
603+
{},
604+
},
605+
wantErr: false,
606+
},
607+
{
608+
name: "cleanup trunk port",
609+
expect: func(m *mock.MockNetworkClientMockRecorder) {
610+
o1 := ports.ListOpts{
611+
Name: portName1,
612+
}
613+
p1 := []ports.Port{
614+
{
615+
ID: portID1,
616+
Name: portName1,
617+
},
618+
}
619+
m.ListPort(o1).Return(p1, nil)
620+
m.ListTrunk(trunks.ListOpts{
621+
PortID: portID1,
622+
}).Return([]trunks.Trunk{
623+
{
624+
ID: trunkID1,
625+
},
626+
}, nil)
627+
m.DeleteTrunk(trunkID1)
628+
m.DeletePort(portID1)
629+
},
630+
portOpts: []infrav1.PortOpts{
631+
{},
632+
},
633+
trunkSupported: true,
634+
wantErr: false,
635+
},
592636
}
593637

594638
eventObject := &infrav1.OpenStackMachine{}
595639

596640
for _, tt := range tests {
597641
t.Run(tt.name, func(t *testing.T) {
642+
mockCtrl := gomock.NewController(t)
643+
defer mockCtrl.Finish()
644+
598645
g := NewWithT(t)
599646
mockClient := mock.NewMockNetworkClient(mockCtrl)
600647
tt.expect(mockClient.EXPECT())
@@ -605,6 +652,7 @@ func Test_GarbageCollectErrorInstancesPort(t *testing.T) {
605652
eventObject,
606653
instanceName,
607654
tt.portOpts,
655+
tt.trunkSupported,
608656
)
609657
if tt.wantErr {
610658
g.Expect(err).To(HaveOccurred())

0 commit comments

Comments
 (0)