Skip to content

Commit c2e806b

Browse files
SoManyHsrix0rrr
authored andcommitted
fix(aws-ecs): fix default daemon deploymentConfig values (#2210)
Fixes #2209
1 parent 87b1ea0 commit c2e806b

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

packages/@aws-cdk/aws-ecs/lib/base/base-service.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export interface BaseServiceProps {
3838
* service's DesiredCount value, that can run in a service during a
3939
* deployment.
4040
*
41-
* @default 200
41+
* @default 100 if daemon, otherwise 200
4242
*/
4343
readonly maximumPercent?: number;
4444

@@ -47,7 +47,7 @@ export interface BaseServiceProps {
4747
* the Amazon ECS service's DesiredCount value, that must
4848
* continue to run and remain healthy during a deployment.
4949
*
50-
* @default 50
50+
* @default 0 if daemon, otherwise 50
5151
*/
5252
readonly minimumHealthyPercent?: number;
5353

packages/@aws-cdk/aws-ecs/lib/ec2/ec2-service.ts

+10
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ export class Ec2Service extends BaseService implements elb.ILoadBalancerTarget {
7070
throw new Error('Daemon mode launches one task on every instance. Don\'t supply desiredCount.');
7171
}
7272

73+
if (props.daemon && props.maximumPercent !== undefined && props.maximumPercent !== 100) {
74+
throw new Error('Maximum percent must be 100 for daemon mode.');
75+
}
76+
77+
if (props.daemon && props.minimumHealthyPercent !== undefined && props.minimumHealthyPercent !== 0) {
78+
throw new Error('Minimum healthy percent must be 0 for daemon mode.');
79+
}
80+
7381
if (!isEc2Compatible(props.taskDefinition.compatibility)) {
7482
throw new Error('Supplied TaskDefinition is not configured for compatibility with EC2');
7583
}
@@ -78,6 +86,8 @@ export class Ec2Service extends BaseService implements elb.ILoadBalancerTarget {
7886
...props,
7987
// If daemon, desiredCount must be undefined and that's what we want. Otherwise, default to 1.
8088
desiredCount: props.daemon || props.desiredCount !== undefined ? props.desiredCount : 1,
89+
maximumPercent: props.daemon && props.maximumPercent === undefined ? 100 : props.maximumPercent,
90+
minimumHealthyPercent: props.daemon && props.minimumHealthyPercent === undefined ? 0 : props.minimumHealthyPercent ,
8191
},
8292
{
8393
cluster: props.cluster.clusterName,

packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-service.ts

+55-1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,56 @@ export = {
7575
test.done();
7676
},
7777

78+
"errors if daemon and maximumPercent not 100"(test: Test) {
79+
// GIVEN
80+
const stack = new cdk.Stack();
81+
const vpc = new ec2.VpcNetwork(stack, 'MyVpc', {});
82+
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
83+
cluster.addCapacity('DefaultAutoScalingGroup', { instanceType: new ec2.InstanceType('t2.micro') });
84+
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'Ec2TaskDef');
85+
taskDefinition.addContainer('BaseContainer', {
86+
image: ecs.ContainerImage.fromRegistry('test'),
87+
memoryReservationMiB: 10,
88+
});
89+
90+
// THEN
91+
test.throws(() => {
92+
new ecs.Ec2Service(stack, "Ec2Service", {
93+
cluster,
94+
taskDefinition,
95+
daemon: true,
96+
maximumPercent: 300
97+
});
98+
}, /Maximum percent must be 100 for daemon mode./);
99+
100+
test.done();
101+
},
102+
103+
"errors if daemon and minimum not 0"(test: Test) {
104+
// GIVEN
105+
const stack = new cdk.Stack();
106+
const vpc = new ec2.VpcNetwork(stack, 'MyVpc', {});
107+
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
108+
cluster.addCapacity('DefaultAutoScalingGroup', { instanceType: new ec2.InstanceType('t2.micro') });
109+
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'Ec2TaskDef');
110+
taskDefinition.addContainer('BaseContainer', {
111+
image: ecs.ContainerImage.fromRegistry('test'),
112+
memoryReservationMiB: 10,
113+
});
114+
115+
// THEN
116+
test.throws(() => {
117+
new ecs.Ec2Service(stack, "Ec2Service", {
118+
cluster,
119+
taskDefinition,
120+
daemon: true,
121+
minimumHealthyPercent: 50
122+
});
123+
}, /Minimum healthy percent must be 0 for daemon mode./);
124+
125+
test.done();
126+
},
127+
78128
'Output does not contain DesiredCount if daemon mode is set'(test: Test) {
79129
// GIVEN
80130
const stack = new cdk.Stack();
@@ -141,7 +191,11 @@ export = {
141191

142192
// THEN
143193
expect(stack).to(haveResource("AWS::ECS::Service", {
144-
SchedulingStrategy: "DAEMON"
194+
SchedulingStrategy: "DAEMON",
195+
DeploymentConfiguration: {
196+
MaximumPercent: 100,
197+
MinimumHealthyPercent: 0
198+
},
145199
}));
146200

147201
test.done();

0 commit comments

Comments
 (0)