Skip to content

Commit

Permalink
Add SQS InterruptionEvent.InstanceType (#1104)
Browse files Browse the repository at this point in the history
* Add SQS InterruptionEvent.InstanceType

* Fix indentation

* Merge InstanceType fields in webhook.combinedDrainData

* Fix formatting
  • Loading branch information
hari-rad authored Dec 31, 2024
1 parent 90f6262 commit 32c9ad3
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 14 deletions.
1 change: 1 addition & 0 deletions pkg/monitor/sqsevent/asg-lifecycle-event.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func (m SQSMonitor) asgTerminationToInterruptionEvent(event *EventBridgeEvent, m
IsManaged: nodeInfo.IsManaged,
InstanceID: lifecycleDetail.EC2InstanceID,
ProviderID: nodeInfo.ProviderID,
InstanceType: nodeInfo.InstanceType,
Description: fmt.Sprintf("ASG Lifecycle Termination event received. Instance will be interrupted at %s \n", event.getTime()),
}

Expand Down
1 change: 1 addition & 0 deletions pkg/monitor/sqsevent/ec2-state-change-event.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func (m SQSMonitor) ec2StateChangeToInterruptionEvent(event *EventBridgeEvent, m
AutoScalingGroupName: nodeInfo.AsgName,
InstanceID: ec2StateChangeDetail.InstanceID,
ProviderID: nodeInfo.ProviderID,
InstanceType: nodeInfo.InstanceType,
Description: fmt.Sprintf("EC2 State Change event received. Instance %s went into %s at %s \n", ec2StateChangeDetail.InstanceID, ec2StateChangeDetail.State, event.getTime()),
}

Expand Down
1 change: 1 addition & 0 deletions pkg/monitor/sqsevent/rebalance-recommendation-event.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func (m SQSMonitor) rebalanceRecommendationToInterruptionEvent(event *EventBridg
IsManaged: nodeInfo.IsManaged,
InstanceID: nodeInfo.InstanceID,
ProviderID: nodeInfo.ProviderID,
InstanceType: nodeInfo.InstanceType,
Description: fmt.Sprintf("Rebalance recommendation event received. Instance %s will be cordoned at %s \n", rebalanceRecDetail.InstanceID, event.getTime()),
}
interruptionEvent.PostDrainTask = func(interruptionEvent monitor.InterruptionEvent, n node.Node) error {
Expand Down
1 change: 1 addition & 0 deletions pkg/monitor/sqsevent/scheduled-change-event.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func (m SQSMonitor) scheduledEventToInterruptionEvents(event *EventBridgeEvent,
NodeName: nodeInfo.Name,
InstanceID: nodeInfo.InstanceID,
ProviderID: nodeInfo.ProviderID,
InstanceType: nodeInfo.InstanceType,
IsManaged: nodeInfo.IsManaged,
Description: fmt.Sprintf("AWS Health scheduled change event received. Instance %s will be interrupted at %s \n", nodeInfo.InstanceID, event.getTime()),
}
Expand Down
1 change: 1 addition & 0 deletions pkg/monitor/sqsevent/spot-itn-event.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func (m SQSMonitor) spotITNTerminationToInterruptionEvent(event *EventBridgeEven
IsManaged: nodeInfo.IsManaged,
InstanceID: spotInterruptionDetail.InstanceID,
ProviderID: nodeInfo.ProviderID,
InstanceType: nodeInfo.InstanceType,
Description: fmt.Sprintf("Spot Interruption notice for instance %s was sent at %s \n", spotInterruptionDetail.InstanceID, event.getTime()),
}
interruptionEvent.PostDrainTask = func(interruptionEvent monitor.InterruptionEvent, n node.Node) error {
Expand Down
24 changes: 13 additions & 11 deletions pkg/monitor/sqsevent/sqs-monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,12 +340,13 @@ func (m SQSMonitor) completeLifecycleAction(input *autoscaling.CompleteLifecycle

// NodeInfo is relevant information about a single node
type NodeInfo struct {
AsgName string
InstanceID string
ProviderID string
IsManaged bool
Name string
Tags map[string]string
AsgName string
InstanceID string
ProviderID string
InstanceType string
IsManaged bool
Name string
Tags map[string]string
}

// getNodeInfo returns the NodeInfo record for the given instanceID.
Expand Down Expand Up @@ -411,11 +412,12 @@ func (m SQSMonitor) getNodeInfo(instanceID string) (*NodeInfo, error) {
}

nodeInfo := &NodeInfo{
Name: *instance.PrivateDnsName,
InstanceID: instanceID,
ProviderID: providerID,
Tags: make(map[string]string),
IsManaged: true,
Name: *instance.PrivateDnsName,
InstanceID: instanceID,
ProviderID: providerID,
InstanceType: *instance.InstanceType,
Tags: make(map[string]string),
IsManaged: true,
}
for _, t := range (*instance).Tags {
nodeInfo.Tags[*t.Key] = *t.Value
Expand Down
1 change: 1 addition & 0 deletions pkg/monitor/sqsevent/sqs-monitor_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ func getDescribeInstancesResp(instanceID string, privateDNSName string, tags map
GroupName: aws.String(""),
Tenancy: aws.String("default"),
},
InstanceType: aws.String("t3.medium"),
PrivateDnsName: aws.String(privateDNSName),
Tags: awsTags,
},
Expand Down
1 change: 1 addition & 0 deletions pkg/monitor/sqsevent/sqs-monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,7 @@ func getDescribeInstancesResp(privateDNSName string, withASGTag bool, withManage
GroupName: aws.String(""),
Tenancy: aws.String("default"),
},
InstanceType: aws.String("t3.medium"),
PrivateDnsName: &privateDNSName,
Tags: tags,
},
Expand Down
1 change: 1 addition & 0 deletions pkg/monitor/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type InterruptionEvent struct {
Pods []string
InstanceID string
ProviderID string
InstanceType string
IsManaged bool
StartTime time.Time
EndTime time.Time
Expand Down
12 changes: 9 additions & 3 deletions pkg/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ import (
type combinedDrainData struct {
ec2metadata.NodeMetadata
monitor.InterruptionEvent
InstanceID string
InstanceID string
InstanceType string
}

// Post makes a http post to send drain event data to webhook url
Expand All @@ -60,12 +61,17 @@ func Post(additionalInfo ec2metadata.NodeMetadata, event *monitor.InterruptionEv
return
}

// Need to merge the two data sources manually since both have an InstanceID field
// Need to merge the two data sources manually since both have
// InstanceID and InstanceType fields
instanceID := additionalInfo.InstanceID
if event.InstanceID != "" {
instanceID = event.InstanceID
}
var combined = combinedDrainData{NodeMetadata: additionalInfo, InterruptionEvent: *event, InstanceID: instanceID}
instanceType := additionalInfo.InstanceType
if event.InstanceType != "" {
instanceType = event.InstanceType
}
var combined = combinedDrainData{NodeMetadata: additionalInfo, InterruptionEvent: *event, InstanceID: instanceID, InstanceType: instanceType}

var byteBuffer bytes.Buffer
err = webhookTemplate.Execute(&byteBuffer, combined)
Expand Down

0 comments on commit 32c9ad3

Please sign in to comment.