Skip to content

Commit 83c35a0

Browse files
committed
fix(eks): fixing the filtering by subnetIDs option
1 parent 154bff0 commit 83c35a0

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed

packages/aws-cdk-lib/aws-eks/test/cluster.test.ts

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2891,6 +2891,163 @@ describe('cluster', () => {
28912891
});
28922892
});
28932893

2894+
test('private endpoint access selects private subnets from looked up vpc for filtering by IDs with given context', () => {
2895+
const vpcId = 'vpc-12345';
2896+
// can't use the regular fixture because it also adds a VPC to the stack, which prevents
2897+
// us from setting context.
2898+
const stack = new cdk.Stack(new cdk.App(), 'Stack', {
2899+
env: {
2900+
account: '11112222',
2901+
region: 'us-east-1',
2902+
},
2903+
});
2904+
2905+
stack.node.setContext(`vpc-provider:account=${stack.account}:filter.vpc-id=${vpcId}:region=${stack.region}:returnAsymmetricSubnets=true`, {
2906+
vpcId: vpcId,
2907+
vpcCidrBlock: '10.0.0.0/16',
2908+
subnetGroups: [
2909+
{
2910+
name: 'Private',
2911+
type: 'Private',
2912+
subnets: [
2913+
{
2914+
subnetId: 'subnet-private-in-us-east-1a',
2915+
cidr: '10.0.1.0/24',
2916+
availabilityZone: 'us-east-1a',
2917+
routeTableId: 'rtb-06068e4c4049921ef',
2918+
},
2919+
],
2920+
},
2921+
{
2922+
name: 'Public',
2923+
type: 'Public',
2924+
subnets: [
2925+
{
2926+
subnetId: 'subnet-public-in-us-east-1c',
2927+
cidr: '10.0.0.0/24',
2928+
availabilityZone: 'us-east-1c',
2929+
routeTableId: 'rtb-0ff08e62195198dbb',
2930+
},
2931+
],
2932+
},
2933+
],
2934+
});
2935+
2936+
const vpc = ec2.Vpc.fromLookup(stack, 'Vpc', {
2937+
vpcId: vpcId,
2938+
});
2939+
2940+
new eks.Cluster(stack, 'Cluster', {
2941+
vpc,
2942+
version: CLUSTER_VERSION,
2943+
prune: false,
2944+
endpointAccess: eks.EndpointAccess.PRIVATE,
2945+
vpcSubnets: [{
2946+
subnetFilters: [
2947+
ec2.SubnetFilter.byIds(['subnet-private-in-us-east-1a']),
2948+
],
2949+
}],
2950+
kubectlLayer: new KubectlV31Layer(stack, 'KubectlLayer'),
2951+
});
2952+
2953+
const nested = stack.node.tryFindChild('@aws-cdk/aws-eks.KubectlProvider') as cdk.NestedStack;
2954+
Template.fromStack(nested).hasResourceProperties('AWS::Lambda::Function', {
2955+
VpcConfig: { SubnetIds: ['subnet-private-in-us-east-1a'] },
2956+
});
2957+
});
2958+
2959+
test('private endpoint access skips validation for private subnets from looked up vpc for filtering by IDs with no context', () => {
2960+
const vpcId = 'vpc-12345';
2961+
// can't use the regular fixture because it also adds a VPC to the stack, which prevents
2962+
// us from setting context.
2963+
const stack = new cdk.Stack(new cdk.App(), 'Stack', {
2964+
env: {
2965+
account: '11112222',
2966+
region: 'us-east-1',
2967+
},
2968+
});
2969+
2970+
const vpc = ec2.Vpc.fromLookup(stack, 'Vpc', {
2971+
vpcId: vpcId,
2972+
});
2973+
2974+
new eks.Cluster(stack, 'Cluster', {
2975+
vpc,
2976+
version: CLUSTER_VERSION,
2977+
prune: false,
2978+
endpointAccess: eks.EndpointAccess.PRIVATE,
2979+
vpcSubnets: [{
2980+
subnetFilters: [
2981+
ec2.SubnetFilter.byIds(['subnet-private-in-us-east-1a']),
2982+
],
2983+
}],
2984+
kubectlLayer: new KubectlV31Layer(stack, 'KubectlLayer'),
2985+
});
2986+
});
2987+
2988+
test('private endpoint access validates private subnets from looked up vpc for other select subnet options', () => {
2989+
const vpcId = 'vpc-12345';
2990+
// can't use the regular fixture because it also adds a VPC to the stack, which prevents
2991+
// us from setting context.
2992+
const stack = new cdk.Stack(new cdk.App(), 'Stack', {
2993+
env: {
2994+
account: '11112222',
2995+
region: 'us-east-1',
2996+
},
2997+
});
2998+
2999+
stack.node.setContext(`vpc-provider:account=${stack.account}:filter.vpc-id=${vpcId}:region=${stack.region}:returnAsymmetricSubnets=true`, {
3000+
vpcId: vpcId,
3001+
vpcCidrBlock: '10.0.0.0/16',
3002+
subnetGroups: [
3003+
{
3004+
name: 'Public',
3005+
type: 'Public',
3006+
subnets: [
3007+
{
3008+
subnetId: 'subnet-public-in-us-east-1c',
3009+
cidr: '10.0.0.0/24',
3010+
availabilityZone: 'us-east-1c',
3011+
routeTableId: 'rtb-0ff08e62195198dbb',
3012+
},
3013+
],
3014+
},
3015+
{
3016+
name: 'Private',
3017+
type: 'Private',
3018+
subnets: [
3019+
{
3020+
subnetId: 'subnet-private-in-us-east-1a',
3021+
cidr: '10.0.1.0/24',
3022+
availabilityZone: 'us-east-1a',
3023+
routeTableId: 'rtb-06068e4c4049921ef',
3024+
},
3025+
],
3026+
},
3027+
],
3028+
});
3029+
3030+
const vpc = ec2.Vpc.fromLookup(stack, 'Vpc', {
3031+
vpcId: vpcId,
3032+
});
3033+
3034+
new eks.Cluster(stack, 'Cluster', {
3035+
vpc,
3036+
version: CLUSTER_VERSION,
3037+
prune: false,
3038+
endpointAccess: eks.EndpointAccess.PRIVATE,
3039+
vpcSubnets: [{
3040+
subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS,
3041+
}],
3042+
kubectlLayer: new KubectlV31Layer(stack, 'KubectlLayer'),
3043+
});
3044+
3045+
const nested = stack.node.tryFindChild('@aws-cdk/aws-eks.KubectlProvider') as cdk.NestedStack;
3046+
Template.fromStack(nested).hasResourceProperties('AWS::Lambda::Function', {
3047+
VpcConfig: { SubnetIds: ['subnet-private-in-us-east-1a'] },
3048+
});
3049+
});
3050+
28943051
test('private endpoint access selects only private subnets from managed vpc with concrete subnet selection', () => {
28953052
const { stack } = testFixture();
28963053

0 commit comments

Comments
 (0)