Skip to content

Commit 8a96454

Browse files
authored
chore(eks-v2-alpha): remove nested stack (#32692)
### Reason for this change In the current EKS module, `kubectlProvider` is created in a nested stack. The nested stack here is not necessary and hence moving it to the main stack. ### Description of changes Move `kubectlProvider` to the main stack. ### Describe any new or updated permissions being added <!— What new or updated IAM permissions are needed to support the changes being introduced ? --> ### Description of how you validated changes Updated unit tests so they all passed ### Checklist - [ ] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent d82f214 commit 8a96454

File tree

5 files changed

+59
-126
lines changed

5 files changed

+59
-126
lines changed

packages/@aws-cdk/aws-eks-v2-alpha/lib/kubectl-provider.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Construct, IConstruct } from 'constructs';
22
import { ICluster, Cluster } from './cluster';
33
import * as iam from 'aws-cdk-lib/aws-iam';
44
import * as lambda from 'aws-cdk-lib/aws-lambda';
5-
import { Duration, Stack, NestedStack, Names, CfnCondition, Fn, Aws } from 'aws-cdk-lib/core';
5+
import { Duration, Stack, Names, CfnCondition, Fn, Aws } from 'aws-cdk-lib/core';
66
import * as cr from 'aws-cdk-lib/custom-resources';
77
import { AwsCliLayer } from 'aws-cdk-lib/lambda-layer-awscli';
88
import { KubectlLayer } from 'aws-cdk-lib/lambda-layer-kubectl';
@@ -61,7 +61,7 @@ export interface IKubectlProvider extends IConstruct {
6161
/**
6262
* Implementation of Kubectl Lambda
6363
*/
64-
export class KubectlProvider extends NestedStack implements IKubectlProvider {
64+
export class KubectlProvider extends Construct implements IKubectlProvider {
6565

6666
/**
6767
* Take existing provider or create new based on cluster

packages/@aws-cdk/aws-eks-v2-alpha/test/cluster.test.ts

+43-110
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,7 @@ describe('cluster', () => {
10841084
Type: 'Custom::AWSCDK-EKS-KubernetesResource',
10851085
Properties: {
10861086
ServiceToken: {
1087-
'Fn::ImportValue': 'Stack:ExportsOutputFnGetAttawscdkawseksKubectlProviderNestedStackawscdkawseksKubectlProviderNestedStackResourceA7AEBA6BOutputsStackawscdkawseksKubectlProviderframeworkonEvent8897FD9BArn49BEF20C',
1087+
'Fn::ImportValue': 'Stack:ExportsOutputFnGetAttawscdkawseksKubectlProviderframeworkonEvent0A650005Arn27EC41A8',
10881088
},
10891089
Manifest: '[{\"foo\":\"bar\"}]',
10901090
ClusterName: { 'Fn::ImportValue': 'Stack:ExportsOutputRefcluster611F8AFFA07FC079' },
@@ -2069,11 +2069,9 @@ describe('cluster', () => {
20692069
},
20702070
});
20712071

2072-
// the kubectl provider is inside a nested stack.
2073-
const nested = stack.node.tryFindChild('@aws-cdk/aws-eks.KubectlProvider') as cdk.NestedStack;
2074-
Template.fromStack(nested).hasResourceProperties('AWS::Lambda::Function', {
2072+
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
20752073
VpcConfig: {
2076-
SecurityGroupIds: [{ Ref: 'referencetoStackCluster17032651AClusterSecurityGroupId' }],
2074+
SecurityGroupIds: [{ 'Fn::GetAtt': ['Cluster192CD0375', 'ClusterSecurityGroupId'] }],
20772075
},
20782076
});
20792077
});
@@ -2101,9 +2099,7 @@ describe('cluster', () => {
21012099
},
21022100
});
21032101

2104-
// the kubectl provider is inside a nested stack.
2105-
const nested = stack.node.tryFindChild('@aws-cdk/aws-eks.KubectlProvider') as cdk.NestedStack;
2106-
Template.fromStack(nested).hasResourceProperties('AWS::Lambda::Function', {
2102+
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
21072103
Environment: {
21082104
Variables: {
21092105
Foo: 'Bar',
@@ -2139,11 +2135,9 @@ describe('cluster', () => {
21392135
},
21402136
});
21412137

2142-
// the kubectl provider is inside a nested stack.
2143-
const nested = stack.node.tryFindChild('@aws-cdk/aws-eks.KubectlProvider') as cdk.NestedStack;
2144-
Template.fromStack(nested).hasResourceProperties('AWS::Lambda::Function', {
2138+
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
21452139
Role: {
2146-
Ref: 'referencetoStackKubectlIamRole02F8947EArn',
2140+
'Fn::GetAtt': ['awscdkawseksKubectlProviderframeworkonEventServiceRoleF4FAF053', 'Arn'],
21472141
},
21482142
});
21492143
});
@@ -2165,10 +2159,9 @@ describe('cluster', () => {
21652159
chart,
21662160
});
21672161

2168-
const nested = stack.node.tryFindChild('Imported-KubectlProvider') as cdk.NestedStack;
2169-
Template.fromStack(nested).hasResourceProperties('AWS::Lambda::Function', {
2162+
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
21702163
Role: {
2171-
Ref: 'referencetoKubectlLambdaRole7D084D94Arn',
2164+
'Fn::GetAtt': ['ImportedKubectlProviderframeworkonEventServiceRole6603B49A', 'Arn'],
21722165
},
21732166
});
21742167
Template.fromStack(stack).hasResourceProperties(HelmChart.RESOURCE_TYPE, {
@@ -2199,11 +2192,9 @@ describe('cluster', () => {
21992192
vpcSubnets: [{ subnetType: ec2.SubnetType.PUBLIC }],
22002193
});
22012194

2202-
const nested = stack.node.tryFindChild('@aws-cdk/aws-eks.KubectlProvider') as cdk.NestedStack;
2203-
22042195
// we don't attach vpc config in case endpoint is public only, regardless of whether
22052196
// the vpc has private subnets or not.
2206-
Template.fromStack(nested).hasResourceProperties('AWS::Lambda::Function', {
2197+
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
22072198
VpcConfig: Match.absent(),
22082199
});
22092200
});
@@ -2217,11 +2208,9 @@ describe('cluster', () => {
22172208
endpointAccess: eks.EndpointAccess.PUBLIC,
22182209
});
22192210

2220-
const nested = stack.node.tryFindChild('@aws-cdk/aws-eks.KubectlProvider') as cdk.NestedStack;
2221-
22222211
// we don't attach vpc config in case endpoint is public only, regardless of whether
22232212
// the vpc has private subnets or not.
2224-
Template.fromStack(nested).hasResourceProperties('AWS::Lambda::Function', {
2213+
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
22252214
VpcConfig: Match.absent(),
22262215
});
22272216
});
@@ -2248,11 +2237,9 @@ describe('cluster', () => {
22482237
endpointAccess: eks.EndpointAccess.PRIVATE,
22492238
});
22502239

2251-
const nested = stack.node.tryFindChild('@aws-cdk/aws-eks.KubectlProvider') as cdk.NestedStack;
2252-
2253-
const functions = Template.fromStack(nested).findResources('AWS::Lambda::Function');
2254-
expect(functions.Handler886CB40B.Properties.VpcConfig.SubnetIds.length).not.toEqual(0);
2255-
expect(functions.Handler886CB40B.Properties.VpcConfig.SecurityGroupIds.length).not.toEqual(0);
2240+
const functions = Template.fromStack(stack).findResources('AWS::Lambda::Function');
2241+
expect(functions.awscdkawseksKubectlProviderHandlerAABA4423.Properties.VpcConfig.SubnetIds.length).not.toEqual(0);
2242+
expect(functions.awscdkawseksKubectlProviderHandlerAABA4423.Properties.VpcConfig.SecurityGroupIds.length).not.toEqual(0);
22562243
});
22572244

22582245
test('private and non restricted public without private subnets', () => {
@@ -2265,11 +2252,9 @@ describe('cluster', () => {
22652252
vpcSubnets: [{ subnetType: ec2.SubnetType.PUBLIC }],
22662253
});
22672254

2268-
const nested = stack.node.tryFindChild('@aws-cdk/aws-eks.KubectlProvider') as cdk.NestedStack;
2269-
22702255
// we don't have private subnets, but we don't need them since public access
22712256
// is not restricted.
2272-
Template.fromStack(nested).hasResourceProperties('AWS::Lambda::Function', {
2257+
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
22732258
VpcConfig: Match.absent(),
22742259
});
22752260
});
@@ -2283,12 +2268,10 @@ describe('cluster', () => {
22832268
endpointAccess: eks.EndpointAccess.PUBLIC_AND_PRIVATE,
22842269
});
22852270

2286-
const nested = stack.node.tryFindChild('@aws-cdk/aws-eks.KubectlProvider') as cdk.NestedStack;
2287-
22882271
// we have private subnets so we should use them.
2289-
const functions = Template.fromStack(nested).findResources('AWS::Lambda::Function');
2290-
expect(functions.Handler886CB40B.Properties.VpcConfig.SubnetIds.length).not.toEqual(0);
2291-
expect(functions.Handler886CB40B.Properties.VpcConfig.SecurityGroupIds.length).not.toEqual(0);
2272+
const functions = Template.fromStack(stack).findResources('AWS::Lambda::Function');
2273+
expect(functions.awscdkawseksKubectlProviderHandlerAABA4423.Properties.VpcConfig.SubnetIds.length).not.toEqual(0);
2274+
expect(functions.awscdkawseksKubectlProviderHandlerAABA4423.Properties.VpcConfig.SecurityGroupIds.length).not.toEqual(0);
22922275
});
22932276

22942277
test('private and restricted public without private subnets', () => {
@@ -2313,12 +2296,10 @@ describe('cluster', () => {
23132296
endpointAccess: eks.EndpointAccess.PUBLIC_AND_PRIVATE.onlyFrom('1.2.3.4/32'),
23142297
});
23152298

2316-
const nested = stack.node.tryFindChild('@aws-cdk/aws-eks.KubectlProvider') as cdk.NestedStack;
2317-
23182299
// we have private subnets so we should use them.
2319-
const functions = Template.fromStack(nested).findResources('AWS::Lambda::Function');
2320-
expect(functions.Handler886CB40B.Properties.VpcConfig.SubnetIds.length).not.toEqual(0);
2321-
expect(functions.Handler886CB40B.Properties.VpcConfig.SecurityGroupIds.length).not.toEqual(0);
2300+
const functions = Template.fromStack(stack).findResources('AWS::Lambda::Function');
2301+
expect(functions.awscdkawseksKubectlProviderHandlerAABA4423.Properties.VpcConfig.SubnetIds.length).not.toEqual(0);
2302+
expect(functions.awscdkawseksKubectlProviderHandlerAABA4423.Properties.VpcConfig.SecurityGroupIds.length).not.toEqual(0);
23222303
});
23232304

23242305
test('private endpoint access selects only private subnets from looked up vpc', () => {
@@ -2372,8 +2353,7 @@ describe('cluster', () => {
23722353
endpointAccess: eks.EndpointAccess.PRIVATE,
23732354
});
23742355

2375-
const nested = stack.node.tryFindChild('@aws-cdk/aws-eks.KubectlProvider') as cdk.NestedStack;
2376-
Template.fromStack(nested).hasResourceProperties('AWS::Lambda::Function', {
2356+
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
23772357
VpcConfig: { SubnetIds: ['subnet-private-in-us-east-1a'] },
23782358
});
23792359
});
@@ -2437,8 +2417,7 @@ describe('cluster', () => {
24372417
}],
24382418
});
24392419

2440-
const nested = stack.node.tryFindChild('@aws-cdk/aws-eks.KubectlProvider') as cdk.NestedStack;
2441-
Template.fromStack(nested).hasResourceProperties('AWS::Lambda::Function', {
2420+
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
24422421
VpcConfig: { SubnetIds: ['subnet-private-in-us-east-1a'] },
24432422
});
24442423
});
@@ -2462,11 +2441,10 @@ describe('cluster', () => {
24622441
}],
24632442
});
24642443

2465-
const nested = stack.node.tryFindChild('@aws-cdk/aws-eks.KubectlProvider') as cdk.NestedStack;
2466-
Template.fromStack(nested).hasResourceProperties('AWS::Lambda::Function', {
2444+
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
24672445
VpcConfig: {
24682446
SubnetIds: [
2469-
{ Ref: 'referencetoStackVpcPrivateSubnet1Subnet8E6A14CBRef' },
2447+
{ Ref: 'VpcPrivateSubnet1Subnet536B997A' },
24702448
'subnet-unknown',
24712449
],
24722450
},
@@ -2488,8 +2466,7 @@ describe('cluster', () => {
24882466
}],
24892467
});
24902468

2491-
const nested = stack.node.tryFindChild('@aws-cdk/aws-eks.KubectlProvider') as cdk.NestedStack;
2492-
Template.fromStack(nested).hasResourceProperties('AWS::Lambda::Function', {
2469+
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
24932470
VpcConfig: { SubnetIds: ['subnet1'] },
24942471
});
24952472
});
@@ -2541,21 +2518,19 @@ describe('cluster', () => {
25412518
},
25422519
});
25432520

2544-
// the kubectl provider is inside a nested stack.
2545-
const nested = stack.node.tryFindChild('@aws-cdk/aws-eks.KubectlProvider') as cdk.NestedStack;
2546-
Template.fromStack(nested).hasResourceProperties('AWS::Lambda::Function', {
2521+
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
25472522
VpcConfig: {
25482523
SecurityGroupIds: [
25492524
{
2550-
Ref: 'referencetoStackCluster17032651AClusterSecurityGroupId',
2525+
'Fn::GetAtt': ['Cluster192CD0375', 'ClusterSecurityGroupId'],
25512526
},
25522527
],
25532528
SubnetIds: [
25542529
{
2555-
Ref: 'referencetoStackVpcPrivate1Subnet1Subnet6764A0F6Ref',
2530+
Ref: 'VpcPrivate1Subnet1SubnetC688B2B1',
25562531
},
25572532
{
2558-
Ref: 'referencetoStackVpcPrivate1Subnet2SubnetDFD49645Ref',
2533+
Ref: 'VpcPrivate1Subnet2SubnetA2AF15C7',
25592534
},
25602535
],
25612536
},
@@ -2604,10 +2579,8 @@ describe('cluster', () => {
26042579
},
26052580
});
26062581

2607-
// the kubectl provider is inside a nested stack.
2608-
const nested = stack.node.tryFindChild('@aws-cdk/aws-eks.KubectlProvider') as cdk.NestedStack;
2609-
const functions = Template.fromStack(nested).findResources('AWS::Lambda::Function');
2610-
expect(functions.Handler886CB40B.Properties.VpcConfig.SubnetIds.length).toEqual(16);
2582+
const functions = Template.fromStack(stack).findResources('AWS::Lambda::Function');
2583+
expect(functions.awscdkawseksKubectlProviderHandlerAABA4423.Properties.VpcConfig.SubnetIds.length).toEqual(16);
26112584
});
26122585

26132586
test('kubectl provider considers vpc subnet selection', () => {
@@ -2653,27 +2626,25 @@ describe('cluster', () => {
26532626
},
26542627
});
26552628

2656-
// the kubectl provider is inside a nested stack.
2657-
const nested = stack.node.tryFindChild('@aws-cdk/aws-eks.KubectlProvider') as cdk.NestedStack;
2658-
Template.fromStack(nested).hasResourceProperties('AWS::Lambda::Function', {
2629+
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
26592630
VpcConfig: {
26602631
SecurityGroupIds: [
26612632
{
2662-
Ref: 'referencetoStackCluster17032651AClusterSecurityGroupId',
2633+
'Fn::GetAtt': ['Cluster192CD0375', 'ClusterSecurityGroupId'],
26632634
},
26642635
],
26652636
SubnetIds: [
26662637
{
2667-
Ref: 'referencetoStackVpcPrivate1Subnet1Subnet6764A0F6Ref',
2638+
Ref: 'VpcPrivate1Subnet1SubnetC688B2B1',
26682639
},
26692640
{
2670-
Ref: 'referencetoStackVpcPrivate1Subnet2SubnetDFD49645Ref',
2641+
Ref: 'VpcPrivate1Subnet2SubnetA2AF15C7',
26712642
},
26722643
{
2673-
Ref: 'referencetoStackVpcPrivate2Subnet1Subnet586AD392Ref',
2644+
Ref: 'VpcPrivate2Subnet1SubnetE13E2E30',
26742645
},
26752646
{
2676-
Ref: 'referencetoStackVpcPrivate2Subnet2SubnetE42148C0Ref',
2647+
Ref: 'VpcPrivate2Subnet2Subnet158A38AB',
26772648
},
26782649
],
26792650
},
@@ -2734,8 +2705,8 @@ describe('cluster', () => {
27342705
expect(resources[expectedKubernetesGetId].Properties).toEqual({
27352706
ServiceToken: {
27362707
'Fn::GetAtt': [
2737-
'awscdkawseksKubectlProviderNestedStackawscdkawseksKubectlProviderNestedStackResourceA7AEBA6B',
2738-
'Outputs.StackawscdkawseksKubectlProviderframeworkonEvent8897FD9BArn',
2708+
'awscdkawseksKubectlProviderframeworkonEvent0A650005',
2709+
'Arn',
27392710
],
27402711
},
27412712
ClusterName: {
@@ -2773,10 +2744,9 @@ describe('cluster', () => {
27732744
});
27742745

27752746
// THEN
2776-
const providerStack = stack.node.tryFindChild('@aws-cdk/aws-eks.KubectlProvider') as cdk.NestedStack;
2777-
Template.fromStack(providerStack).hasResourceProperties('AWS::Lambda::Function', {
2747+
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
27782748
Layers: [
2779-
{ Ref: 'AwsCliLayerF44AAF94' },
2749+
{ Ref: 'awscdkawseksKubectlProviderAwsCliLayerF72FE066' },
27802750
'arn:of:layer',
27812751
],
27822752
});
@@ -2833,11 +2803,10 @@ describe('cluster', () => {
28332803
});
28342804

28352805
// THEN
2836-
const providerStack = stack.node.tryFindChild('@aws-cdk/aws-eks.KubectlProvider') as cdk.NestedStack;
2837-
Template.fromStack(providerStack).hasResourceProperties('AWS::Lambda::Function', {
2806+
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
28382807
Layers: [
28392808
'arn:of:layer',
2840-
{ Ref: 'KubectlLayer600207B5' },
2809+
{ Ref: 'awscdkawseksKubectlProviderKubectlLayerA7F2FE55' },
28412810
],
28422811
});
28432812
});
@@ -2870,42 +2839,6 @@ describe('cluster', () => {
28702839
});
28712840
});
28722841

2873-
test('custom memory size for kubectl provider', () => {
2874-
// GIVEN
2875-
const { stack, vpc, app } = testFixture();
2876-
2877-
// WHEN
2878-
new eks.Cluster(stack, 'Cluster', {
2879-
vpc,
2880-
version: CLUSTER_VERSION,
2881-
kubectlMemory: cdk.Size.gibibytes(2),
2882-
});
2883-
2884-
// THEN
2885-
const casm = app.synth();
2886-
const providerNestedStackTemplate = JSON.parse(fs.readFileSync(path.join(casm.directory, 'StackawscdkawseksKubectlProvider7346F799.nested.template.json'), 'utf-8'));
2887-
expect(providerNestedStackTemplate?.Resources?.Handler886CB40B?.Properties?.MemorySize).toEqual(2048);
2888-
});
2889-
2890-
test('custom memory size for imported clusters', () => {
2891-
// GIVEN
2892-
const { stack, app } = testFixture();
2893-
2894-
// WHEN
2895-
const cluster = eks.Cluster.fromClusterAttributes(stack, 'Imported', {
2896-
clusterName: 'my-cluster',
2897-
kubectlRoleArn: 'arn:aws:iam::123456789012:role/MyRole',
2898-
kubectlMemory: cdk.Size.gibibytes(4),
2899-
});
2900-
2901-
cluster.addManifest('foo', { bar: 123 });
2902-
2903-
// THEN
2904-
const casm = app.synth();
2905-
const providerNestedStackTemplate = JSON.parse(fs.readFileSync(path.join(casm.directory, 'StackStackImported1CBA9C50KubectlProviderAA00BA49.nested.template.json'), 'utf-8'));
2906-
expect(providerNestedStackTemplate?.Resources?.Handler886CB40B?.Properties?.MemorySize).toEqual(4096);
2907-
});
2908-
29092842
test('create a cluster using custom kubernetes network config', () => {
29102843
// GIVEN
29112844
const { stack } = testFixture();

packages/@aws-cdk/aws-eks-v2-alpha/test/k8s-object-value.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ describe('k8s object value', () => {
2929
Properties: {
3030
ServiceToken: {
3131
'Fn::GetAtt': [
32-
'awscdkawseksKubectlProviderNestedStackawscdkawseksKubectlProviderNestedStackResourceA7AEBA6B',
33-
'Outputs.awscdkawseksKubectlProviderframeworkonEvent0A650005Arn',
32+
'awscdkawseksKubectlProviderframeworkonEvent0A650005',
33+
'Arn',
3434
],
3535
},
3636
ClusterName: { Ref: 'MyCluster4C1BA579' },
@@ -70,8 +70,8 @@ describe('k8s object value', () => {
7070
Properties: {
7171
ServiceToken: {
7272
'Fn::GetAtt': [
73-
'awscdkawseksKubectlProviderNestedStackawscdkawseksKubectlProviderNestedStackResourceA7AEBA6B',
74-
'Outputs.awscdkawseksKubectlProviderframeworkonEvent0A650005Arn',
73+
'awscdkawseksKubectlProviderframeworkonEvent0A650005',
74+
'Arn',
7575
],
7676
},
7777
ClusterName: { Ref: 'MyCluster4C1BA579' },

packages/@aws-cdk/aws-eks-v2-alpha/test/k8s-patch.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ describe('k8s patch', () => {
2323
Template.fromStack(stack).hasResourceProperties('Custom::AWSCDK-EKS-KubernetesPatch', {
2424
ServiceToken: {
2525
'Fn::GetAtt': [
26-
'awscdkawseksKubectlProviderNestedStackawscdkawseksKubectlProviderNestedStackResourceA7AEBA6B',
27-
'Outputs.awscdkawseksKubectlProviderframeworkonEvent0A650005Arn',
26+
'awscdkawseksKubectlProviderframeworkonEvent0A650005',
27+
'Arn',
2828
],
2929
},
3030
ResourceName: 'myResourceName',

0 commit comments

Comments
 (0)