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): services essential container exceptions thrown too soon #13240

Merged
merged 5 commits into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
7 changes: 3 additions & 4 deletions packages/@aws-cdk/aws-ecs/lib/ec2/ec2-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,6 @@ export class Ec2Service extends BaseService implements IEc2Service {

this.addPlacementConstraints(...props.placementConstraints || []);
this.addPlacementStrategies(...props.placementStrategies || []);

if (!this.taskDefinition.defaultContainer) {
throw new Error('A TaskDefinition must have at least one essential container');
}
}

/**
Expand Down Expand Up @@ -266,6 +262,9 @@ export class Ec2Service extends BaseService implements IEc2Service {
if (!this.cluster.hasEc2Capacity) {
ret.push('Cluster for this service needs Ec2 capacity. Call addXxxCapacity() on the cluster.');
}
if (!this.taskDefinition.defaultContainer) {
ret.push('A TaskDefinition must have at least one essential container');
}
return ret;
}
}
Expand Down
9 changes: 7 additions & 2 deletions packages/@aws-cdk/aws-ecs/lib/fargate/fargate-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,15 @@ export class FargateService extends BaseService implements IFargateService {
}

this.configureAwsVpcNetworkingWithSecurityGroups(props.cluster.vpc, props.assignPublicIp, props.vpcSubnets, securityGroups);
}

if (!props.taskDefinition.defaultContainer) {
throw new Error('A TaskDefinition must have at least one essential container');
protected onValidate() {
const validationErrors = super.onValidate();
Copy link
Contributor

Choose a reason for hiding this comment

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

I like the change, but we're changing the way we do validations in v2, and we need new code to be compatible with the new way of doing things.

New way looks like:

this.node.addValidation({
   validate: () => !this.taskDefinition.defaultContainer ? ['A taskdefinition...'] : [],
});

if (!this.taskDefinition.defaultContainer) {
validationErrors.push('A TaskDefinition must have at least one essential container');
}

return validationErrors;
}
}

Expand Down
44 changes: 39 additions & 5 deletions packages/@aws-cdk/aws-ecs/test/ec2/ec2-service.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, haveResource } from '@aws-cdk/assert';
import { expect, haveResource, haveResourceLike } from '@aws-cdk/assert';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as elb from '@aws-cdk/aws-elasticloadbalancing';
import * as elbv2 from '@aws-cdk/aws-elasticloadbalancingv2';
Expand Down Expand Up @@ -541,14 +541,48 @@ nodeunitShim({
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'Ec2TaskDef');

// Errors on validation, not on construction.
new ecs.Ec2Service(stack, 'Ec2Service', {
cluster,
taskDefinition,
});

// THEN
test.throws(() => {
new ecs.Ec2Service(stack, 'Ec2Service', {
cluster,
taskDefinition,
});
expect(stack);
}, /one essential container/);

test.done();
},

'allows adding the default container after creating the service'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(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');

new ecs.Ec2Service(stack, 'FargateService', {
cluster,
taskDefinition,
});

// Add the container *after* creating the service
taskDefinition.addContainer('main', {
image: ecs.ContainerImage.fromRegistry('somecontainer'),
memoryReservationMiB: 10,
});

// THEN
expect(stack).to(haveResourceLike('AWS::ECS::TaskDefinition', {
ContainerDefinitions: [
{
Name: 'main',
},
],
}));

test.done();
},

Expand Down
40 changes: 36 additions & 4 deletions packages/@aws-cdk/aws-ecs/test/fargate/fargate-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,14 +482,46 @@ nodeunitShim({
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
const taskDefinition = new ecs.FargateTaskDefinition(stack, 'FargateTaskDef');

// Errors on validation, not on construction.
new ecs.FargateService(stack, 'FargateService', {
cluster,
taskDefinition,
});

// THEN
test.throws(() => {
new ecs.FargateService(stack, 'FargateService', {
cluster,
taskDefinition,
});
expect(stack);
}, /one essential container/);

test.done();
},

'allows adding the default container after creating the service'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'MyVpc', {});
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
const taskDefinition = new ecs.FargateTaskDefinition(stack, 'FargateTaskDef');

new ecs.FargateService(stack, 'FargateService', {
cluster,
taskDefinition,
});

// Add the container *after* creating the service
taskDefinition.addContainer('main', {
image: ecs.ContainerImage.fromRegistry('somecontainer'),
});

// THEN
expect(stack).to(haveResourceLike('AWS::ECS::TaskDefinition', {
ContainerDefinitions: [
{
Name: 'main',
},
],
}));

test.done();
},

Expand Down