-
Notifications
You must be signed in to change notification settings - Fork 597
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate scheduling strategy for Fargate services (#2559)
* Validate scheduling strategy for Fargate services * Update test/fixtures/templates/good/resources/ecs/test_fargate_scheduling_strategy.yaml Co-authored-by: Pat Myron <[email protected]> * Fix Fargate strategy rule to handle non string properties Change: Intrinsic functions can be used for resource properties and previously this rule checker doesn't handle such scenario. --------- Co-authored-by: Pat Myron <[email protected]>
- Loading branch information
Showing
6 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/cfnlint/rules/resources/ecs/FargateDeploymentSchedulingStrategy.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
""" | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
SPDX-License-Identifier: MIT-0 | ||
""" | ||
from cfnlint.rules import CloudFormationLintRule, RuleMatch | ||
|
||
|
||
class FargateDeploymentSchedulingStrategy(CloudFormationLintRule): | ||
id = "E3044" | ||
shortdesc = "Check Fargate service scheduling strategy" | ||
description = "Check that Fargate service scheduling strategy is REPLICA" | ||
source_url = "https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ecs-service.html#cfn-ecs-service-schedulingstrategy" | ||
tags = ["properties", "ecs", "service", "container", "fargate"] | ||
|
||
def match(self, cfn): | ||
matches = [] | ||
ecs_services = cfn.get_resource_properties(["AWS::ECS::Service"]) | ||
|
||
for ecs_service in ecs_services: | ||
path = ecs_service["Path"] | ||
properties = ecs_service["Value"] | ||
if isinstance(properties, dict): | ||
launch_type = properties.get("LaunchType", None) | ||
if isinstance(launch_type, str) and launch_type == "Fargate": | ||
scheduling_strategy = properties.get("SchedulingStrategy", None) | ||
if ( | ||
isinstance(scheduling_strategy, str) | ||
and scheduling_strategy != "REPLICA" | ||
): | ||
error_message = f"Fargate service only support REPLICA as scheduling strategy at {'/'.join(map(str, path))}" | ||
matches.append(RuleMatch(path, error_message)) | ||
return matches |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
test/fixtures/templates/bad/resources/ecs/test_fargate_scheduling_strategy.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Resources: | ||
Service1: | ||
Type: AWS::ECS::Service | ||
Properties: | ||
LaunchType: Fargate | ||
SchedulingStrategy: DAEMON |
21 changes: 21 additions & 0 deletions
21
test/fixtures/templates/good/resources/ecs/test_fargate_scheduling_strategy.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
Resources: | ||
Service1: | ||
Type: AWS::ECS::Service | ||
Properties: | ||
LaunchType: Fargate | ||
SchedulingStrategy: REPLICA | ||
Service2: | ||
Type: AWS::ECS::Service | ||
Properties: | ||
LaunchType: EC2 | ||
SchedulingStrategy: DAEMON | ||
Service3: | ||
Type: AWS::ECS::Service | ||
Properties: | ||
LaunchType: EXTERNAL | ||
SchedulingStrategy: DAEMON | ||
Service4: | ||
Type: AWS::ECS::Service | ||
Properties: | ||
LaunchType: !Join ["", ["FAR", "GATE"]] | ||
SchedulingStrategy: DAEMON |
31 changes: 31 additions & 0 deletions
31
test/unit/rules/resources/ecs/test_fargate_service_scheduling_strategy.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
""" | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
SPDX-License-Identifier: MIT-0 | ||
""" | ||
from test.unit.rules import BaseRuleTestCase | ||
|
||
from cfnlint.rules.resources.ecs.FargateDeploymentSchedulingStrategy import ( | ||
FargateDeploymentSchedulingStrategy, | ||
) | ||
|
||
|
||
class TestFargateServiceSchedulingStrategy(BaseRuleTestCase): | ||
""" | ||
Test that Fargate service has correct scheduling strategy (REPLICA) | ||
""" | ||
|
||
def setUp(self): | ||
super(TestFargateServiceSchedulingStrategy, self).setUp() | ||
self.collection.register(FargateDeploymentSchedulingStrategy()) | ||
self.success_templates = [ | ||
"test/fixtures/templates/good/resources/ecs/test_fargate_scheduling_strategy.yaml" | ||
] | ||
|
||
def test_file_positive(self): | ||
self.helper_file_positive() | ||
|
||
def test_file_negative(self): | ||
self.helper_file_negative( | ||
"test/fixtures/templates/bad/resources/ecs/test_fargate_scheduling_strategy.yaml", | ||
1, | ||
) |