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

fix(ecs-patterns): minHealthyPercent and maxHealthyPercent props validation #26193

Merged
merged 5 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ export class ApplicationLoadBalancedFargateService extends ApplicationLoadBalanc
throw new Error('You must specify one of: taskDefinition or image');
}

this.validateHealthyPercentage('minHealthyPercent', props.minHealthyPercent);
this.validateHealthyPercentage('maxHealthyPercent', props.maxHealthyPercent);
Copy link
Contributor

Choose a reason for hiding this comment

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

While we're here, please also validate that min < max.


const desiredCount = FeatureFlags.of(this).isEnabled(cxapi.ECS_REMOVE_DEFAULT_DESIRED_COUNT) ? this.internalDesiredCount : this.desiredCount;

this.service = new FargateService(this, 'Service', {
Expand All @@ -120,4 +123,14 @@ export class ApplicationLoadBalancedFargateService extends ApplicationLoadBalanc
});
this.addServiceAsTarget(this.service);
}

/**
* Throws an error if the specified percent is not an integer or negative.
*/
private validateHealthyPercentage(name: string, value?: number) {
if (value === undefined) { return; }
if (!Number.isInteger(value) || value < 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (value === undefined) { return; }
if (!Number.isInteger(value) || value < 0) {
if (value === undefined || Token.isUnresolved(value)) {
return;
}

throw new Error(`${name}: Must be a non-negative integer; received ${value}`);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1208,3 +1208,37 @@ test('NetworkLoadBalancedFargateService multiple capacity provider strategies ar
]),
});
});

test('should validate minHealthyPercent', () => {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'VPC');
const cluster = new ecs.Cluster(stack, 'Cluster', { vpc });

// WHEN
expect(() => new ecsPatterns.ApplicationLoadBalancedFargateService(stack, 'Service', {
cluster,
taskImageOptions: {
image: ecs.ContainerImage.fromRegistry('/aws/aws-example-app'),
dockerLabels: { label1: 'labelValue1', label2: 'labelValue2' },
},
minHealthyPercent: 0.5,
})).toThrow(/Must be a non-negative integer/);
});

test('should validate maxHealthyPercent', () => {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'VPC');
const cluster = new ecs.Cluster(stack, 'Cluster', { vpc });

// WHEN
expect(() => new ecsPatterns.ApplicationLoadBalancedFargateService(stack, 'Service', {
cluster,
taskImageOptions: {
image: ecs.ContainerImage.fromRegistry('/aws/aws-example-app'),
dockerLabels: { label1: 'labelValue1', label2: 'labelValue2' },
},
maxHealthyPercent: 0.5,
})).toThrow(/Must be a non-negative integer/);
});