diff --git a/upup/pkg/fi/cloudup/awstasks/autoscalinggroup.go b/upup/pkg/fi/cloudup/awstasks/autoscalinggroup.go index 69506fb4d11f6..fa63732bf9358 100644 --- a/upup/pkg/fi/cloudup/awstasks/autoscalinggroup.go +++ b/upup/pkg/fi/cloudup/awstasks/autoscalinggroup.go @@ -32,6 +32,7 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/autoscaling" "github.com/aws/aws-sdk-go/service/ec2" + "github.com/aws/aws-sdk-go/service/elb" "k8s.io/klog/v2" ) @@ -124,6 +125,37 @@ func (e *AutoscalingGroup) Find(c *fi.Context) (*AutoscalingGroup, error) { }) } + { + // pkg/model/awsmodel/autoscalinggroup.go doesn't know the LoadBalancerName of the API ELB task that it sets up + // it only knows the LoadBalancerName of external load balancers passed through the InstanceGroupSpec + // We lookup the LoadBalancerName for LoadBalancer tasks that don't have it set, in order to attach the LB to the ASG + // To prevent spurious changes being reported, we need to regenerate those tasks to be identical when Finding them from AWS + // Because we don't know whether any given LoadBalancerName attached to an ASG is the API ELB task or not, + // we have to find the API ELB task, lookup its LoadBalancerName, and then compare that to the list of attached LoadBalancers + // in order to make the tasks returned from Find identical to the ones generated by the model. + var apiLBTask *LoadBalancer + var apiLBDesc *elb.LoadBalancerDescription + for _, lb := range e.LoadBalancers { + if !fi.BoolValue(lb.Shared) { + apiLBTask = lb + } + } + if apiLBTask != nil { + apiLBDesc, err = FindLoadBalancerByNameTag(c.Cloud.(awsup.AWSCloud), fi.StringValue(apiLBTask.Name)) + if err != nil { + return nil, err + } + if apiLBDesc != nil { + for i := 0; i < len(actual.LoadBalancers); i++ { + lb := actual.LoadBalancers[i] + if aws.StringValue(apiLBDesc.LoadBalancerName) == aws.StringValue(lb.Name) { + actual.LoadBalancers[i] = apiLBTask + } + } + } + } + } + for _, tg := range g.TargetGroupARNs { actual.TargetGroups = append(actual.TargetGroups, &TargetGroup{ARN: aws.String(*tg)}) }