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

Add capability to reap EC2 nodes not managed by ASG #72

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
46 changes: 35 additions & 11 deletions pkg/reaper/nodereaper/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,41 @@ func (ctx *ReaperContext) uncordonNode(name string, dryRun bool, ignoreDrainFail
return nil
}

func (ctx *ReaperContext) terminateInstance(w autoscalingiface.AutoScalingAPI, id string, nodeName string) error {

terminateInput := &autoscaling.TerminateInstanceInAutoScalingGroupInput{
InstanceId: &id,
ShouldDecrementDesiredCapacity: aws.Bool(false),
}

_, err := w.TerminateInstanceInAutoScalingGroup(terminateInput)
if err != nil {
return err
}
func (ctx *ReaperContext) terminateInstance(w ReaperAwsAuth, id string, nodeName string) error {
describeInput := &autoscaling.DescribeAutoScalingInstancesInput{
InstanceIds: []*string{
aws.String(id),
},
}

response, derr := w.ASG.DescribeAutoScalingInstances(describeInput)
if derr != nil {
return derr
}

if len(response.AutoScalingInstances) == 0 {
terminateInput := &ec2.TerminateInstancesInput{
DryRun: aws.Bool(false),
InstanceIds: []*string{
aws.String(id),
},
}

_, err := w.EC2.TerminateInstances(terminateInput)
if err != nil {
return err
}
} else {
terminateInput := &autoscaling.TerminateInstanceInAutoScalingGroupInput{
InstanceId: &id,
ShouldDecrementDesiredCapacity: aws.Bool(false),
}

_, err := w.ASG.TerminateInstanceInAutoScalingGroup(terminateInput)
if err != nil {
return err
}
}

if err := ctx.annotateNode(nodeName, stateAnnotationKey, terminatedStateName); err != nil {
log.Warnf("failed to update state annotation on node '%v'", nodeName)
Expand Down
4 changes: 2 additions & 2 deletions pkg/reaper/nodereaper/nodereaper.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ func (ctx *ReaperContext) reapOldNodes(w ReaperAwsAuth) error {

if !ctx.DryRun {
log.Infof("reaping old node %v -> %v", instance.NodeName, instance.InstanceID)
err = ctx.terminateInstance(w.ASG, instance.InstanceID, instance.NodeName)
err = ctx.terminateInstance(w, instance.InstanceID, instance.NodeName)
if err != nil {
return err
}
Expand Down Expand Up @@ -623,7 +623,7 @@ func (ctx *ReaperContext) reapUnhealthyNodes(w ReaperAwsAuth) error {
if !ctx.DryRun {
log.Infof("reaping unhealthy node %v -> %v", instance.NodeName, instance)

err = ctx.terminateInstance(w.ASG, instance.InstanceID, instance.NodeName)
err = ctx.terminateInstance(w, instance.InstanceID, instance.NodeName)
if err != nil {
return err
}
Expand Down