-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix "N instances should be drained but only M instances was drained"
This commit fixes the following error: ``` Error: failed to replace instances: github.com/abicky/ecsmec/cmd.newRuntimeError github.com/abicky/ecsmec/cmd/root.go:38 - failed to terminate instances: github.com/abicky/ecsmec/internal/capacity.(*AutoScalingGroup).ReplaceInstances github.com/abicky/ecsmec/internal/capacity/autoscalinggroup.go:64 - failed to drain instances: github.com/abicky/ecsmec/internal/capacity.(*AutoScalingGroup).terminateInstances github.com/abicky/ecsmec/internal/capacity/autoscalinggroup.go:212 - 9 instances should be drained but only 8 instances was drained: github.com/abicky/ecsmec/internal/capacity.(*drainer).Drain github.com/abicky/ecsmec/internal/capacity/drainer.go:75 ```
- Loading branch information
Showing
7 changed files
with
164 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package capacity | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/ecs" | ||
"golang.org/x/xerrors" | ||
) | ||
|
||
type Cluster interface { | ||
Name() string | ||
WaitUntilContainerInstancesRegistered(context.Context, int, *time.Time) error | ||
} | ||
|
||
type cluster struct { | ||
name string | ||
ecsSvc ECSAPI | ||
} | ||
|
||
func NewCluster(name string, ecsSvc ECSAPI) Cluster { | ||
return &cluster{ | ||
name: name, | ||
ecsSvc: ecsSvc, | ||
} | ||
} | ||
|
||
func (c *cluster) Name() string { | ||
return c.name | ||
} | ||
|
||
func (c *cluster) WaitUntilContainerInstancesRegistered(ctx context.Context, count int, registeredAt *time.Time) error { | ||
if count == 0 { | ||
return nil | ||
} | ||
|
||
ticker := time.NewTicker(10 * time.Second) | ||
defer ticker.Stop() | ||
|
||
timeout := 5 * time.Minute | ||
timer := time.NewTimer(timeout) | ||
defer timer.Stop() | ||
|
||
params := &ecs.ListContainerInstancesInput{ | ||
Cluster: aws.String(c.name), | ||
Filter: aws.String(fmt.Sprintf("registeredAt >= %s", registeredAt.UTC().Format(time.RFC3339))), | ||
} | ||
for { | ||
foundCount := 0 | ||
paginator := ecs.NewListContainerInstancesPaginator(c.ecsSvc, params) | ||
for paginator.HasMorePages() { | ||
page, err := paginator.NextPage(ctx) | ||
if err != nil { | ||
return xerrors.Errorf("failed to list container instances: %w", err) | ||
} | ||
foundCount += len(page.ContainerInstanceArns) | ||
} | ||
if foundCount == count { | ||
return nil | ||
} | ||
|
||
select { | ||
case <-ticker.C: | ||
continue | ||
case <-timer.C: | ||
return xerrors.Errorf("%d container instances expect to be registered but only %d instances were registered within %v", count, foundCount, timeout) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package capacity | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/abicky/ecsmec/internal/testing/capacitymock" | ||
"github.com/aws/aws-sdk-go-v2/service/ecs" | ||
"go.uber.org/mock/gomock" | ||
) | ||
|
||
func TestCluster_WaitUntilContainerInstancesRegistered(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
ctx := context.Background() | ||
|
||
ecsMock := capacitymock.NewMockECSAPI(ctrl) | ||
|
||
ecsMock.EXPECT().ListContainerInstances(ctx, gomock.Any(), gomock.Any()). | ||
DoAndReturn(func(_ context.Context, params *ecs.ListContainerInstancesInput, _ ...func(options *ecs.Options)) (*ecs.ListContainerInstancesOutput, error) { | ||
return &ecs.ListContainerInstancesOutput{ | ||
ContainerInstanceArns: []string{ | ||
fmt.Sprintf("arn:aws:ecs:ap-northeast-1:1234:container-instance/test/xxxxxxxxxx"), | ||
}, | ||
}, nil | ||
}) | ||
|
||
cluster := NewCluster("cluster", ecsMock) | ||
now := time.Now() | ||
if err := cluster.WaitUntilContainerInstancesRegistered(ctx, 1, &now); err != nil { | ||
t.Errorf("err = %#v; want nil", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
package capacitymock | ||
|
||
//go:generate mockgen -package capacitymock -destination mocks.go github.com/abicky/ecsmec/internal/capacity AutoScalingAPI,Drainer,EC2API,ECSAPI,Poller,SQSAPI | ||
//go:generate mockgen -package capacitymock -destination mocks.go github.com/abicky/ecsmec/internal/capacity AutoScalingAPI,Drainer,EC2API,ECSAPI,Poller,SQSAPI,Cluster |