Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eks): cluster creation fails when configured with an imported public subnet and private endpoint #11620

Merged
merged 4 commits into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/@aws-cdk/aws-eks/lib/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1352,18 +1352,20 @@ export class Cluster extends ClusterBase {

private selectPrivateSubnets(): ec2.ISubnet[] {
const privateSubnets: ec2.ISubnet[] = [];
const vpcPrivateSubnetIds = this.vpc.privateSubnets.map(s => s.subnetId);
const vpcPublicSubnetIds = this.vpc.publicSubnets.map(s => s.subnetId);

for (const placement of this.vpcSubnets) {

for (const subnet of this.vpc.selectSubnets(placement).subnets) {

if (this.vpc.privateSubnets.includes(subnet)) {
if (vpcPrivateSubnetIds.includes(subnet.subnetId)) {
// definitely private, take it.
privateSubnets.push(subnet);
continue;
}

if (this.vpc.publicSubnets.includes(subnet)) {
if (vpcPublicSubnetIds.includes(subnet.subnetId)) {
// definitely public, skip it.
continue;
}
Expand Down
97 changes: 97 additions & 0 deletions packages/@aws-cdk/aws-eks/test/test.cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2106,6 +2106,103 @@ export = {
test.done();
},

'private endpoint access selects only private subnets from looked up vpc with concrete subnet selection'(test: Test) {

const vpcId = 'vpc-12345';
// can't use the regular fixture because it also adds a VPC to the stack, which prevents
// us from setting context.
const stack = new cdk.Stack(new cdk.App(), 'Stack', {
env: {
account: '11112222',
region: 'us-east-1',
},
});
stack.node.setContext(`vpc-provider:account=${stack.account}:filter.vpc-id=${vpcId}:region=${stack.region}:returnAsymmetricSubnets=true`, {
vpcId: vpcId,
vpcCidrBlock: '10.0.0.0/16',
subnetGroups: [
{
name: 'Private',
type: 'Private',
subnets: [
{
subnetId: 'subnet-private-in-us-east-1a',
cidr: '10.0.1.0/24',
availabilityZone: 'us-east-1a',
routeTableId: 'rtb-06068e4c4049921ef',
},
],
},
{
name: 'Public',
type: 'Public',
subnets: [
{
subnetId: 'subnet-public-in-us-east-1c',
cidr: '10.0.0.0/24',
availabilityZone: 'us-east-1c',
routeTableId: 'rtb-0ff08e62195198dbb',
},
],
},
],
});
const vpc = ec2.Vpc.fromLookup(stack, 'Vpc', {
vpcId: vpcId,
});

new eks.Cluster(stack, 'Cluster', {
vpc,
version: CLUSTER_VERSION,
endpointAccess: eks.EndpointAccess.PRIVATE,
vpcSubnets: [{
subnets: [
ec2.Subnet.fromSubnetId(stack, 'Private', 'subnet-private-in-us-east-1a'),
ec2.Subnet.fromSubnetId(stack, 'Public', 'subnet-public-in-us-east-1c'),
],
}],
});

const nested = stack.node.tryFindChild('@aws-cdk/aws-eks.KubectlProvider') as cdk.NestedStack;
const template = expect(nested).value;

test.deepEqual(template.Resources.Handler886CB40B.Properties.VpcConfig.SubnetIds, [
'subnet-private-in-us-east-1a',
]);

test.done();
},

'private endpoint access selects only private subnets from managed vpc with concrete subnet selection'(test: Test) {

const { stack } = testFixture();

const vpc = new ec2.Vpc(stack, 'Vpc');

new eks.Cluster(stack, 'Cluster', {
vpc,
version: CLUSTER_VERSION,
endpointAccess: eks.EndpointAccess.PRIVATE,
vpcSubnets: [{
subnets: [
vpc.privateSubnets[0],
vpc.publicSubnets[1],
ec2.Subnet.fromSubnetId(stack, 'Private', 'subnet-unknown'),
],
}],
});

const nested = stack.node.tryFindChild('@aws-cdk/aws-eks.KubectlProvider') as cdk.NestedStack;
const template = expect(nested).value;

test.deepEqual(template.Resources.Handler886CB40B.Properties.VpcConfig.SubnetIds, [
{ Ref: 'referencetoStackVpcPrivateSubnet1Subnet8E6A14CBRef' },
'subnet-unknown',
]);

test.done();
},

'private endpoint access considers specific subnet selection'(test: Test) {
const { stack } = testFixture();
new eks.Cluster(stack, 'Cluster', {
Expand Down