Skip to content

Commit

Permalink
Attach/detach TGs from ASGs when using direct target
Browse files Browse the repository at this point in the history
  • Loading branch information
rifelpet committed Oct 29, 2020
1 parent b7f66a6 commit 7022f28
Showing 1 changed file with 58 additions and 2 deletions.
60 changes: 58 additions & 2 deletions upup/pkg/fi/cloudup/awstasks/autoscalinggroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,24 @@ func (v *AutoscalingGroup) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *Autos
detachLBRequest.LoadBalancerNames = e.getLBsToDetach(a.LoadBalancers)
}

changes.Tags = nil
changes.LoadBalancers = nil
}

var attachTGRequest *autoscaling.AttachLoadBalancerTargetGroupsInput
var detachTGRequest *autoscaling.DetachLoadBalancerTargetGroupsInput
if changes.TargetGroups != nil {
attachTGRequest = &autoscaling.AttachLoadBalancerTargetGroupsInput{
AutoScalingGroupName: e.Name,
TargetGroupARNs: e.AutoscalingTargetGroups(),
}

if a != nil && len(a.TargetGroups) > 0 {
detachTGRequest = &autoscaling.DetachLoadBalancerTargetGroupsInput{
AutoScalingGroupName: e.Name,
TargetGroupARNs: e.getTGsToDetach(a.TargetGroups),
}
}
changes.TargetGroups = nil
}

if changes.Metrics != nil || changes.Granularity != nil {
Expand Down Expand Up @@ -560,6 +577,16 @@ func (v *AutoscalingGroup) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *Autos
return fmt.Errorf("error attaching LoadBalancers: %v", err)
}
}
if detachTGRequest != nil {
if _, err := t.Cloud.Autoscaling().DetachLoadBalancerTargetGroups(detachTGRequest); err != nil {
return fmt.Errorf("error attaching TargetGroups: %v", err)
}
}
if attachTGRequest != nil {
if _, err := t.Cloud.Autoscaling().AttachLoadBalancerTargetGroups(attachTGRequest); err != nil {
return fmt.Errorf("error attaching TargetGroups: %v", err)
}
}
}

return nil
Expand Down Expand Up @@ -632,6 +659,17 @@ func (e *AutoscalingGroup) AutoscalingLoadBalancers() []*string {
return list
}

// AutoscalingTargetGroups returns a list of TGs attatched to the ASG
func (e *AutoscalingGroup) AutoscalingTargetGroups() []*string {
var list []*string

for _, v := range e.TargetGroups {
list = append(list, v.ARN)
}

return list
}

// processCompare returns processes that exist in a but not in b
func processCompare(a *[]string, b *[]string) []*string {
notInB := []*string{}
Expand Down Expand Up @@ -670,7 +708,7 @@ func (e *AutoscalingGroup) getASGTagsToDelete(currentTags map[string]string) []*
}

// getLBsToDetach loops through the currently set LBs and builds a list of
// LBs to be detach from the Autoscaling Group
// LBs to be detached from the Autoscaling Group
func (e *AutoscalingGroup) getLBsToDetach(currentLBs []*LoadBalancer) []*string {
lbsToDetach := []*string{}
desiredLBs := map[string]bool{}
Expand All @@ -687,6 +725,24 @@ func (e *AutoscalingGroup) getLBsToDetach(currentLBs []*LoadBalancer) []*string
return lbsToDetach
}

// getTGsToDetach loops through the currently set LBs and builds a list of
// target groups to be detached from the Autoscaling Group
func (e *AutoscalingGroup) getTGsToDetach(currentTGs []*TargetGroup) []*string {
tgsToDetach := []*string{}
desiredTGs := map[string]bool{}

for _, v := range e.TargetGroups {
desiredTGs[*v.ARN] = true
}

for _, v := range currentTGs {
if _, ok := desiredTGs[*v.ARN]; !ok {
tgsToDetach = append(tgsToDetach, v.Name)
}
}
return tgsToDetach
}

type terraformASGTag struct {
Key *string `json:"key" cty:"key"`
Value *string `json:"value" cty:"value"`
Expand Down

0 comments on commit 7022f28

Please sign in to comment.