Skip to content

Commit 09e51f2

Browse files
authored
Merge pull request #1236 from awslabs/bugfix/deep-clone-constructs-1165
Bugfix/deep clone constructs 1165
2 parents 3e21f75 + 749878e commit 09e51f2

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

examples/passing-constructs.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import 'source-map-support/register';
2+
import * as cdk from 'aws-cdk-lib';
3+
import * as iam from 'aws-cdk-lib/aws-iam';
4+
import * as blueprints from '../lib';
5+
import { Construct } from 'constructs';
6+
7+
const app = new cdk.App();
8+
9+
const account = process.env.CDK_DEFAULT_ACCOUNT;
10+
const region = process.env.CDK_DEFAULT_REGION;
11+
12+
// Stack that creates a role
13+
class RoleStack extends cdk.Stack {
14+
public readonly customRole: iam.Role;
15+
16+
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
17+
super(scope, id, props);
18+
19+
this.customRole = new iam.Role(this, 'CustomRole', {
20+
assumedBy: new iam.ServicePrincipal('eks.amazonaws.com'),
21+
managedPolicies: [
22+
iam.ManagedPolicy.fromAwsManagedPolicyName('AmazonEKSClusterPolicy')
23+
]
24+
});
25+
}
26+
}
27+
28+
// Contrived addon that takes role from another stack
29+
class CustomRoleAddOn implements blueprints.ClusterAddOn {
30+
private role: iam.Role;
31+
32+
constructor(role: iam.Role) {
33+
this.role = role;
34+
}
35+
36+
deploy(clusterInfo: blueprints.ClusterInfo): Promise<Construct> {
37+
console.log(`Role ARN: ${this.role.roleArn}`);
38+
console.log(`Role Name: ${this.role.roleName}`);
39+
40+
// Return a dummy construct
41+
return Promise.resolve(new Construct(clusterInfo.cluster, 'CustomRoleAddOn'));
42+
}
43+
}
44+
45+
// Create role stack
46+
const roleStack = new RoleStack(app, 'role-stack', {
47+
env: { account, region }
48+
});
49+
50+
// Example customer issue reproduction:
51+
const addOns: Array<blueprints.ClusterAddOn> = [
52+
new CustomRoleAddOn(roleStack.customRole)
53+
];
54+
55+
const stack = blueprints.EksBlueprint.builder()
56+
.account(account)
57+
.region(region)
58+
.addOns(...addOns)
59+
.version("auto")
60+
.build(app, 'passing-constructs');
61+
62+
// Add dependency
63+
stack.addDependency(roleStack);
64+
65+
void stack; // Keep for debugging

lib/utils/object-utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { KubernetesVersion } from "aws-cdk-lib/aws-eks";
22
import { cloneDeepWith } from 'lodash';
3+
import {Construct} from "constructs";
34
import * as nutil from 'node:util/types';
45

56
export const setPath = (obj : any, path: string, val: any) => {
@@ -22,7 +23,7 @@ export function cloneDeep<T>(source: T, resolveFn?: (arg: any) => any ): T {
2223
if (
2324
value &&
2425
(value instanceof KubernetesVersion ||
25-
(typeof value === "object" && "managedPolicyArn" in value) || // checks for ManagedPolicyReference
26+
(Construct.isConstruct(value)) || // checks for ManagedPolicyReference
2627
nutil.isProxy(value))
2728
) {
2829
return resolveFn ? resolveFn(value) : value;

0 commit comments

Comments
 (0)