Skip to content

Commit 11b4274

Browse files
authored
chore: ignore INACTIVE cluster in DefaultCluster (#2045)
When we delete ECS cluster, AWS return it as INACTIVE cluster. ``` INACTIVE The cluster has been deleted. Clusters with an INACTIVE status may remain discoverable in your account for a period of time. However, this behavior is subject to change in the future, so you should not rely on INACTIVE clusters persisting. ``` https://docs.aws.amazon.com/AmazonECS/latest/developerguide/clusters.html We can't run task on INACTIVE cluster so we should ignore it.
1 parent cb514cb commit 11b4274

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

internal/pkg/aws/ecs/ecs.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"github.com/aws/aws-sdk-go/service/ecs"
1717
)
1818

19+
const clusterStatusActive = "ACTIVE"
20+
1921
type api interface {
2022
DescribeClusters(input *ecs.DescribeClustersInput) (*ecs.DescribeClustersOutput, error)
2123
DescribeServices(input *ecs.DescribeServicesInput) (*ecs.DescribeServicesOutput, error)
@@ -195,6 +197,10 @@ func (e *ECS) DefaultCluster() (string, error) {
195197

196198
// NOTE: right now at most 1 default cluster is possible, so cluster[0] must be the default cluster
197199
cluster := resp.Clusters[0]
200+
if aws.StringValue(cluster.Status) != clusterStatusActive {
201+
return "", ErrNoDefaultCluster
202+
}
203+
198204
return aws.StringValue(cluster.ClusterArn), nil
199205
}
200206

internal/pkg/aws/ecs/ecs_test.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,17 +422,35 @@ func TestECS_DefaultCluster(t *testing.T) {
422422
{
423423
ClusterArn: aws.String("arn:aws:ecs:us-east-1:0123456:cluster/cluster1"),
424424
ClusterName: aws.String("cluster1"),
425+
Status: aws.String(clusterStatusActive),
425426
},
426427
{
427428
ClusterArn: aws.String("arn:aws:ecs:us-east-1:0123456:cluster/cluster2"),
428429
ClusterName: aws.String("cluster2"),
430+
Status: aws.String(clusterStatusActive),
429431
},
430432
},
431433
}, nil)
432434
},
433435

434436
wantedClusters: "arn:aws:ecs:us-east-1:0123456:cluster/cluster1",
435437
},
438+
"ignore inactive cluster": {
439+
mockECSClient: func(m *mocks.Mockapi) {
440+
m.EXPECT().
441+
DescribeClusters(&ecs.DescribeClustersInput{}).
442+
Return(&ecs.DescribeClustersOutput{
443+
Clusters: []*ecs.Cluster{
444+
{
445+
ClusterArn: aws.String("arn:aws:ecs:us-east-1:0123456:cluster/cluster1"),
446+
ClusterName: aws.String("cluster1"),
447+
Status: aws.String("INACTIVE"),
448+
},
449+
},
450+
}, nil)
451+
},
452+
wantedError: fmt.Errorf("default cluster does not exist"),
453+
},
436454
"failed to get default clusters": {
437455
mockECSClient: func(m *mocks.Mockapi) {
438456
m.EXPECT().
@@ -492,7 +510,10 @@ func TestECS_HasDefaultCluster(t *testing.T) {
492510
m.EXPECT().DescribeClusters(&ecs.DescribeClustersInput{}).
493511
Return(&ecs.DescribeClustersOutput{
494512
Clusters: []*ecs.Cluster{
495-
{ClusterArn: aws.String("cluster")},
513+
{
514+
ClusterArn: aws.String("cluster"),
515+
Status: aws.String(clusterStatusActive),
516+
},
496517
},
497518
}, nil)
498519
},

0 commit comments

Comments
 (0)