Skip to content

Commit 9b3df9f

Browse files
committed
Imrpoved TG reconciling
1 parent 1de2d59 commit 9b3df9f

File tree

4 files changed

+42
-36
lines changed

4 files changed

+42
-36
lines changed

pkg/alb/lb/loadbalancer.go

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,10 @@ func (l *LoadBalancer) Reconcile(rOpts *ReconcileOptions) []error {
332332
Eventf: rOpts.Eventf,
333333
VpcID: l.lb.current.VpcId,
334334
ManagedSGInstance: l.options.current.managedInstanceSG,
335+
IgnoreDeletes: true,
335336
}
336337

337-
tgs, deletedTG, err := l.targetgroups.Reconcile(tgsOpts)
338+
tgs, err := l.targetgroups.Reconcile(tgsOpts)
338339
if err != nil {
339340
errors = append(errors, err)
340341
return errors
@@ -352,33 +353,26 @@ func (l *LoadBalancer) Reconcile(rOpts *ReconcileOptions) []error {
352353
l.listeners = ltnrs
353354
}
354355

355-
// REFACTOR!
356-
// This chunk of code has some questionable logic and we should probably move
357-
// the TG clean up out of here and into tg. I also dont think that lb.listeners < 1 is a valid check
358-
//
359-
// Return now if listeners are already deleted, signifies has already been destructed and
360-
// TG clean-up, based on rules below does not need to occur.
361-
for _, t := range deletedTG {
362-
if err := albelbv2.ELBV2svc.RemoveTargetGroup(t.CurrentARN()); err != nil {
363-
errors = append(errors, err)
364-
continue
365-
}
366-
index, _ := l.targetgroups.FindById(t.ID)
367-
l.targetgroups = append(l.targetgroups[:index], l.targetgroups[index+1:]...)
356+
tgsOpts.IgnoreDeletes = false
357+
tgs, err = l.targetgroups.Reconcile(tgsOpts)
358+
if err != nil {
359+
errors = append(errors, err)
360+
return errors
368361
}
362+
l.targetgroups = tgs
369363

370-
for _, listener := range l.listeners {
371-
unusedTGs := listener.GetRules().FindUnusedTGs(l.targetgroups)
372-
for _, t := range unusedTGs {
373-
if err := albelbv2.ELBV2svc.RemoveTargetGroup(t.CurrentARN()); err != nil {
374-
errors = append(errors, err)
375-
continue
376-
}
377-
index, _ := l.targetgroups.FindById(t.ID)
378-
l.targetgroups = append(l.targetgroups[:index], l.targetgroups[index+1:]...)
379-
}
380-
}
381-
// END REFACTOR
364+
// Decide: Is this still needed?
365+
// for _, listener := range l.listeners {
366+
// unusedTGs := listener.GetRules().FindUnusedTGs(l.targetgroups)
367+
// for _, t := range unusedTGs {
368+
// if err := albelbv2.ELBV2svc.RemoveTargetGroup(t.CurrentARN()); err != nil {
369+
// errors = append(errors, err)
370+
// continue
371+
// }
372+
// index, _ := l.targetgroups.FindById(t.ID)
373+
// l.targetgroups = append(l.targetgroups[:index], l.targetgroups[index+1:]...)
374+
// }
375+
// }
382376

383377
return errors
384378
}

pkg/alb/tg/targetgroup.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,13 @@ func (t *TargetGroup) Reconcile(rOpts *ReconcileOptions) error {
144144
// No DesiredState means target group may not be needed.
145145
// However, target groups aren't deleted until after rules are created
146146
// Ensuring we know what target groups are truly no longer in use.
147-
case t.tg.desired == nil:
148-
t.deleted = true
149-
return nil
147+
case t.tg.desired == nil && !rOpts.IgnoreDeletes:
148+
t.logger.Infof("Start TargetGroup deletion.")
149+
if err := t.delete(rOpts); err != nil {
150+
return err
151+
}
152+
rOpts.Eventf(api.EventTypeNormal, "DELETE", "%v target group deleted", t.ID)
153+
t.logger.Infof("Completed TargetGroup deletion.")
150154

151155
// No CurrentState means target group doesn't exist in AWS and should be created.
152156
case t.tg.current == nil:
@@ -302,6 +306,16 @@ func (t *TargetGroup) modify(mods tgChange, rOpts *ReconcileOptions) error {
302306
return nil
303307
}
304308

309+
// delete a TargetGroup.
310+
func (t *TargetGroup) delete(rOpts *ReconcileOptions) error {
311+
if err := albelbv2.ELBV2svc.RemoveTargetGroup(t.CurrentARN()); err != nil {
312+
rOpts.Eventf(api.EventTypeWarning, "ERROR", "Error deleting %v target group: %s", t.ID, err.Error())
313+
return err
314+
}
315+
t.deleted = true
316+
return nil
317+
}
318+
305319
func (t *TargetGroup) needsModification() tgChange {
306320
var changes tgChange
307321

pkg/alb/tg/targetgroups.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,17 @@ func (t TargetGroups) FindCurrentByARN(id string) (int, *TargetGroup) {
4848
// Reconcile kicks off the state synchronization for every target group inside this TargetGroups
4949
// instance. It returns the new TargetGroups its created and a list of TargetGroups it believes
5050
// should be cleaned up.
51-
func (t TargetGroups) Reconcile(rOpts *ReconcileOptions) (TargetGroups, TargetGroups, error) {
51+
func (t TargetGroups) Reconcile(rOpts *ReconcileOptions) (TargetGroups, error) {
5252
var output TargetGroups
53-
var deleted TargetGroups
53+
5454
for _, tg := range t {
5555
if err := tg.Reconcile(rOpts); err != nil {
56-
return nil, nil, err
57-
}
58-
if tg.deleted {
59-
deleted = append(deleted, tg)
56+
return nil, err
6057
}
6158
output = append(output, tg)
6259
}
6360

64-
return output, deleted, nil
61+
return output, nil
6562
}
6663

6764
// StripDesiredState removes the Tags.Desired, DesiredTargetGroup, and Targets.Desired from all TargetGroups

pkg/alb/tg/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type ReconcileOptions struct {
4848
Eventf func(string, string, string, ...interface{})
4949
VpcID *string
5050
ManagedSGInstance *string
51+
IgnoreDeletes bool
5152
}
5253

5354
type tgChange uint

0 commit comments

Comments
 (0)