Skip to content

Commit

Permalink
Merge pull request #922 from aws-quickstart/bug/import-cluster-provid…
Browse files Browse the repository at this point in the history
…er-token-failure

Fixes #916
  • Loading branch information
elamaran11 authored Feb 13, 2024
2 parents 4f406be + 6ba7dae commit 26bc12d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/cluster-providers/import-cluster-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@ import { Construct } from "constructs";
import { getResource } from "../resource-providers/utils";
import { LookupOpenIdConnectProvider } from "../resource-providers";
import { logger } from "../utils";
import { isToken, uniqueId } from "../utils/id-utils";


/**
* Properties object for the ImportClusterProvider.
*/
export interface ImportClusterProviderProps extends Omit<eks.ClusterAttributes, "vpc"> {

/**
* Used for the CDK construct id for the imported cluster. Useful when passing tokens for cluster name.
*/
id?: string;

/**
* This property is needed as it drives selection of certain add-on versions as well as kubectl layer.
*/
Expand All @@ -27,7 +34,11 @@ export interface ImportClusterProviderProps extends Omit<eks.ClusterAttributes,
*/
export class ImportClusterProvider implements ClusterProvider {

constructor(private readonly props: ImportClusterProviderProps) { }
readonly id: string;

constructor(readonly props: ImportClusterProviderProps) {
this.id = props.id ?? (isToken(this.props.clusterName) ? uniqueId() : this.props.clusterName);
}

/**
* Implements contract method to create a cluster, by importing an existing cluster.
Expand All @@ -42,8 +53,7 @@ export class ImportClusterProvider implements ClusterProvider {
if(! props.kubectlLayer) {
props.kubectlLayer = selectKubectlLayer(scope, props.version);
}

const existingCluster = eks.Cluster.fromClusterAttributes(scope, 'imported-cluster-' + this.props.clusterName, props);
const existingCluster = eks.Cluster.fromClusterAttributes(scope, `imported-cluster-${this.id}`, props);
return new ClusterInfo(existingCluster, this.props.version);
}

Expand Down
19 changes: 19 additions & 0 deletions lib/utils/id-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Token } from 'aws-cdk-lib';
import { v4 as uuid } from 'uuid';

/**
* Generates a globally unique identifier.
* @returns string representation of a GUID
*/
export function uniqueId() : string {
return uuid();
}

/**
* Tests the input to see if it is a token (unresolved token representation of a reference in CDK, e.g. ${TOKEN[Bucket.Name.1234]})
* @param input string containing the string identifier
* @returns true if the passed input is a token
*/
export function isToken(input: string) : boolean {
return Token.isUnresolved(input);
}
19 changes: 19 additions & 0 deletions test/clusterprovider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import { SubnetType } from 'aws-cdk-lib/aws-ec2';
import { CapacityType, KubernetesVersion } from 'aws-cdk-lib/aws-eks';
import * as blueprints from '../lib';
import { AsgClusterProvider, MngClusterProvider } from '../lib';
import { logger } from '../lib/utils';
import { isToken } from '../lib/utils/id-utils';

const addAutoScalingGroupCapacityMock = jest.spyOn(eks.Cluster.prototype, 'addAutoScalingGroupCapacity');
const addNodegroupCapacityMock = jest.spyOn(eks.Cluster.prototype, 'addNodegroupCapacity');
logger.settings.minLevel = 3;

test("Generic cluster provider correctly registers managed node groups", async () => {
const app = new cdk.App();
Expand Down Expand Up @@ -341,3 +344,19 @@ test("Kubernetes Version gets set correctly in NodeGroup", () => {

expect(stack.getClusterInfo().version.version).toBe("1.28");
});

test("Import cluster provider can use output values from other stacks as params", () => {
const importClusterProvider = new blueprints.ImportClusterProvider({
clusterName: cdk.Fn.importValue('ClusterName'),
version: KubernetesVersion.V1_27,
clusterEndpoint: cdk.Fn.importValue('ClusterEndpoint'),
openIdConnectProvider: blueprints.getResource((context) =>
new blueprints.LookupOpenIdConnectProvider(
'classified',
).provide(context),
),
kubectlRoleArn: cdk.Fn.importValue('KubectlRoleArn'),
clusterSecurityGroupId: cdk.Fn.importValue('ClusterSecurityGroupId'),
});
expect(isToken(importClusterProvider.id)).toBeFalsy();
});

0 comments on commit 26bc12d

Please sign in to comment.