Skip to content

Commit

Permalink
Add two new rules to validate fargate tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
kddejong committed Jul 4, 2024
1 parent daf73f2 commit 292a25c
Show file tree
Hide file tree
Showing 6 changed files with 365 additions and 116 deletions.
Original file line number Diff line number Diff line change
@@ -1,114 +1,149 @@
{
"if": {
"properties": {
"RequiresCompatibilities": {
"type": "array",
"contains": {
"type": "string",
"enum": ["FARGATE"]
}
},
"Cpu": {
"type": ["string", "integer"]
},
"Memory": {
"type": ["string", "integer"]
}
},
"required": ["RequiresCompatibilities", "Cpu", "Memory"]
"if": {
"properties": {
"Cpu": {
"type": [
"string",
"integer"
]
},
"Memory": {
"type": [
"string",
"integer"
]
},
"RequiresCompatibilities": {
"contains": {
"enum": [
"FARGATE"
],
"type": "string"
},
"then": {
"anyOf": [
{
"properties": {
"Cpu": {
"enum": ["256", 256]
},
"Memory": {
"enum": [
"512",
"1024",
"2048",
512,
1024,
2048
]
}
}
},
{
"properties": {
"Cpu": {
"enum": ["512", 512]
},
"Memory": {
"minimum": 1024,
"maximum": 4096,
"multipleOf": 1024
}
}
},
{
"properties": {
"Cpu": {
"enum": ["1024", 1024]
},
"Memory": {
"minimum": 2048,
"maximum": 8192,
"multipleOf": 1024
}
}
},
{
"properties": {
"Cpu": {
"enum": ["2048", 2048]
},
"Memory": {
"minimum": 4096,
"maximum": 16384,
"multipleOf": 1024
}
}
},
{
"properties": {
"Cpu": {
"enum": ["4096", 4096]
},
"Memory": {
"minimum": 8192,
"maximum": 30720,
"multipleOf": 1024

}
}
},
{
"properties": {
"Cpu": {
"enum": ["8192", 8192]
},
"Memory": {
"minimum": 16384,
"maximum": 61440,
"multipleOf": 4096
}
}
},
{
"properties": {
"Cpu": {
"enum": ["16384", 16384]
},
"Memory": {
"minimum": 32768,
"maximum": 122880,
"multipleOf": 8192
}
}
}
]
"type": "array"
}
},
"required": [
"RequiresCompatibilities",
"Cpu",
"Memory"
]
},
"then": {
"anyOf": [
{
"properties": {
"Cpu": {
"enum": [
256
]
},
"Memory": {
"enum": [
512,
1024,
2048
]
}
}
},
{
"properties": {
"Cpu": {
"enum": [
512
]
},
"Memory": {
"maximum": 4096,
"minimum": 1024,
"multipleOf": 1024
}
}
},
{
"properties": {
"Cpu": {
"enum": [
1024
]
},
"Memory": {
"maximum": 8192,
"minimum": 2048,
"multipleOf": 1024
}
}
},
{
"properties": {
"Cpu": {
"enum": [
2048
]
},
"Memory": {
"maximum": 16384,
"minimum": 4096,
"multipleOf": 1024
}
}
},
{
"properties": {
"Cpu": {
"enum": [
4096
]
},
"Memory": {
"maximum": 30720,
"minimum": 8192,
"multipleOf": 1024
}
}
},
{
"properties": {
"Cpu": {
"enum": [
8192
]
},
"Memory": {
"maximum": 61440,
"minimum": 16384,
"multipleOf": 4096
}
}
},
{
"properties": {
"Cpu": {
"enum": [
16384
]
},
"Memory": {
"maximum": 122880,
"minimum": 32768,
"multipleOf": 8192
}
}
}
],
"properties": {
"Cpu": {
"enum": [
256,
512,
1024,
2048,
4096,
8192,
16384
]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"if": {
"properties": {
"RequiresCompatibilities": {
"contains": {
"enum": [
"FARGATE"
],
"type": "string"
},
"type": "array"
}
},
"required": [
"RequiresCompatibilities"
]
},
"then": {
"not": {
"required": [
"PlacementConstraints"
]
},
"properties": {
"Cpu": {
"enum": [
256,
512,
1024,
2048,
4096,
8192,
16384
]
}
},
"required": [
"Cpu",
"Memory"
]
}
}
2 changes: 1 addition & 1 deletion src/cfnlint/rules/resources/ecs/FargateCpuMemory.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
class FargateCpuMemory(CfnLintJsonSchema):
id = "E3047"
shortdesc = (
"Validate ECS Fargate tasks have the right " "combination of CPU and memory"
"Validate ECS Fargate tasks have the right combination of CPU and memory"
)
description = (
"When using a ECS Fargate task there is a specfic combination "
Expand Down
46 changes: 46 additions & 0 deletions src/cfnlint/rules/resources/ecs/TaskFargateProperties.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
"""

from __future__ import annotations

from collections import deque
from typing import Any

import cfnlint.data.schemas.extensions.aws_ecs_taskdefinition
from cfnlint.jsonschema import ValidationResult
from cfnlint.jsonschema.protocols import Validator
from cfnlint.rules.jsonschema.CfnLintJsonSchema import CfnLintJsonSchema, SchemaDetails


class TaskFargateProperties(CfnLintJsonSchema):
id = "E3048"
shortdesc = "Validate ECS Fargate tasks have required properties and values"
description = (
"When using a ECS Fargate task there is a specfic combination "
"of required properties and values"
)
source_url = "https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ecs-taskdefinition.html#cfn-ecs-taskdefinition-memory"
tags = ["properties", "ecs", "service", "container", "fargate"]

def __init__(self) -> None:
super().__init__(
keywords=["Resources/AWS::ECS::TaskDefinition/Properties"],
schema_details=SchemaDetails(
module=cfnlint.data.schemas.extensions.aws_ecs_taskdefinition,
filename="fargate_properties.json",
),
all_matches=True,
)

def validate(
self, validator: Validator, keywords: Any, instance: Any, schema: dict[str, Any]
) -> ValidationResult:
for err in super().validate(validator, keywords, instance, schema):
if err.validator == "not":
err.message = "'PlacementConstraints' isn't supported for Fargate tasks"
err.path = deque(["PlacementConstraints"])
yield err
continue
yield err
Loading

0 comments on commit 292a25c

Please sign in to comment.