Skip to content

Commit f69e34c

Browse files
authored
Fix for leaking target groups on service delete (#525)
* Fix for leaking target groups on service delete due to listener delete race condition
1 parent af28cb1 commit f69e34c

File tree

5 files changed

+76
-1
lines changed

5 files changed

+76
-1
lines changed

docs/contributing/developer.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ make e2e-clean
9393

9494
## Local Development
9595

96-
A minimal sanity check on changes can be done with make presubmit. This command will also run on PR.
96+
A minimal test of changes can be done with ```make presubmit```. This command will also run on PR.
9797

9898
```
9999
make presubmit

pkg/aws/services/vpclattice.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ func IsInvalidError(err error) bool {
9494

9595
type Lattice interface {
9696
vpclatticeiface.VPCLatticeAPI
97+
ListListenersAsList(ctx context.Context, input *vpclattice.ListListenersInput) ([]*vpclattice.ListenerSummary, error)
9798
GetRulesAsList(ctx context.Context, input *vpclattice.ListRulesInput) ([]*vpclattice.GetRuleOutput, error)
9899
ListRulesAsList(ctx context.Context, input *vpclattice.ListRulesInput) ([]*vpclattice.RuleSummary, error)
99100
ListServiceNetworksAsList(ctx context.Context, input *vpclattice.ListServiceNetworksInput) ([]*vpclattice.ServiceNetworkSummary, error)
@@ -128,6 +129,23 @@ func NewDefaultLattice(sess *session.Session, region string) *defaultLattice {
128129
return &defaultLattice{VPCLatticeAPI: latticeSess, cache: cache}
129130
}
130131

132+
func (d *defaultLattice) ListListenersAsList(ctx context.Context, input *vpclattice.ListListenersInput) ([]*vpclattice.ListenerSummary, error) {
133+
var result []*vpclattice.ListenerSummary
134+
135+
err := d.ListListenersPagesWithContext(ctx, input, func(page *vpclattice.ListListenersOutput, lastPage bool) bool {
136+
for _, l := range page.Items {
137+
result = append(result, l)
138+
}
139+
return true
140+
})
141+
142+
if err != nil {
143+
return nil, err
144+
}
145+
146+
return result, nil
147+
}
148+
131149
func (d *defaultLattice) GetRulesAsList(ctx context.Context, input *vpclattice.ListRulesInput) ([]*vpclattice.GetRuleOutput, error) {
132150
var result []*vpclattice.GetRuleOutput
133151

pkg/aws/services/vpclattice_mocks.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/deploy/lattice/service_manager.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,25 @@ func (m *defaultServiceManager) deleteAllAssociations(ctx context.Context, svc *
302302
return nil
303303
}
304304

305+
func (m *defaultServiceManager) deleteAllListeners(ctx context.Context, svc *SvcSummary) error {
306+
listeners, err := m.cloud.Lattice().ListListenersAsList(ctx, &vpclattice.ListListenersInput{
307+
ServiceIdentifier: svc.Id,
308+
})
309+
if err != nil {
310+
return err
311+
}
312+
for _, listener := range listeners {
313+
_, err = m.cloud.Lattice().DeleteListenerWithContext(ctx, &vpclattice.DeleteListenerInput{
314+
ServiceIdentifier: svc.Id,
315+
ListenerIdentifier: listener.Id,
316+
})
317+
if err != nil {
318+
return err
319+
}
320+
}
321+
return nil
322+
}
323+
305324
func (m *defaultServiceManager) deleteAssociation(ctx context.Context, assocArn *string) error {
306325
delReq := &DelSnSvcAssocReq{ServiceNetworkServiceAssociationIdentifier: assocArn}
307326
_, err := m.cloud.Lattice().DeleteServiceNetworkServiceAssociationWithContext(ctx, delReq)
@@ -371,6 +390,12 @@ func (m *defaultServiceManager) Delete(ctx context.Context, svc *Service) error
371390
return err
372391
}
373392

393+
// deleting listeners explicitly helps ensure target groups are free to delete
394+
err = m.deleteAllListeners(ctx, svcSum)
395+
if err != nil {
396+
return err
397+
}
398+
374399
err = m.deleteService(ctx, svcSum)
375400
if err != nil {
376401
return err

pkg/deploy/lattice/service_manager_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,23 @@ func TestServiceManagerInteg(t *testing.T) {
311311
DeleteServiceWithContext(gomock.Any(), gomock.Any()).Return(nil, nil).
312312
Times(1)
313313

314+
// assert we delete listeners
315+
mockLattice.EXPECT().
316+
ListListenersAsList(gomock.Any(), gomock.Any()).
317+
Return([]*vpclattice.ListenerSummary{
318+
{
319+
Id: aws.String("L1"),
320+
},
321+
{
322+
Id: aws.String("L2"),
323+
},
324+
}, nil)
325+
326+
mockLattice.EXPECT().
327+
DeleteListenerWithContext(gomock.Any(), gomock.Any()).
328+
Return(nil, nil).
329+
Times(2)
330+
314331
err := m.Delete(ctx, svc)
315332
assert.Nil(t, err)
316333
})

0 commit comments

Comments
 (0)