Skip to content
This repository has been archived by the owner on May 17, 2023. It is now read-only.

feat: Support instance size selector and provisioning new VPC when deploy #14

Merged
merged 4 commits into from
Sep 2, 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
10 changes: 5 additions & 5 deletions bin/sonatype-nexus3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { SonatypeNexus3Stack } from '../lib/sonatype-nexus3-stack';

const app = new cdk.App();
new SonatypeNexus3Stack(app, 'SonatypeNexus3OnEKS', {
env: {
region: process.env.CDK_DEFAULT_REGION,
account: process.env.CDK_DEFAULT_ACCOUNT,
}
env: {
region: process.env.CDK_DEFAULT_REGION,
account: process.env.CDK_DEFAULT_ACCOUNT,
},
});

cdk.Tag.add(app, 'app', 'nexus3');
cdk.Tag.add(app, 'app', 'nexus3');
21 changes: 15 additions & 6 deletions lib/sonatype-nexus3-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,22 @@ export class SonatypeNexus3Stack extends cdk.Stack {
validation: certmgr.CertificateValidation.fromDns(hostedZone),
});
}

const vpc = ec2.Vpc.fromLookup(this, 'vpc', {
isDefault: true
});
if (this.azOfSubnets(vpc.publicSubnets) <= 1 ||
let vpc!: ec2.IVpc;
let createNewVpc:boolean = this.node.tryGetContext('createNewVpc') ?? false
if (createNewVpc){
vpc = new ec2.Vpc(this,'newVpcForEKS',{
maxAzs: 2,
natGateways:1,
} )
}
else{
vpc = ec2.Vpc.fromLookup(this, 'vpc', {
isDefault: true
});
if (this.azOfSubnets(vpc.publicSubnets) <= 1 ||
this.azOfSubnets(vpc.privateSubnets) <= 1) {
throw new Error(`VPC '${vpc.vpcId}' must have both public and private subnets cross two AZs at least.`);
}
}

const clusterAdmin = new iam.Role(this, 'AdminRole', {
Expand Down Expand Up @@ -115,7 +124,7 @@ export class SonatypeNexus3Stack extends cdk.Stack {

cluster.addNodegroup('nodegroup', {
nodegroupName: 'nexus3',
instanceType: new ec2.InstanceType('m5.large'),
instanceType: new ec2.InstanceType( this.node.tryGetContext('instanceType') ?? 'm5.large'),
minSize: 1,
maxSize: 3,
// Have to bind IAM role to node due to Nexus3 uses old AWS Java SDK not supporting IRSA
Expand Down
46 changes: 46 additions & 0 deletions test/sonatype-nexus3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,49 @@ function overrideStackWithContextDomainName(app: cdk.App, stack: cdk.Stack,
});
return { app, stack };
}

describe('Nexus OSS stack and instanceType newVpc', () => {
let app: cdk.App;
let stack: cdk.Stack;
let previous: (scope: cdk.Construct, options: cdk.GetContextValueOptions) => cdk.GetContextValueResult;

afterAll(() => {
mock.restoreContextProvider(previous);
});

beforeEach(() => {
({ app, stack } = overrideStackWithContextNewVpcInstanceType(app, stack, 'example.com','m5.xlarge', true));
});

test('Nexus Stack have new vpc and custom instanceType', () => {
expect(stack).toHaveResource(`AWS::EC2::VPC`,{
CidrBlock: "10.0.0.0/16",
});
expect(stack).toHaveResource(`AWS::EKS::Nodegroup`,{
InstanceTypes: ["m5.xlarge"]
});
});

});

function overrideStackWithContextNewVpcInstanceType(app: cdk.App, stack: cdk.Stack,
domainName: string | undefined, instanceType?: string | undefined, createNewVpc?: boolean | undefined, domainZone?: string ) {
app = new cdk.App({
context: {
domainName: domainName,
domainZone,
instanceType,
createNewVpc,
}
});

const env = {
region: 'ap-northeast-1',
account: '1234567890xx',
}

stack = new SonatypeNexus3.SonatypeNexus3Stack(app, 'NexusStack', {
env,
});
return { app, stack };
}