Skip to content
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): support restart policy for container #31228

Merged
merged 24 commits into from
Sep 24, 2024
Merged

Conversation

go-to-k
Copy link
Contributor

@go-to-k go-to-k commented Aug 27, 2024

Issue # (if applicable)

Closes #31127 #31425.

Reason for this change

A restart policy can be specified in CloudFormation, but not in L2.

https://docs.aws.amazon.com/AmazonECS/latest/developerguide/container-restart-policy.html

Description of changes

Add enableRestartPolicy and some properties to the container definition.

Description of how you validated changes

unit tests and integ tests.

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@github-actions github-actions bot added distinguished-contributor [Pilot] contributed 50+ PRs to the CDK effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. p2 labels Aug 27, 2024
@aws-cdk-automation aws-cdk-automation requested a review from a team August 27, 2024 06:22
@go-to-k go-to-k changed the title WIP: feat(ecs): support restart policy for container definition WIP: feat(ecs): support restart policy in container definition Aug 27, 2024
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a 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.

@go-to-k go-to-k changed the title WIP: feat(ecs): support restart policy in container definition feat(ecs): support restart policy in container definition Aug 27, 2024
@go-to-k go-to-k changed the title feat(ecs): support restart policy in container definition feat(ecs): support restart policy for container definition Aug 27, 2024
@go-to-k go-to-k changed the title feat(ecs): support restart policy for container definition feat(ecs): support restart policy for container Aug 27, 2024
sample codes
@go-to-k go-to-k marked this pull request as ready for review September 10, 2024 05:46
@aws-cdk-automation aws-cdk-automation dismissed their stale review September 10, 2024 05:48

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

@go-to-k go-to-k marked this pull request as draft September 10, 2024 06:05
@go-to-k go-to-k marked this pull request as ready for review September 10, 2024 07:46
@aws-cdk-automation aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Sep 10, 2024
@go-to-k go-to-k marked this pull request as draft September 11, 2024 05:37
@go-to-k go-to-k marked this pull request as ready for review September 11, 2024 07:00
Comment on lines +388 to +396
/**
* Enable a restart policy for a container.
*
* When you set up a restart policy, Amazon ECS can restart the container without needing to replace the task.
*
* @default - false unless `restartIgnoredExitCodes` or `restartAttemptPeriod` is set.
* @see https://docs.aws.amazon.com/AmazonECS/latest/developerguide/container-restart-policy.html
*/
readonly enableRestartPolicy?: boolean;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having an interface called RestartPolicy, the flag and other properties are flatly defined.

There are two reasons for this.

The first is to comply with the CDK design guidelines.

Secondly, it was uncomfortable that an empty RestartPolicy had to be specified to enable the restart policy with the default configuration.

taskDefinition.addContainer('container', {
  image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"),
  restartPolicy: {},
});

@@ -590,6 +626,8 @@ export class ContainerDefinition extends Construct {
if (props.ulimits) {
this.addUlimits(...props.ulimits);
}

this.validateRestartPolicy(props.enableRestartPolicy, props.restartIgnoredExitCodes, props.restartAttemptPeriod);
Copy link
Contributor Author

@go-to-k go-to-k Sep 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This validation function is called in the constructor, not in the renderRestartPolicy function (lines 1550).

The render function is called in the renderContainerDefinition function, which is called by a Lazy method in the TaskDefinition. Therefore, the rendering does not run in the construction phase (even if the construct is created with new ContainerDefinition(), it still does not run).

However, it is better to run the validation earlier, so the validation is not done in the render function, but in the constructor, which allows it to run earlier. This ensures that the validation is performed when the construct is created with new ContainerDefinition().

Copy link
Contributor

@GavinZZ GavinZZ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you for the contribution!

Copy link
Contributor

mergify bot commented Sep 24, 2024

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).

@aws-cdk-automation aws-cdk-automation removed the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Sep 24, 2024
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 6692548
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify mergify bot merged commit a22e8cc into aws:main Sep 24, 2024
12 checks passed
Copy link
Contributor

mergify bot commented Sep 24, 2024

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).

Copy link

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 24, 2024
@go-to-k go-to-k deleted the restart-policy branch September 25, 2024 04:44
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
distinguished-contributor [Pilot] contributed 50+ PRs to the CDK effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. p2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ecs: support container definition's restart policy
3 participants