Skip to content
Prev Previous commit
Next Next commit
dedup lifecycle action completion code
  • Loading branch information
cjerad committed Apr 7, 2022
commit 186caec85a0072c85ed39b162dc1dccfc06166b9
55 changes: 55 additions & 0 deletions src/pkg/event/asgterminate/lifecycleaction/complete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Copyright 2022 Amazon.com, Inc. or its affiliates. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package lifecycleaction

import (
"context"
"errors"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/autoscaling"
)

type (
ASGLifecycleActionCompleter interface {
CompleteLifecycleActionWithContext(aws.Context, *autoscaling.CompleteLifecycleActionInput, ...request.Option) (*autoscaling.CompleteLifecycleActionOutput, error)
}

Input struct {
AutoScalingGroupName string
LifecycleActionToken string
LifecycleHookName string
EC2InstanceID string
}
)

func Complete(ctx context.Context, completer ASGLifecycleActionCompleter, input Input) (bool, error) {
if _, err := completer.CompleteLifecycleActionWithContext(ctx, &autoscaling.CompleteLifecycleActionInput{
AutoScalingGroupName: aws.String(input.AutoScalingGroupName),
LifecycleActionResult: aws.String("CONTINUE"),
LifecycleHookName: aws.String(input.LifecycleHookName),
LifecycleActionToken: aws.String(input.LifecycleActionToken),
InstanceId: aws.String(input.EC2InstanceID),
}); err != nil {
var f awserr.RequestFailure
return errors.As(err, &f) && f.StatusCode() != 400, err
}

return false, nil
}
23 changes: 7 additions & 16 deletions src/pkg/event/asgterminate/v1/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ package v1

import (
"context"
"errors"

"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-node-termination-handler/pkg/event/asgterminate/lifecycleaction"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
Expand All @@ -38,18 +35,12 @@ func (e EC2InstanceTerminateLifecycleAction) EC2InstanceIDs() []string {
}

func (e EC2InstanceTerminateLifecycleAction) Done(ctx context.Context) (bool, error) {
if _, err := e.CompleteLifecycleActionWithContext(ctx, &autoscaling.CompleteLifecycleActionInput{
AutoScalingGroupName: aws.String(e.Detail.AutoScalingGroupName),
LifecycleActionResult: aws.String("CONTINUE"),
LifecycleHookName: aws.String(e.Detail.LifecycleHookName),
LifecycleActionToken: aws.String(e.Detail.LifecycleActionToken),
InstanceId: aws.String(e.Detail.EC2InstanceID),
}); err != nil {
var f awserr.RequestFailure
return errors.As(err, &f) && f.StatusCode() != 400, err
}

return false, nil
return lifecycleaction.Complete(ctx, e, lifecycleaction.Input{
AutoScalingGroupName: e.Detail.AutoScalingGroupName,
LifecycleActionToken: e.Detail.LifecycleActionToken,
LifecycleHookName: e.Detail.LifecycleHookName,
EC2InstanceID: e.Detail.EC2InstanceID,
})
}

func (e EC2InstanceTerminateLifecycleAction) MarshalLogObject(enc zapcore.ObjectEncoder) error {
Expand Down
23 changes: 7 additions & 16 deletions src/pkg/event/asgterminate/v2/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ package v2

import (
"context"
"errors"

"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-node-termination-handler/pkg/event/asgterminate/lifecycleaction"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
Expand All @@ -38,18 +35,12 @@ func (e EC2InstanceTerminateLifecycleAction) EC2InstanceIDs() []string {
}

func (e EC2InstanceTerminateLifecycleAction) Done(ctx context.Context) (bool, error) {
if _, err := e.CompleteLifecycleActionWithContext(ctx, &autoscaling.CompleteLifecycleActionInput{
AutoScalingGroupName: aws.String(e.Detail.AutoScalingGroupName),
LifecycleActionResult: aws.String("CONTINUE"),
LifecycleHookName: aws.String(e.Detail.LifecycleHookName),
LifecycleActionToken: aws.String(e.Detail.LifecycleActionToken),
InstanceId: aws.String(e.Detail.EC2InstanceID),
}); err != nil {
var f awserr.RequestFailure
return errors.As(err, &f) && f.StatusCode() != 400, err
}

return false, nil
return lifecycleaction.Complete(ctx, e, lifecycleaction.Input{
AutoScalingGroupName: e.Detail.AutoScalingGroupName,
LifecycleActionToken: e.Detail.LifecycleActionToken,
LifecycleHookName: e.Detail.LifecycleHookName,
EC2InstanceID: e.Detail.EC2InstanceID,
})
}

func (e EC2InstanceTerminateLifecycleAction) MarshalLogObject(enc zapcore.ObjectEncoder) error {
Expand Down