Skip to content

Commit 44f8012

Browse files
committed
Remove unused ASG calls since tags should propagate to instance
1 parent 9a985a6 commit 44f8012

File tree

1 file changed

+2
-69
lines changed

1 file changed

+2
-69
lines changed

pkg/monitor/sqsevent/sqs-monitor.go

Lines changed: 2 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"github.com/aws/aws-node-termination-handler/pkg/monitor"
2222
"github.com/aws/aws-sdk-go/aws"
2323
"github.com/aws/aws-sdk-go/aws/awserr"
24-
"github.com/aws/aws-sdk-go/service/autoscaling"
2524
"github.com/aws/aws-sdk-go/service/autoscaling/autoscalingiface"
2625
"github.com/aws/aws-sdk-go/service/ec2"
2726
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
@@ -353,78 +352,12 @@ func (m SQSMonitor) getNodeInfo(instanceID string) (*NodeInfo, error) {
353352
}
354353
}
355354

356-
if m.CheckIfManaged {
357-
if nodeInfo.AsgName == "" {
358-
// If ASG tags are not propagated we might need to use the API
359-
// to retrieve the ASG name
360-
nodeInfo.AsgName, err = m.retrieveAutoScalingGroupName(nodeInfo.InstanceID)
361-
if err != nil {
362-
return nil, fmt.Errorf("unable to retrieve AutoScaling group: %w", err)
363-
}
364-
}
365-
if nodeInfo.Tags[m.ManagedAsgTag] == "" {
366-
// if ASG tags are not propagated we might have to check the ASG directly
367-
nodeInfo.IsManaged, err = m.isASGManaged(nodeInfo.AsgName, nodeInfo.InstanceID)
368-
if err != nil {
369-
return nil, err
370-
}
371-
}
355+
if m.CheckIfManaged && nodeInfo.Tags[m.ManagedAsgTag] == "" {
356+
nodeInfo.IsManaged = false
372357
}
373358

374359
infoJSON, _ := json.MarshalIndent(nodeInfo, " ", " ")
375360
log.Debug().Msgf("Got node info from AWS: %s", infoJSON)
376361

377362
return nodeInfo, nil
378363
}
379-
380-
// isASGManaged returns whether the autoscaling group should be managed by node termination handler
381-
func (m SQSMonitor) isASGManaged(asgName string, instanceID string) (bool, error) {
382-
if asgName == "" {
383-
return false, nil
384-
}
385-
asgFilter := autoscaling.Filter{Name: aws.String("auto-scaling-group"), Values: []*string{aws.String(asgName)}}
386-
asgDescribeTagsInput := autoscaling.DescribeTagsInput{
387-
Filters: []*autoscaling.Filter{&asgFilter},
388-
}
389-
isManaged := false
390-
err := m.ASG.DescribeTagsPages(&asgDescribeTagsInput, func(resp *autoscaling.DescribeTagsOutput, next bool) bool {
391-
for _, tag := range resp.Tags {
392-
if *tag.Key == m.ManagedAsgTag {
393-
isManaged = true
394-
// breaks paging loop
395-
return false
396-
}
397-
}
398-
// continue paging loop
399-
return true
400-
})
401-
402-
log.Debug().
403-
Str("instance_id", instanceID).
404-
Str("tag_key", m.ManagedAsgTag).
405-
Bool("is_managed", isManaged).
406-
Msg("directly checked if instance's Auto Scaling Group is managed")
407-
return isManaged, err
408-
}
409-
410-
// retrieveAutoScalingGroupName returns the autoscaling group name for a given instanceID
411-
func (m SQSMonitor) retrieveAutoScalingGroupName(instanceID string) (string, error) {
412-
asgDescribeInstanceInput := autoscaling.DescribeAutoScalingInstancesInput{
413-
InstanceIds: []*string{&instanceID},
414-
MaxRecords: aws.Int64(50),
415-
}
416-
asgs, err := m.ASG.DescribeAutoScalingInstances(&asgDescribeInstanceInput)
417-
if err != nil {
418-
return "", err
419-
}
420-
if len(asgs.AutoScalingInstances) == 0 {
421-
log.Debug().Str("instance_id", instanceID).Msg("Did not find an Auto Scaling Group for the given instance id")
422-
return "", nil
423-
}
424-
asgName := asgs.AutoScalingInstances[0].AutoScalingGroupName
425-
log.Debug().
426-
Str("instance_id", instanceID).
427-
Str("asg_name", *asgName).
428-
Msg("performed API lookup of instance ASG")
429-
return *asgName, nil
430-
}

0 commit comments

Comments
 (0)