Skip to content

Commit

Permalink
Add cluster#addCapacityProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
SoManyHs committed Feb 15, 2021
1 parent ae20c6c commit 3e5fd57
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
26 changes: 22 additions & 4 deletions packages/@aws-cdk/aws-ecs/lib/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as iam from '@aws-cdk/aws-iam';
import * as kms from '@aws-cdk/aws-kms';
import * as cloudmap from '@aws-cdk/aws-servicediscovery';
import * as ssm from '@aws-cdk/aws-ssm';
import { Duration, IResource, Resource, Stack } from '@aws-cdk/core';
import { Duration, Lazy, IResource, Resource, Stack } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { InstanceDrainHook } from './drain-hook/instance-drain-hook';
import { ECSMetrics } from './ecs-canned-metrics.generated';
Expand Down Expand Up @@ -110,8 +110,10 @@ export class Cluster extends Resource implements ICluster {

/**
* The capacity providers associated with the cluster.
*
* Currently only FARGATE and FARGATE_SPOT are supported.
*/
public readonly capacityProviders?: string[];
private _capacityProviders: string[] = [];

/**
* The AWS Cloud Map namespace to associate with the cluster.
Expand Down Expand Up @@ -146,10 +148,12 @@ export class Cluster extends Resource implements ICluster {
clusterSettings = [{ name: 'containerInsights', value: props.containerInsights ? ContainerInsights.ENABLED : ContainerInsights.DISABLED }];
}

this._capacityProviders = props.capacityProviders ?? []; // TODO put adding FARGATE and FARGATE_SPOT here behind feature flag

const cluster = new CfnCluster(this, 'Resource', {
clusterName: this.physicalName,
clusterSettings,
capacityProviders: props.capacityProviders,
capacityProviders: Lazy.list({ produce: () => this._capacityProviders }, { omitEmpty: true }),
});

this.clusterArn = this.getResourceArnAttribute(cluster.attrArn, {
Expand All @@ -161,7 +165,6 @@ export class Cluster extends Resource implements ICluster {

this.vpc = props.vpc || new ec2.Vpc(this, 'Vpc', { maxAzs: 2 });

this.capacityProviders = props.capacityProviders;

this._defaultCloudMapNamespace = props.defaultCloudMapNamespace !== undefined
? this.addDefaultCloudMapNamespace(props.defaultCloudMapNamespace)
Expand Down Expand Up @@ -338,6 +341,21 @@ export class Cluster extends Resource implements ICluster {
}
}

/**
* addCapacityProvider adds the name of a capacityProvider to the list of supproted capacityProviders for a cluster.
*
* @param provider the capacity provider to add to this cluster.
*/
public addCapacityProvider(provider: string) {
if (!(provider === 'FARGATE' || provider === 'FARGATE_SPOT')) {
throw new Error('CapacityProvider not supported');
}

if (!this._capacityProviders.includes(provider)) {
this._capacityProviders.push(provider);
}
}

private configureWindowsAutoScalingGroup(autoScalingGroup: autoscaling.AutoScalingGroup, options: AddAutoScalingGroupCapacityOptions = {}) {
// clear the cache of the agent
autoScalingGroup.addUserData('Remove-Item -Recurse C:\\ProgramData\\Amazon\\ECS\\Cache');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export = {
test.done();
},

'with capacity provider strategies specified'(test: Test) {
'does not set launchType when capacity provider strategies specified'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'MyVpc', {});
Expand Down
49 changes: 49 additions & 0 deletions packages/@aws-cdk/aws-ecs/test/test.ecs-cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1666,6 +1666,7 @@ export = {
);
test.done();
},

'throws when machineImage and machineImageType both specified'(test: Test) {
// GIVEN
const app = new cdk.App();
Expand All @@ -1682,4 +1683,52 @@ export = {
}, /You can only specify either machineImage or machineImageType, not both./);
test.done();
},

'allows specifying capacityProviders'(test: Test) {
// GIVEN
const app = new cdk.App();
const stack = new cdk.Stack(app, 'test');

// WHEN
new ecs.Cluster(stack, 'EcsCluster', { capacityProviders: ['FARGATE_SPOT'] });

// THEN
expect(stack).to(haveResource('AWS::ECS::Cluster', {
CapacityProviders: ['FARGATE_SPOT'],
}));

test.done();
},

'allows adding capacityProviders post-construction'(test: Test) {
// GIVEN
const app = new cdk.App();
const stack = new cdk.Stack(app, 'test');
const cluster = new ecs.Cluster(stack, 'EcsCluster');

// WHEN
cluster.addCapacityProvider('FARGATE');
cluster.addCapacityProvider('FARGATE'); // does not add twice

// THEN
expect(stack).to(haveResource('AWS::ECS::Cluster', {
CapacityProviders: ['FARGATE'],
}));

test.done();
},

'throws for unsupported capacity providers'(test: Test) {
// GIVEN
const app = new cdk.App();
const stack = new cdk.Stack(app, 'test');
const cluster = new ecs.Cluster(stack, 'EcsCluster');

// THEN
test.throws(() => {
cluster.addCapacityProvider('HONK');
}, /CapacityProvider not supported/);

test.done();
},
};

0 comments on commit 3e5fd57

Please sign in to comment.