func()

in pkg/monitor/sqsevent/sqs-monitor.go [192:239]


func (m SQSMonitor) processEventBridgeEvent(eventBridgeEvent *EventBridgeEvent, message *sqs.Message) []InterruptionEventWrapper {
	interruptionEventWrappers := []InterruptionEventWrapper{}
	interruptionEvent := &monitor.InterruptionEvent{}
	var err error

	if eventBridgeEvent == nil {
		return append(interruptionEventWrappers, InterruptionEventWrapper{nil, fmt.Errorf("eventBridgeEvent is nil")})
	}
	if message == nil {
		return append(interruptionEventWrappers, InterruptionEventWrapper{nil, fmt.Errorf("message is nil")})
	}

	switch eventBridgeEvent.Source {
	case "aws.autoscaling":
		lifecycleEvent := LifecycleDetail{}
		err = json.Unmarshal([]byte(eventBridgeEvent.Detail), &lifecycleEvent)
		if err != nil {
			interruptionEvent, err = nil, fmt.Errorf("unmarshaling message, %s, from ASG lifecycle event: %w", *message.MessageId, err)
			interruptionEventWrappers = append(interruptionEventWrappers, InterruptionEventWrapper{interruptionEvent, err})
		}
		if lifecycleEvent.LifecycleTransition == ASGLaunchingLifecycleTransition {
			interruptionEvent, err = m.createAsgInstanceLaunchEvent(eventBridgeEvent, message)
			interruptionEventWrappers = append(interruptionEventWrappers, InterruptionEventWrapper{interruptionEvent, err})
		} else if lifecycleEvent.LifecycleTransition == ASGTerminatingLifecycleTransition {
			interruptionEvent, err = m.asgTerminationToInterruptionEvent(eventBridgeEvent, message)
			interruptionEventWrappers = append(interruptionEventWrappers, InterruptionEventWrapper{interruptionEvent, err})
		}
		return interruptionEventWrappers

	case "aws.ec2":
		if eventBridgeEvent.DetailType == "EC2 Instance State-change Notification" {
			interruptionEvent, err = m.ec2StateChangeToInterruptionEvent(eventBridgeEvent, message)
		} else if eventBridgeEvent.DetailType == "EC2 Spot Instance Interruption Warning" {
			interruptionEvent, err = m.spotITNTerminationToInterruptionEvent(eventBridgeEvent, message)
		} else if eventBridgeEvent.DetailType == "EC2 Instance Rebalance Recommendation" {
			interruptionEvent, err = m.rebalanceRecommendationToInterruptionEvent(eventBridgeEvent, message)
		}
		return append(interruptionEventWrappers, InterruptionEventWrapper{interruptionEvent, err})

	case "aws.health":
		if eventBridgeEvent.DetailType == "AWS Health Event" {
			return m.scheduledEventToInterruptionEvents(eventBridgeEvent, message)
		}
	}

	err = fmt.Errorf("event source (%s) is not supported", eventBridgeEvent.Source)
	return append(interruptionEventWrappers, InterruptionEventWrapper{nil, err})
}