Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix error handling for missing instances #409

Merged
merged 1 commit into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions pkg/monitor/sqsevent/sqs-monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import (
"encoding/json"
"errors"
"fmt"

"github.com/aws/aws-node-termination-handler/pkg/monitor"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/autoscaling/autoscalingiface"
"github.com/aws/aws-sdk-go/service/ec2"
Expand Down Expand Up @@ -88,7 +88,7 @@ func (m SQSMonitor) Monitor() error {
}

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

return nil
Expand Down Expand Up @@ -189,10 +189,14 @@ func (m SQSMonitor) retrieveNodeName(instanceID string) (string, error) {
},
})
if err != nil {
if aerr, ok := err.(awserr.Error); ok && aerr.Code() == "InvalidInstanceID.NotFound" {
log.Warn().Msgf("No instance found with instance-id %s", instanceID)
return "", ErrNodeStateNotRunning
}
return "", err
}
if len(result.Reservations) == 0 || len(result.Reservations[0].Instances) == 0 {
log.Info().Msgf("No instance found with instance-id %s", instanceID)
log.Warn().Msgf("No instance found with instance-id %s", instanceID)
return "", ErrNodeStateNotRunning
}

Expand Down
36 changes: 36 additions & 0 deletions pkg/monitor/sqsevent/sqs-monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,42 @@ func TestMonitor_EC2NoInstances(t *testing.T) {
}
}

func TestMonitor_DescribeInstancesError(t *testing.T) {
for _, event := range []sqsevent.EventBridgeEvent{spotItnEvent, asgLifecycleEvent} {
msg, err := getSQSMessageFromEvent(event)
h.Ok(t, err)
messages := []*sqs.Message{
&msg,
}
sqsMock := h.MockedSQS{
ReceiveMessageResp: sqs.ReceiveMessageOutput{Messages: messages},
ReceiveMessageErr: nil,
}
ec2Mock := h.MockedEC2{
DescribeInstancesResp: ec2.DescribeInstancesOutput{},
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")),
}
drainChan := make(chan monitor.InterruptionEvent, 1)

sqsMonitor := sqsevent.SQSMonitor{
SQS: sqsMock,
EC2: ec2Mock,
QueueURL: "https://test-queue",
InterruptionChan: drainChan,
}

err = sqsMonitor.Monitor()
h.Ok(t, err)

select {
case <-drainChan:
h.Ok(t, fmt.Errorf("Expected no events"))
default:
h.Ok(t, nil)
}
}
}

func TestMonitor_EC2NoDNSName(t *testing.T) {
msg, err := getSQSMessageFromEvent(asgLifecycleEvent)
h.Ok(t, err)
Expand Down