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

collect ECS tasks whose desired status is STOPPED. #46

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading