Skip to content

Commit d325781

Browse files
committed
fix error handling for missing instances
1 parent 85e022b commit d325781

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

pkg/monitor/sqsevent/sqs-monitor.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import (
1717
"encoding/json"
1818
"errors"
1919
"fmt"
20-
2120
"github.com/aws/aws-node-termination-handler/pkg/monitor"
2221
"github.com/aws/aws-sdk-go/aws"
22+
"github.com/aws/aws-sdk-go/aws/awserr"
2323
"github.com/aws/aws-sdk-go/service/autoscaling"
2424
"github.com/aws/aws-sdk-go/service/autoscaling/autoscalingiface"
2525
"github.com/aws/aws-sdk-go/service/ec2"
@@ -88,7 +88,7 @@ func (m SQSMonitor) Monitor() error {
8888
}
8989

9090
if len(messages) > 0 && failedEvents == len(messages) {
91-
return fmt.Errorf("All of the waiting queue events could not be processed")
91+
return fmt.Errorf("none of the waiting queue events could be processed")
9292
}
9393

9494
return nil
@@ -189,10 +189,14 @@ func (m SQSMonitor) retrieveNodeName(instanceID string) (string, error) {
189189
},
190190
})
191191
if err != nil {
192+
if aerr, ok := err.(awserr.Error); ok && aerr.Code() == "InvalidInstanceID.NotFound" {
193+
log.Warn().Msgf("No instance found with instance-id %s", instanceID)
194+
return "", ErrNodeStateNotRunning
195+
}
192196
return "", err
193197
}
194198
if len(result.Reservations) == 0 || len(result.Reservations[0].Instances) == 0 {
195-
log.Info().Msgf("No instance found with instance-id %s", instanceID)
199+
log.Warn().Msgf("No instance found with instance-id %s", instanceID)
196200
return "", ErrNodeStateNotRunning
197201
}
198202

pkg/monitor/sqsevent/sqs-monitor_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,42 @@ func TestMonitor_EC2NoInstances(t *testing.T) {
485485
}
486486
}
487487

488+
func TestMonitor_DescribeInstancesError(t *testing.T) {
489+
for _, event := range []sqsevent.EventBridgeEvent{spotItnEvent, asgLifecycleEvent} {
490+
msg, err := getSQSMessageFromEvent(event)
491+
h.Ok(t, err)
492+
messages := []*sqs.Message{
493+
&msg,
494+
}
495+
sqsMock := h.MockedSQS{
496+
ReceiveMessageResp: sqs.ReceiveMessageOutput{Messages: messages},
497+
ReceiveMessageErr: nil,
498+
}
499+
ec2Mock := h.MockedEC2{
500+
DescribeInstancesResp: ec2.DescribeInstancesOutput{},
501+
DescribeInstancesErr: awserr.New("InvalidInstanceID.NotFound", "The instance ID 'i-0d6bd3ce2bf8a6751' does not exist\n\tstatus code: 400, request id: 6a5c30e2-922d-464c-946c-a1ec76e5920b", fmt.Errorf("original error")),
502+
}
503+
drainChan := make(chan monitor.InterruptionEvent, 1)
504+
505+
sqsMonitor := sqsevent.SQSMonitor{
506+
SQS: sqsMock,
507+
EC2: ec2Mock,
508+
QueueURL: "https://test-queue",
509+
InterruptionChan: drainChan,
510+
}
511+
512+
err = sqsMonitor.Monitor()
513+
h.Ok(t, err)
514+
515+
select {
516+
case <-drainChan:
517+
h.Ok(t, fmt.Errorf("Expected no events"))
518+
default:
519+
h.Ok(t, nil)
520+
}
521+
}
522+
}
523+
488524
func TestMonitor_EC2NoDNSName(t *testing.T) {
489525
msg, err := getSQSMessageFromEvent(asgLifecycleEvent)
490526
h.Ok(t, err)

0 commit comments

Comments
 (0)