Skip to content

Commit

Permalink
fix(eks): generated cluster name can exceed 63 characters
Browse files Browse the repository at this point in the history
Since the generated cluster name includes the logical ID of the resource as a prefix with a postfix of the request ID, the resulting generated name can exceed 63 characters.

Fixes #5596

NOTE: since the current version of the EKS module have not been released yet, this is not a breaking change.
  • Loading branch information
Elad Ben-Israel committed Dec 31, 2019
1 parent 6f7e3c6 commit 5b8451a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { IsCompleteResponse, OnEventResponse } from '@aws-cdk/custom-resources/l
// eslint-disable-next-line import/no-extraneous-dependencies
import * as aws from 'aws-sdk';

const MAX_CLUSTER_NAME_LEN = 63;

export class ClusterResourceHandler {

public get clusterName() {
Expand Down Expand Up @@ -71,7 +73,7 @@ export class ClusterResourceHandler {
throw new Error('"roleArn" is required');
}

const clusterName = this.newProps.name || `${this.logicalResourceId}-${this.requestId}`;
const clusterName = this.newProps.name || this.generateClusterName();

const resp = await this.eks.createCluster({
...this.newProps,
Expand Down Expand Up @@ -221,6 +223,13 @@ export class ClusterResourceHandler {
};
}
}

private generateClusterName() {
// max length is 63 characters
const suffix = this.requestId.replace(/-/g, ''); // 32 chars
const prefix = this.logicalResourceId.substr(0, MAX_CLUSTER_NAME_LEN - suffix.length - 1);
return `${prefix}-${suffix}`;
}
}

export interface EksClient {
Expand Down Expand Up @@ -265,3 +274,4 @@ function analyzeUpdate(oldProps: Partial<aws.EKS.CreateClusterRequest>, newProps
updateLogging: JSON.stringify(newProps.logging) !== JSON.stringify(oldProps.logging),
};
}

18 changes: 9 additions & 9 deletions packages/@aws-cdk/aws-eks/test/test.cluster-resource-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export = {
subnetIds: ['subnet1', 'subnet2'],
securityGroupIds: ['sg1', 'sg2', 'sg3']
},
name: 'MyResourceId-fake-request-id'
name: 'MyResourceId-fakerequestid'
});

test.done();
Expand Down Expand Up @@ -200,15 +200,15 @@ export = {

// THEN
test.deepEqual(mocks.actualRequest.createClusterRequest!, {
name: 'MyResourceId-fake-request-id',
name: 'MyResourceId-fakerequestid',
roleArn: 'arn:of:role',
resourcesVpcConfig:
{
subnetIds: ['subnet1', 'subnet2'],
securityGroupIds: ['sg1', 'sg2', 'sg3']
}
});
test.deepEqual(resp, { PhysicalResourceId: 'MyResourceId-fake-request-id' });
test.deepEqual(resp, { PhysicalResourceId: 'MyResourceId-fakerequestid' });
test.done();
},

Expand All @@ -230,9 +230,9 @@ export = {
}));
const resp = await handler.onEvent();

test.deepEqual(resp, { PhysicalResourceId: 'MyResourceId-fake-request-id' });
test.deepEqual(resp, { PhysicalResourceId: 'MyResourceId-fakerequestid' });
test.deepEqual(mocks.actualRequest.createClusterRequest, {
name: 'MyResourceId-fake-request-id',
name: 'MyResourceId-fakerequestid',
roleArn: 'arn:of:role',
resourcesVpcConfig:
{
Expand All @@ -251,9 +251,9 @@ export = {
}));
const resp = await handler.onEvent();

test.deepEqual(resp, { PhysicalResourceId: 'MyResourceId-fake-request-id' });
test.deepEqual(resp, { PhysicalResourceId: 'MyResourceId-fakerequestid' });
test.deepEqual(mocks.actualRequest.createClusterRequest, {
name: 'MyResourceId-fake-request-id',
name: 'MyResourceId-fakerequestid',
roleArn: 'new-arn'
});
test.done();
Expand Down Expand Up @@ -295,9 +295,9 @@ export = {
const resp = await handler.onEvent();

// THEN
test.deepEqual(resp, { PhysicalResourceId: 'MyResourceId-fake-request-id' });
test.deepEqual(resp, { PhysicalResourceId: 'MyResourceId-fakerequestid' });
test.deepEqual(mocks.actualRequest.createClusterRequest, {
name: 'MyResourceId-fake-request-id',
name: 'MyResourceId-fakerequestid',
roleArn: 'new-arn'
});
test.done();
Expand Down

0 comments on commit 5b8451a

Please sign in to comment.