Skip to content

Commit

Permalink
add a flag to select target types to deregister instance from (#214)
Browse files Browse the repository at this point in the history
* add deregister-target-types flag to select target types to deregister

Signed-off-by: Yuxuan Zhu <[email protected]>

* upgrade golang image to 1.21

Signed-off-by: Yuxuan Zhu <[email protected]>

* update readme

Signed-off-by: Yuxuan Zhu <[email protected]>

* add debug

Signed-off-by: Yuxuan Zhu <[email protected]>

* remove debug logging

Signed-off-by: Yuxuan Zhu <[email protected]>

* revert golang version to 1.19

Signed-off-by: Yuxuan Zhu <[email protected]>

---------

Signed-off-by: Yuxuan Zhu <[email protected]>
Co-authored-by: Yuxuan Zhu <[email protected]>
Co-authored-by: Todd Ekenstam <[email protected]>
  • Loading branch information
3 people authored Sep 9, 2024
1 parent ad9b7d7 commit f3b1488
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ time="2020-03-11T07:24:49Z" level=info msg="event ce25c321-ec67-3f0b-c156-a7c1f7
| polling-interval | 10 | Int | interval in seconds for which to poll SQS |
| with-deregister | true | Bool | try to deregister deleting instance from target groups |
| refresh-expired-credentials | false | Bool | refreshes expired credentials (requires shared credentials file) |
| deregister-target-types | "classic-elb,target-group" | String | comma separated list of target types to deregister instance from (classic-elb, target-group) |


## Release History
Expand Down
5 changes: 5 additions & 0 deletions cmd/serve.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"fmt"
"os"
"time"

Expand Down Expand Up @@ -30,6 +31,7 @@ var (
nodeName string
logLevel string
deregisterTargetGroups bool
deregisterTargetTypes []string
refreshExpiredCredentials bool
drainRetryIntervalSeconds int
maxDrainConcurrency int64
Expand Down Expand Up @@ -83,6 +85,7 @@ var serveCmd = &cobra.Command{
DrainRetryAttempts: uint(drainRetryAttempts),
Region: region,
WithDeregister: deregisterTargetGroups,
DeregisterTargetTypes: deregisterTargetTypes,
}

s := service.New(auth, context)
Expand All @@ -105,6 +108,8 @@ func init() {
serveCmd.Flags().IntVar(&drainRetryAttempts, "drain-retries", 3, "number of times to retry the node drain operation")
serveCmd.Flags().IntVar(&pollingIntervalSeconds, "polling-interval", 10, "interval in seconds for which to poll SQS")
serveCmd.Flags().BoolVar(&deregisterTargetGroups, "with-deregister", true, "try to deregister deleting instance from target groups")
serveCmd.Flags().StringSliceVar(&deregisterTargetTypes, "deregister-target-types", []string{service.TargetTypeClassicELB.String(), service.TargetTypeTargetGroup.String()},
fmt.Sprintf("comma separated list of target types to deregister instance from (%s, %s)", service.TargetTypeClassicELB.String(), service.TargetTypeTargetGroup.String()))
serveCmd.Flags().BoolVar(&refreshExpiredCredentials, "refresh-expired-credentials", false, "refreshes expired credentials (requires shared credentials file)")
}

Expand Down
1 change: 1 addition & 0 deletions pkg/service/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type ManagerContext struct {
DrainRetryAttempts uint
PollingIntervalSeconds int64
WithDeregister bool
DeregisterTargetTypes []string
MaxDrainConcurrency *semaphore.Weighted
MaxTimeToProcessSeconds int64
}
Expand Down
31 changes: 19 additions & 12 deletions pkg/service/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"math/rand"
"runtime"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -73,6 +74,7 @@ func (mgr *Manager) Start() {
log.Infof("node drain retry interval seconds = %v", ctx.DrainRetryIntervalSeconds)
log.Infof("node drain retry attempts = %v", ctx.DrainRetryAttempts)
log.Infof("with alb deregister = %v", ctx.WithDeregister)
log.Infof("deregister target types = %v", ctx.DeregisterTargetTypes)

// start metrics server
log.Infof("starting metrics server on %v%v", MetricsEndpoint, MetricsPort)
Expand Down Expand Up @@ -305,6 +307,7 @@ func (mgr *Manager) deleteNodeTarget(event *LifecycleEvent) error {

func (mgr *Manager) scanMembership(event *LifecycleEvent) (*ScanResult, error) {
var (
ctx = &mgr.context
elbv2Client = mgr.authenticator.ELBv2Client
elbClient = mgr.authenticator.ELBClient
instanceID = event.EC2InstanceID
Expand All @@ -315,22 +318,26 @@ func (mgr *Manager) scanMembership(event *LifecycleEvent) (*ScanResult, error) {

// get all target groups
targetGroups := []*elbv2.TargetGroup{}
err := elbv2Client.DescribeTargetGroupsPages(&elbv2.DescribeTargetGroupsInput{}, func(page *elbv2.DescribeTargetGroupsOutput, lastPage bool) bool {
targetGroups = append(targetGroups, page.TargetGroups...)
return page.NextMarker != nil
})
if err != nil {
return scanResult, err
if slices.Contains(ctx.DeregisterTargetTypes, TargetTypeTargetGroup.String()) {
err := elbv2Client.DescribeTargetGroupsPages(&elbv2.DescribeTargetGroupsInput{}, func(page *elbv2.DescribeTargetGroupsOutput, lastPage bool) bool {
targetGroups = append(targetGroups, page.TargetGroups...)
return page.NextMarker != nil
})
if err != nil {
return scanResult, err
}
}

// get all classic elbs
elbDescriptions := []*elb.LoadBalancerDescription{}
err = elbClient.DescribeLoadBalancersPages(&elb.DescribeLoadBalancersInput{}, func(page *elb.DescribeLoadBalancersOutput, lastPage bool) bool {
elbDescriptions = append(elbDescriptions, page.LoadBalancerDescriptions...)
return page.NextMarker != nil
})
if err != nil {
return scanResult, err
if slices.Contains(ctx.DeregisterTargetTypes, TargetTypeClassicELB.String()) {
err := elbClient.DescribeLoadBalancersPages(&elb.DescribeLoadBalancersInput{}, func(page *elb.DescribeLoadBalancersOutput, lastPage bool) bool {
elbDescriptions = append(elbDescriptions, page.LoadBalancerDescriptions...)
return page.NextMarker != nil
})
if err != nil {
return scanResult, err
}
}

log.Infof("%v> checking targetgroup/elb membership", instanceID)
Expand Down
2 changes: 2 additions & 0 deletions pkg/service/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ func Test_HandleEventWithDeregister(t *testing.T) {

ctx := _newBasicContext()
ctx.WithDeregister = true
ctx.DeregisterTargetTypes = []string{TargetTypeClassicELB.String(), TargetTypeTargetGroup.String()}

fakeNodes := []v1.Node{
{
Expand Down Expand Up @@ -398,6 +399,7 @@ func Test_HandleEventWithDeregisterError(t *testing.T) {

ctx := _newBasicContext()
ctx.WithDeregister = true
ctx.DeregisterTargetTypes = []string{TargetTypeClassicELB.String(), TargetTypeTargetGroup.String()}

fakeNodes := []v1.Node{
{
Expand Down

0 comments on commit f3b1488

Please sign in to comment.