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

drain container instance on termination #1692

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 41 additions & 0 deletions provider/aws/lambda/lifecycle/main.go
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"os"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
Expand Down Expand Up @@ -90,6 +91,20 @@ func handle(r Record) error {

fmt.Printf("ci = %+v\n", ci)

if _, err := ECS.UpdateContainerInstancesState(&ecs.UpdateContainerInstancesStateInput{
ContainerInstances: []*string{
aws.String(ci),
},
Status: aws.String("DRAINING"),
Cluster: aws.String(md.Cluster),
}); err != nil {
return err
}

if err := waitForInstanceDrain(md.Cluster, ci); err != nil {
return err
}

if _, err := ECS.DeregisterContainerInstance(&ecs.DeregisterContainerInstanceInput{
Cluster: aws.String(md.Cluster),
ContainerInstance: aws.String(ci),
Expand All @@ -114,6 +129,32 @@ func handle(r Record) error {
return nil
}

func waitForInstanceDrain(cluster, ci string) error {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to use one of the Waiters in the SDK for this?

Copy link
Contributor Author

@MiguelMoll MiguelMoll Feb 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we can and worth a shot. I went this route first to avoid any state changes that happen when calling the Waiter with tasks grabbed before or after instance change to DRAINING.

Either way, a call to ListTasks() (w/ next pages in mind for a waiter) is done. Then pass those task into the waiter. Figured we have the data already we could check it directly.

params := &ecs.ListTasksInput{
Cluster: aws.String(cluster),
ContainerInstance: aws.String(ci),
DesiredStatus: aws.String("RUNNING"),
}

for {
time.Sleep(10 * time.Second)

resp, err := ECS.ListTasks(params)
if err != nil {
return err
}

if len(resp.TaskArns) > 0 {
continue
}

break
}

return nil
}

func metadata() (*Metadata, error) {
var md Metadata

Expand Down