Skip to content

Commit

Permalink
Merge pull request #46 from fujiwara/collect-stopped-tasks
Browse files Browse the repository at this point in the history
collect ECS tasks whose desired status is STOPPED.
  • Loading branch information
fujiwara authored Sep 13, 2024
2 parents 6080757 + 02ce8ce commit b25f3a4
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/ecs"
ecsTypes "github.com/aws/aws-sdk-go-v2/service/ecs/types"
"github.com/aws/aws-sdk-go-v2/service/lambda"
"github.com/samber/lo"
)

type Scanner struct {
Expand Down Expand Up @@ -197,18 +198,24 @@ func (s *Scanner) availableResourcesInCluster(ctx context.Context, clusterArn st
tdArns := make(set)

log.Printf("[debug] Checking tasks in %s", clusterArn)
tp := ecs.NewListTasksPaginator(s.ecs, &ecs.ListTasksInput{Cluster: &clusterArn})
for tp.HasMorePages() {
to, err := tp.NextPage(ctx)
if err != nil {
return nil, err
}
if len(to.TaskArns) == 0 {
continue
taskArns := make([]string, 0)
for _, status := range []ecsTypes.DesiredStatus{ecsTypes.DesiredStatusRunning, ecsTypes.DesiredStatusStopped} {
tp := ecs.NewListTasksPaginator(s.ecs, &ecs.ListTasksInput{
Cluster: &clusterArn,
DesiredStatus: status,
})
for tp.HasMorePages() {
to, err := tp.NextPage(ctx)
if err != nil {
return nil, err
}
taskArns = append(taskArns, to.TaskArns...)
}
}
for _, tasks := range lo.Chunk(taskArns, 100) { // 100 is the max for describeTasks API
tasks, err := s.ecs.DescribeTasks(ctx, &ecs.DescribeTasksInput{
Cluster: &clusterArn,
Tasks: to.TaskArns,
Tasks: tasks,
})
if err != nil {
return nil, err
Expand All @@ -227,6 +234,9 @@ func (s *Scanner) availableResourcesInCluster(ctx context.Context, clusterArn st
log.Printf("[info] taskdef %s is used by %s", td.String(), ts.Resource)
}
for _, c := range task.Containers {
if c.Image == nil {
continue
}
u := ImageURI(aws.ToString(c.Image))
if !u.IsECRImage() {
continue
Expand All @@ -236,7 +246,7 @@ func (s *Scanner) availableResourcesInCluster(ctx context.Context, clusterArn st
if s.Images.Add(u, tdArn) {
log.Printf("[info] image %s is used by %s container on %s", u.String(), *c.Name, ts.Resource)
}
} else {
} else if c.ImageDigest != nil {
base := u.Base()
digest := aws.ToString(c.ImageDigest)
u := ImageURI(base + "@" + digest)
Expand Down

0 comments on commit b25f3a4

Please sign in to comment.