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(eks): can't define a cluster with multiple Fargate profiles #8374

Merged
merged 4 commits into from
Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 18 additions & 0 deletions packages/@aws-cdk/aws-eks/lib/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,12 @@ export class Cluster extends Resource implements ICluster {
*/
public readonly defaultNodegroup?: Nodegroup;

/**
* If the cluster has one (or more) FargateProfiles associated, this array
* will hold a reference to each.
*/
private readonly _fargateProfiles: FargateProfile[] = [];

/**
* If this cluster is kubectl-enabled, returns the `ClusterResource` object
* that manages it. If this cluster is not kubectl-enabled (i.e. uses the
Expand Down Expand Up @@ -757,6 +763,18 @@ export class Cluster extends Resource implements ICluster {
return this.stack.node.tryFindChild(uid) as KubectlProvider || new KubectlProvider(this.stack, uid);
}

/**
* Internal API used by `FargateProfile` to keep inventory of Fargate profiles associated with
* this cluster, for the sake of ensuring the profiles are created sequentially.
*
* @returns the list of FargateProfiles attached to this cluster, including the one just attached.
* @internal
*/
public _attachFargateProfile(fargateProfile: FargateProfile): FargateProfile[] {
this._fargateProfiles.push(fargateProfile);
return this._fargateProfiles;
}

/**
* Installs the AWS spot instance interrupt handler on the cluster if it's not
* already added.
Expand Down
8 changes: 8 additions & 0 deletions packages/@aws-cdk/aws-eks/lib/fargate-profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ export class FargateProfile extends Construct implements ITaggable {
this.fargateProfileArn = resource.getAttString('fargateProfileArn');
this.fargateProfileName = resource.ref;

// Fargate profiles must be created sequentially. If other profile(s) already
// exist on the same cluster, create a dependency to force sequential creation.
const clusterFargateProfiles = props.cluster._attachFargateProfile(this);
if (clusterFargateProfiles.length > 1) {
const previousProfile = clusterFargateProfiles[clusterFargateProfiles.length - 2];
resource.node.addDependency(previousProfile);
}

// map the fargate pod execution role to the relevant groups in rbac
// see https://github.com/aws/aws-cdk/issues/7981
props.cluster.awsAuth.addRoleMapping(role, {
Expand Down
45 changes: 44 additions & 1 deletion packages/@aws-cdk/aws-eks/test/test.fargate.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, ResourcePart } from '@aws-cdk/assert';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as iam from '@aws-cdk/aws-iam';
import { Stack, Tag } from '@aws-cdk/core';
Expand Down Expand Up @@ -252,6 +252,49 @@ export = {
test.done();
},

'multiple Fargate profiles added to a cluster are processed sequentially'(test: Test) {
// GIVEN
const stack = new Stack();
const cluster = new eks.Cluster(stack, 'MyCluster');

// WHEN
cluster.addFargateProfile('MyProfile1', {
selectors: [ { namespace: 'namespace1' } ],
});
cluster.addFargateProfile('MyProfile2', {
selectors: [ { namespace: 'namespace2' } ],
});

// THEN
expect(stack).to(haveResource('Custom::AWSCDK-EKS-FargateProfile', {
Config: {
clusterName: { Ref: 'MyCluster8AD82BF8' },
podExecutionRoleArn: { 'Fn::GetAtt': [ 'MyClusterfargateprofileMyProfile1PodExecutionRole794E9E37', 'Arn' ] },
selectors: [ { namespace: 'namespace1' } ],
},
}));
expect(stack).to(haveResource('Custom::AWSCDK-EKS-FargateProfile', {
Properties: {
ServiceToken: { 'Fn::GetAtt': [
'awscdkawseksClusterResourceProviderNestedStackawscdkawseksClusterResourceProviderNestedStackResource9827C454',
'Outputs.awscdkawseksClusterResourceProviderframeworkonEventEA97AA31Arn',
]},
AssumeRoleArn: { 'Fn::GetAtt': [ 'MyClusterCreationRoleB5FA4FF3', 'Arn' ] },
Config: {
clusterName: { Ref: 'MyCluster8AD82BF8' },
podExecutionRoleArn: { 'Fn::GetAtt': [ 'MyClusterfargateprofileMyProfile2PodExecutionRoleD1151CCF', 'Arn' ] },
selectors: [ { namespace: 'namespace2' } ],
},
},
DependsOn: [
'MyClusterfargateprofileMyProfile1PodExecutionRole794E9E37',
'MyClusterfargateprofileMyProfile1879D501A',
],
}, ResourcePart.CompleteDefinition));

test.done();
},

'fargate role is added to RBAC'(test: Test) {
// GIVEN
const stack = new Stack();
Expand Down