Skip to content

Commit

Permalink
fix: fetch tasks in batches of 100 task arns
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe authored and moshloop committed Jul 9, 2024
1 parent 8f2a723 commit e58bde0
Showing 1 changed file with 38 additions and 16 deletions.
54 changes: 38 additions & 16 deletions scrapers/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,37 +363,59 @@ func (aws Scraper) ecsServices(ctx *AWSContext, config v1.AWS, client *ecs.Clien
}
}

func (aws Scraper) listAllECSTasks(ctx *AWSContext, client *ecs.Client, clusterArn string, results *v1.ScrapeResults) ([]string, error) {
var taskArns []string
for _, desiredStatus := range ecsTypes.DesiredStatus("").Values() {
ctx.Logger.V(2).Infof("scraping ECS tasks for cluster %s", clusterArn)

for {
tasks, err := client.ListTasks(ctx, &ecs.ListTasksInput{
Cluster: &clusterArn,
DesiredStatus: desiredStatus,
})
if err != nil {
results.Errorf(err, "failed to list ECS tasks in cluster %s", clusterArn)
continue
}

taskArns = append(taskArns, tasks.TaskArns...)
if tasks.NextToken == nil {
break
}
}
}

return taskArns, nil
}

func (aws Scraper) ecsTasks(ctx *AWSContext, config v1.AWS, client *ecs.Client, clusterArn, clusterName string, results *v1.ScrapeResults) {
if !config.Includes("ECSTask") {
return
}

var y ecsTypes.DesiredStatus
for _, desiredStatus := range y.Values() {
ctx.Logger.V(2).Infof("scraping ECS tasks for cluster %s", clusterArn)

tasks, err := client.ListTasks(ctx, &ecs.ListTasksInput{
Cluster: &clusterArn,
DesiredStatus: desiredStatus,
})
if err != nil {
results.Errorf(err, "failed to list ECS tasks in cluster %s", clusterArn)
continue
}
allTaskArns, err := aws.listAllECSTasks(ctx, client, clusterArn, results)
if err != nil {
results.Errorf(err, "failed to list ECS tasks in cluster %s", clusterArn)
return
}

if len(tasks.TaskArns) == 0 {
continue
}
for _, taskArns := range lo.Chunk(allTaskArns, 100) {
ctx.Logger.V(2).Infof("scraping ECS tasks for cluster %s", clusterArn)

describeTasksOutput, err := client.DescribeTasks(ctx, &ecs.DescribeTasksInput{
Cluster: &clusterArn,
Tasks: tasks.TaskArns,
Tasks: taskArns,
})
if err != nil {
results.Errorf(err, "failed to describe ECS tasks in cluster %s", clusterArn)
continue
}

if len(describeTasksOutput.Failures) > 0 {
results.Errorf(fmt.Errorf("%v", describeTasksOutput.Failures), "failed to describe ECS tasks in cluster %s", clusterArn)
continue
}

for _, task := range describeTasksOutput.Tasks {
taskID := strings.Split(*task.TaskArn, "/")[len(strings.Split(*task.TaskArn, "/"))-1]

Expand Down

0 comments on commit e58bde0

Please sign in to comment.