-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(ecs): add validation checks to memory cpu combinations of FARGATE compatible task definitions #30166
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pull request linter has failed. See the aws-cdk-automation comment below for failure reasons. If you believe this pull request should receive an exemption, please comment and provide a justification.
A comment requesting an exemption should contain the text Exemption Request
. Additionally, if clarification is needed add Clarification Request
to a comment.
✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks 👍 Left some comments for adjustments
// Validate CPU and memory combinations if fargate compatible | ||
if (isFargateCompatible(props.compatibility)) { | ||
// Check the combination as per doc https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-cpu-memory-error.html | ||
this.validateFargateTaskDefinitionMemoryCpu(props.cpu!, props.memoryMiB!); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we take the opportunity to remove the !
s? It's a bit annoying to see them.
if (this.isFargateCompatible) {
if (!props.cpu || !props.memoryMiB) {
throw new Error(`Fargate-compatible tasks require both CPU (${props.cpu}) and memory (${props.memoryMiB}) specifications`);
}
// validate the cpu and memory size for the Windows operation system family.
if (props.runtimePlatform?.operatingSystemFamily?.isWindows()) {
this.checkFargateWindowsBasedTasksSize(props.cpu, props.memoryMiB, props.runtimePlatform);
}
// Check the combination as per doc https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-cpu-memory-error.html
this.validateFargateTaskDefinitionMemoryCpu(props.cpu, props.memoryMiB);
}
const validCpuMemoryCombinations = [ | ||
{ cpu: 256, memory: [512, 1024, 2048] }, | ||
{ cpu: 512, memory: this.range(1024, 4096, 1024) }, | ||
{ cpu: 1024, memory: this.range(2048, 8192, 1024) }, | ||
{ cpu: 2048, memory: this.range(4096, 16384, 1024) }, | ||
{ cpu: 4096, memory: this.range(8192, 30720, 1024) }, | ||
{ cpu: 8192, memory: this.range(16384, 61440, 4096) }, | ||
{ cpu: 16384, memory: this.range(32768, 122880, 8192) }, | ||
]; | ||
|
||
const isValidCombination = validCpuMemoryCombinations.some((combo) => { | ||
return combo.cpu === Number(cpu) && combo.memory.includes(Number(memory)); | ||
}); | ||
|
||
if (!isValidCombination) { | ||
throw new Error('Invalid CPU and memory combinations for FARGATE compatible task definition - https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-cpu-memory-error.html'); | ||
} | ||
} | ||
|
||
private range(start: number, end: number, step: number): number[] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const validCpuMemoryCombinations = [ | |
{ cpu: 256, memory: [512, 1024, 2048] }, | |
{ cpu: 512, memory: this.range(1024, 4096, 1024) }, | |
{ cpu: 1024, memory: this.range(2048, 8192, 1024) }, | |
{ cpu: 2048, memory: this.range(4096, 16384, 1024) }, | |
{ cpu: 4096, memory: this.range(8192, 30720, 1024) }, | |
{ cpu: 8192, memory: this.range(16384, 61440, 4096) }, | |
{ cpu: 16384, memory: this.range(32768, 122880, 8192) }, | |
]; | |
const isValidCombination = validCpuMemoryCombinations.some((combo) => { | |
return combo.cpu === Number(cpu) && combo.memory.includes(Number(memory)); | |
}); | |
if (!isValidCombination) { | |
throw new Error('Invalid CPU and memory combinations for FARGATE compatible task definition - https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-cpu-memory-error.html'); | |
} | |
} | |
private range(start: number, end: number, step: number): number[] { | |
const validCpuMemoryCombinations = [ | |
{ cpu: '256', memory: ['512', '1024', '2048'] }, | |
{ cpu: '512', memory: this.range(1024, 4096, 1024) }, | |
{ cpu: '1024', memory: this.range(2048, 8192, 1024) }, | |
{ cpu: '2048', memory: this.range(4096, 16384, 1024) }, | |
{ cpu: '4096', memory: this.range(8192, 30720, 1024) }, | |
{ cpu: '8192', memory: this.range(16384, 61440, 4096) }, | |
{ cpu: '16384', memory: this.range(32768, 122880, 8192) }, | |
]; | |
const isValidCombination = validCpuMemoryCombinations.some((combo) => { | |
return combo.cpu === cpu && combo.memory.includes(memory); | |
}); | |
if (!isValidCombination) { | |
throw new Error('Invalid CPU and memory combinations for FARGATE compatible task definition - https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-cpu-memory-error.html'); | |
} | |
} | |
private range(start: number, end: number, step: number): string[] { |
What about avoiding Number -> String
casting?
@@ -21,28 +21,24 @@ describe('fargate task definition', () => { | |||
|
|||
}); | |||
|
|||
test('support lazy cpu and memory values', () => { | |||
test('does not support lazy cpu and memory values', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should be able to support this by adding validation on the node
(like this):
this.node.addValidation({ validate: () => this. validateFargateTaskDefinitionMemoryCpu(...);
} | ||
|
||
// Check the combination as per doc https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-cpu-memory-error.html | ||
this.validateFargateTaskDefinitionMemoryCpu(props.cpu, props.memoryMiB); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this.validateFargateTaskDefinitionMemoryCpu(props.cpu, props.memoryMiB); | |
this.node.addValidation({ | |
validate: () => this.validateFargateTaskDefinitionMemoryCpu(props.cpu, props.memoryMiB) | |
}); |
Please note https://github.com/aws/aws-cdk/pull/30166/files#r1641289793
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have made all the suggested changes @lpizzinidev
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please take a look once
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! Left a few comments and nits to be addressed.
packages/aws-cdk-lib/aws-ecs/test/fargate/fargate-task-definition.test.ts
Outdated
Show resolved
Hide resolved
packages/aws-cdk-lib/aws-ecs/test/fargate/fargate-task-definition.test.ts
Outdated
Show resolved
Hide resolved
packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.ts
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the changes, and fixing a few other typos around the repo! Just one minor concern about some of the new logic-shuffling.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks for your contribution!
Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork). |
➡️ PR build request submitted to A maintainer must now check the pipeline and add the |
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork). |
Comments on closed issues and PRs are hard for our team to see. |
Issue # (if applicable)
Closes #22216
Reason for this change
Adds a validation that would point out invalid CPU - Memory limits for a FARGATE compatible task definition. This is based on the document https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-cpu-memory-error.html
Description of changes
Added a
validateFargateTaskDefinitionMemoryCpu
method in the base task definition and run this validation when a task definition has FARGATE in requiresCompatibilities.This is in line with the ECS requirement that even if a task definition has EC2 compatibility (along with FARGATE), FARGATE validations apply
Description of how you validated changes
Validated the changes by providing incorrect as well as correct values, fixed all the invalid combinations that are present in the unit tests
Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license