Skip to content

Commit 72b79c8

Browse files
authored
[aws][fix] Only collect latest task definition (#2055)
1 parent 08926f0 commit 72b79c8

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

plugins/aws/fix_plugin_aws/aws_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ def get_with_retry(
185185
**kwargs: Any,
186186
) -> JsonElement:
187187
try:
188-
# turn off boto retry and rely on our retry handler
189-
return self.call_single(aws_service, action, result_name, max_attempts=0, **kwargs)
188+
# 5 attempts is the default
189+
return self.call_single(aws_service, action, result_name, max_attempts=5, **kwargs)
190190
except ClientError as e:
191191
self.__handle_client_error(e, aws_service, action, expected_errors) # might reraise the exception
192192
return None

plugins/aws/fix_plugin_aws/resource/ecs.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import logging
12
from datetime import datetime
2-
from typing import ClassVar, Dict, Optional, List, Type, Any
3+
from typing import ClassVar, Dict, Optional, List, Type, Any, cast
34

45
from attrs import define, field
56
from fix_plugin_aws.aws_client import AwsClient
@@ -19,6 +20,7 @@
1920
from fixlib.utils import chunks
2021
from fix_plugin_aws.utils import TagsValue, ToDict
2122

23+
log = logging.getLogger("fix.plugins.aws")
2224
service_name = "ecs"
2325

2426

@@ -1075,7 +1077,9 @@ class AwsEcsTaskDefinition(EcsTaggable, AwsResource):
10751077
" Container Service (ECS), providing information such as the Docker image,"
10761078
" CPU, memory, network configuration, and other parameters."
10771079
)
1078-
api_spec: ClassVar[AwsApiSpec] = AwsApiSpec(service_name, "list-task-definitions", "taskDefinitionArns")
1080+
api_spec: ClassVar[AwsApiSpec] = AwsApiSpec(
1081+
service_name, "list-task-definitions", "taskDefinitionArns", {"sort": "desc"}
1082+
)
10791083
reference_kinds: ClassVar[ModelReference] = {
10801084
"predecessors": {"default": ["aws_iam_role"], "delete": ["aws_iam_role"]},
10811085
}
@@ -1141,7 +1145,7 @@ def called_collect_apis(cls) -> List[AwsApiSpec]:
11411145

11421146
@classmethod
11431147
def collect(cls: Type[AwsResource], json: List[Json], builder: GraphBuilder) -> None:
1144-
for task_def_arn in json:
1148+
def collect_task_definition(task_def_arn: str) -> None:
11451149
response = builder.client.get(
11461150
service_name,
11471151
"describe-task-definition",
@@ -1154,7 +1158,17 @@ def collect(cls: Type[AwsResource], json: List[Json], builder: GraphBuilder) ->
11541158
tags = response["tags"]
11551159
task_definition["tags"] = tags
11561160
if task_definition_instance := cls.from_api(task_definition, builder):
1157-
builder.add_node(task_definition_instance, task_def_arn)
1161+
builder.add_node(task_definition_instance)
1162+
1163+
last_task_def_arn = ""
1164+
for arn in cast(List[str], json):
1165+
# Skip task definition with same arn but older version
1166+
no_version = arn.rsplit(":", 1)[0]
1167+
if no_version == last_task_def_arn:
1168+
log.info(f"Skipping task definition {arn} as it is an older version of {last_task_def_arn}")
1169+
continue
1170+
last_task_def_arn = no_version
1171+
builder.submit_work(service_name, collect_task_definition, arn)
11581172

11591173
def connect_in_graph(self, builder: GraphBuilder, source: Json) -> None:
11601174
for role in [self.task_role_arn, self.execution_role_arn]:

0 commit comments

Comments
 (0)