Skip to content

Commit 83b0cdb

Browse files
authored
Add two new rules to validate fargate tasks (#3464)
* Add rule E3047 to validate ECS Fargate cpu/memory * Add two new rules to validate fargate tasks
1 parent 4e28934 commit 83b0cdb

File tree

6 files changed

+521
-0
lines changed

6 files changed

+521
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
{
2+
"if": {
3+
"properties": {
4+
"Cpu": {
5+
"type": [
6+
"string",
7+
"integer"
8+
]
9+
},
10+
"Memory": {
11+
"type": [
12+
"string",
13+
"integer"
14+
]
15+
},
16+
"RequiresCompatibilities": {
17+
"contains": {
18+
"enum": [
19+
"FARGATE"
20+
],
21+
"type": "string"
22+
},
23+
"type": "array"
24+
}
25+
},
26+
"required": [
27+
"RequiresCompatibilities",
28+
"Cpu",
29+
"Memory"
30+
]
31+
},
32+
"then": {
33+
"anyOf": [
34+
{
35+
"properties": {
36+
"Cpu": {
37+
"enum": [
38+
"256"
39+
]
40+
},
41+
"Memory": {
42+
"enum": [
43+
"512",
44+
"1024",
45+
"2048"
46+
]
47+
}
48+
}
49+
},
50+
{
51+
"properties": {
52+
"Cpu": {
53+
"enum": [
54+
"512"
55+
]
56+
},
57+
"Memory": {
58+
"maximum": 4096,
59+
"minimum": 1024,
60+
"multipleOf": 1024
61+
}
62+
}
63+
},
64+
{
65+
"properties": {
66+
"Cpu": {
67+
"enum": [
68+
"1024"
69+
]
70+
},
71+
"Memory": {
72+
"maximum": 8192,
73+
"minimum": 2048,
74+
"multipleOf": 1024
75+
}
76+
}
77+
},
78+
{
79+
"properties": {
80+
"Cpu": {
81+
"enum": [
82+
"2048"
83+
]
84+
},
85+
"Memory": {
86+
"maximum": 16384,
87+
"minimum": 4096,
88+
"multipleOf": 1024
89+
}
90+
}
91+
},
92+
{
93+
"properties": {
94+
"Cpu": {
95+
"enum": [
96+
"4096"
97+
]
98+
},
99+
"Memory": {
100+
"maximum": 30720,
101+
"minimum": 8192,
102+
"multipleOf": 1024
103+
}
104+
}
105+
},
106+
{
107+
"properties": {
108+
"Cpu": {
109+
"enum": [
110+
"8192"
111+
]
112+
},
113+
"Memory": {
114+
"maximum": 61440,
115+
"minimum": 16384,
116+
"multipleOf": 4096
117+
}
118+
}
119+
},
120+
{
121+
"properties": {
122+
"Cpu": {
123+
"enum": [
124+
"16384"
125+
]
126+
},
127+
"Memory": {
128+
"maximum": 122880,
129+
"minimum": 32768,
130+
"multipleOf": 8192
131+
}
132+
}
133+
}
134+
]
135+
}
136+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"if": {
3+
"properties": {
4+
"RequiresCompatibilities": {
5+
"contains": {
6+
"enum": [
7+
"FARGATE"
8+
],
9+
"type": "string"
10+
},
11+
"type": "array"
12+
}
13+
},
14+
"required": [
15+
"RequiresCompatibilities"
16+
]
17+
},
18+
"then": {
19+
"if": {
20+
"properties": {
21+
"Cpu": {
22+
"type": [
23+
"string",
24+
"integer"
25+
]
26+
}
27+
},
28+
"required": [
29+
"Cpu"
30+
]
31+
},
32+
"not": {
33+
"required": [
34+
"PlacementConstraints"
35+
]
36+
},
37+
"required": [
38+
"Cpu",
39+
"Memory"
40+
],
41+
"then": {
42+
"properties": {
43+
"Cpu": {
44+
"enum": [
45+
"256",
46+
"512",
47+
"1024",
48+
"2048",
49+
"4096",
50+
"8192",
51+
"16384"
52+
]
53+
}
54+
}
55+
}
56+
}
57+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
SPDX-License-Identifier: MIT-0
4+
"""
5+
6+
from __future__ import annotations
7+
8+
from typing import Any
9+
10+
import cfnlint.data.schemas.extensions.aws_ecs_taskdefinition
11+
from cfnlint.jsonschema import ValidationError
12+
from cfnlint.rules.jsonschema.CfnLintJsonSchema import CfnLintJsonSchema, SchemaDetails
13+
14+
15+
class FargateCpuMemory(CfnLintJsonSchema):
16+
id = "E3047"
17+
shortdesc = (
18+
"Validate ECS Fargate tasks have the right combination of CPU and memory"
19+
)
20+
description = (
21+
"When using a ECS Fargate task there is a specfic combination "
22+
"of memory and cpu that can be used"
23+
)
24+
source_url = "https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ecs-taskdefinition.html#cfn-ecs-taskdefinition-memory"
25+
tags = ["properties", "ecs", "service", "container", "fargate"]
26+
27+
def __init__(self) -> None:
28+
super().__init__(
29+
keywords=["Resources/AWS::ECS::TaskDefinition/Properties"],
30+
schema_details=SchemaDetails(
31+
module=cfnlint.data.schemas.extensions.aws_ecs_taskdefinition,
32+
filename="fargate_cpu_memory.json",
33+
),
34+
)
35+
36+
def message(self, instance: Any, err: ValidationError) -> str:
37+
return (
38+
f"Cpu {instance.get('Cpu')!r} is not "
39+
"compatible with memory "
40+
f"{instance.get('Memory')!r}"
41+
)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
SPDX-License-Identifier: MIT-0
4+
"""
5+
6+
from __future__ import annotations
7+
8+
from collections import deque
9+
from typing import Any
10+
11+
import cfnlint.data.schemas.extensions.aws_ecs_taskdefinition
12+
from cfnlint.jsonschema import ValidationResult
13+
from cfnlint.jsonschema.protocols import Validator
14+
from cfnlint.rules.jsonschema.CfnLintJsonSchema import CfnLintJsonSchema, SchemaDetails
15+
16+
17+
class TaskFargateProperties(CfnLintJsonSchema):
18+
id = "E3048"
19+
shortdesc = "Validate ECS Fargate tasks have required properties and values"
20+
description = (
21+
"When using a ECS Fargate task there is a specfic combination "
22+
"of required properties and values"
23+
)
24+
source_url = "https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ecs-taskdefinition.html#cfn-ecs-taskdefinition-memory"
25+
tags = ["properties", "ecs", "service", "container", "fargate"]
26+
27+
def __init__(self) -> None:
28+
super().__init__(
29+
keywords=["Resources/AWS::ECS::TaskDefinition/Properties"],
30+
schema_details=SchemaDetails(
31+
module=cfnlint.data.schemas.extensions.aws_ecs_taskdefinition,
32+
filename="fargate_properties.json",
33+
),
34+
all_matches=True,
35+
)
36+
37+
def validate(
38+
self, validator: Validator, keywords: Any, instance: Any, schema: dict[str, Any]
39+
) -> ValidationResult:
40+
for err in super().validate(validator, keywords, instance, schema):
41+
if err.validator == "not":
42+
err.message = "'PlacementConstraints' isn't supported for Fargate tasks"
43+
err.path = deque(["PlacementConstraints"])
44+
yield err
45+
continue
46+
yield err

0 commit comments

Comments
 (0)