Skip to content

Commit

Permalink
fix(aws-ecs): fix default daemon deploymentConfig values (#2210)
Browse files Browse the repository at this point in the history
Fixes #2209
  • Loading branch information
SoManyHs authored and rix0rrr committed Apr 12, 2019
1 parent 87b1ea0 commit c2e806b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-ecs/lib/base/base-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface BaseServiceProps {
* service's DesiredCount value, that can run in a service during a
* deployment.
*
* @default 200
* @default 100 if daemon, otherwise 200
*/
readonly maximumPercent?: number;

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

Expand Down
10 changes: 10 additions & 0 deletions packages/@aws-cdk/aws-ecs/lib/ec2/ec2-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ export class Ec2Service extends BaseService implements elb.ILoadBalancerTarget {
throw new Error('Daemon mode launches one task on every instance. Don\'t supply desiredCount.');
}

if (props.daemon && props.maximumPercent !== undefined && props.maximumPercent !== 100) {
throw new Error('Maximum percent must be 100 for daemon mode.');
}

if (props.daemon && props.minimumHealthyPercent !== undefined && props.minimumHealthyPercent !== 0) {
throw new Error('Minimum healthy percent must be 0 for daemon mode.');
}

if (!isEc2Compatible(props.taskDefinition.compatibility)) {
throw new Error('Supplied TaskDefinition is not configured for compatibility with EC2');
}
Expand All @@ -78,6 +86,8 @@ export class Ec2Service extends BaseService implements elb.ILoadBalancerTarget {
...props,
// If daemon, desiredCount must be undefined and that's what we want. Otherwise, default to 1.
desiredCount: props.daemon || props.desiredCount !== undefined ? props.desiredCount : 1,
maximumPercent: props.daemon && props.maximumPercent === undefined ? 100 : props.maximumPercent,
minimumHealthyPercent: props.daemon && props.minimumHealthyPercent === undefined ? 0 : props.minimumHealthyPercent ,
},
{
cluster: props.cluster.clusterName,
Expand Down
56 changes: 55 additions & 1 deletion packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,56 @@ export = {
test.done();
},

"errors if daemon and maximumPercent not 100"(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.VpcNetwork(stack, 'MyVpc', {});
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
cluster.addCapacity('DefaultAutoScalingGroup', { instanceType: new ec2.InstanceType('t2.micro') });
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'Ec2TaskDef');
taskDefinition.addContainer('BaseContainer', {
image: ecs.ContainerImage.fromRegistry('test'),
memoryReservationMiB: 10,
});

// THEN
test.throws(() => {
new ecs.Ec2Service(stack, "Ec2Service", {
cluster,
taskDefinition,
daemon: true,
maximumPercent: 300
});
}, /Maximum percent must be 100 for daemon mode./);

test.done();
},

"errors if daemon and minimum not 0"(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.VpcNetwork(stack, 'MyVpc', {});
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
cluster.addCapacity('DefaultAutoScalingGroup', { instanceType: new ec2.InstanceType('t2.micro') });
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'Ec2TaskDef');
taskDefinition.addContainer('BaseContainer', {
image: ecs.ContainerImage.fromRegistry('test'),
memoryReservationMiB: 10,
});

// THEN
test.throws(() => {
new ecs.Ec2Service(stack, "Ec2Service", {
cluster,
taskDefinition,
daemon: true,
minimumHealthyPercent: 50
});
}, /Minimum healthy percent must be 0 for daemon mode./);

test.done();
},

'Output does not contain DesiredCount if daemon mode is set'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down Expand Up @@ -141,7 +191,11 @@ export = {

// THEN
expect(stack).to(haveResource("AWS::ECS::Service", {
SchedulingStrategy: "DAEMON"
SchedulingStrategy: "DAEMON",
DeploymentConfiguration: {
MaximumPercent: 100,
MinimumHealthyPercent: 0
},
}));

test.done();
Expand Down

0 comments on commit c2e806b

Please sign in to comment.