Skip to content

Commit

Permalink
feat: azure kubernetes integration
Browse files Browse the repository at this point in the history
  • Loading branch information
MitkoTschimev committed Jun 17, 2020
1 parent 2cb1e47 commit c63fc45
Show file tree
Hide file tree
Showing 28 changed files with 131 additions and 16 deletions.
6 changes: 6 additions & 0 deletions libs/nx-deploy-it/src/adapter/nestjs/nestjs.adapter.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { NxDeployItBaseOptions } from '../base.adapter.model';
import { DeploymentType } from '../../utils/application-type';

export interface NestJSOptions extends NxDeployItBaseOptions {
deploymentType: DeploymentType;
}
57 changes: 41 additions & 16 deletions libs/nx-deploy-it/src/adapter/nestjs/nestjs.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ import { resolve } from 'path';
import * as ncc from '@zeit/ncc';
import { ensureDirSync, ensureFileSync } from 'fs-extra';
import { writeFileSync } from 'fs';
import { NestJSOptions } from './nestjs.adapter.model';
import { DeploymentType } from '../../utils/application-type';

export class NestJSAdapter extends BaseAdapter {
options: NestJSOptions;

async extendOptionsByUserInput() {
await super.extendOptionsByUserInput();
const options = this.options as NxDeployItInitSchematicSchema;
Expand All @@ -27,7 +31,12 @@ export class NestJSAdapter extends BaseAdapter {
questions.push(QUESTIONS.gcpRegionCloudFunctions);
}

const anwsers = await prompt(questions);
questions.push(QUESTIONS.deploymentType);

const anwsers = await prompt<{
deploymentType: DeploymentType;
'gcp:region': string;
}>(questions);
this.options = {
...options,
...anwsers
Expand All @@ -37,23 +46,37 @@ export class NestJSAdapter extends BaseAdapter {
addRequiredDependencies() {
const dependencies = super.addRequiredDependencies();

if (this.options.provider === PROVIDER.AZURE) {
dependencies.push(
{
name: '@nestjs/azure-func-http',
version: '^0.4.2'
},
{
name: '@azure/functions',
version: '^1.2.0'
}
);
if (this.options.deploymentType === DeploymentType.SERVERLESS) {
if (this.options.provider === PROVIDER.AZURE) {
dependencies.push(
{
name: '@nestjs/azure-func-http',
version: '^0.4.2'
},
{
name: '@azure/functions',
version: '^1.2.0'
}
);
}
if (this.options.provider === PROVIDER.AWS) {
dependencies.push({
name: 'aws-serverless-express',
version: '^3.3.6'
});
}
}
if (this.options.provider === PROVIDER.AWS) {
if (this.options.deploymentType === DeploymentType.KUBERNETES) {
dependencies.push({
name: 'aws-serverless-express',
version: '^3.3.6'
name: '@pulumi/kubernetes',
version: '^2.2.0'
});
if (this.options.provider === PROVIDER.AWS) {
dependencies.push({
name: '@pulumi/eks',
version: '^0.19.0'
});
}
}
return dependencies;
}
Expand All @@ -70,7 +93,9 @@ export class NestJSAdapter extends BaseAdapter {
}

getApplicationTemplatePath() {
return `${super.getApplicationTemplatePath()}/nestjs/`;
return `${super.getApplicationTemplatePath()}/nestjs-${
this.options.deploymentType
}/`;
}

getDeployActionConfiguration(): any {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Logger } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';

import { <%= getRootModuleName() %> } from './<%= getRootModulePath() %>';

export async function bootstrap() {
const app = await NestFactory.create(<%= getRootModuleName() %>);
const port = process.env.PORT || 3333;
await app.listen(port, () => {
Logger.log('Listening at http://localhost:' + port);
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
buildcache
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM node:10-alpine
WORKDIR /usr/src/app

COPY functions/dist/main .

EXPOSE 3000
CMD ["node", "index.js"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { bootstrap } from '../../../<%= getRootDirectory() %>/main.aws';

bootstrap();
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as aws from '@pulumi/aws';
import * as awsx from '@pulumi/awsx';
import * as eks from '@pulumi/eks';
import * as pulumi from '@pulumi/pulumi';

const stackConfig = new pulumi.Config();
const config = {
// ===== DONT'T TOUCH THIS -> CONFIG REQUIRED BY nx-deploy-it ======
projectName: stackConfig.get('projectName')
// ===== END ======
};
const projectName = config.projectName;
const stageName = pulumi.getStack().split('-')[0];
const region = aws.config.requireRegion();

// Create a VPC for our cluster.
const vpc = new awsx.ec2.Vpc(`${projectName}-vpc`, {
numberOfAvailabilityZones: 2
});

// Create the EKS cluster itself and a deployment of the Kubernetes dashboard.
const cluster = new eks.Cluster(`${projectName}-cluster`, {
vpcId: vpc.id,
subnetIds: vpc.publicSubnetIds,
instanceType: 't3.micro',
desiredCapacity: 1,
minSize: 1,
maxSize: 1,
deployDashboard: true
});

// Export the cluster's kubeconfig.
export const kubeconfig = cluster.kubeconfig;

// exports.endpoint = pulumi.interpolate`${deployment.invokeUrl}${stageName}`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../tsconfig.app.json",
"compilerOptions": {
"target": "es2015",
"moduleResolution": "node",
"module": "commonjs"
}
}
5 changes: 5 additions & 0 deletions libs/nx-deploy-it/src/utils/application-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export enum ApplicationType {
ANGULAR_UNIVERSAL = 'angular-universal'
}

export enum DeploymentType {
SERVERLESS = 'serverless',
KUBERNETES = 'kubernetes'
}

function getTarget(
targets: TargetDefinitionCollection | {},
targetName: string
Expand Down
13 changes: 13 additions & 0 deletions libs/nx-deploy-it/src/utils/questions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { PROVIDER } from './provider';
import { ANGULAR_UNIVERSAL_DEPLOYMENT_TYPE } from '../adapter/angular-universal/deployment-type.enum';
import { DeploymentType } from './application-type';

export const QUESTIONS = {
// get list with: aws ec2 describe-regions --profile cli-dev-thought | jq ".Regions[] | .RegionName"
Expand Down Expand Up @@ -158,5 +159,17 @@ export const QUESTIONS = {
result: function(r: string) {
return Object.values(this.map(r))[0];
}
},

deploymentType: {
type: 'select',
name: 'deploymentType',
choices: [
{ name: 'Serverless', value: DeploymentType.SERVERLESS },
{ name: 'Kubernetes', value: DeploymentType.KUBERNETES }
],
result: function(r: string) {
return Object.values(this.map(r))[0];
}
}
};

0 comments on commit c63fc45

Please sign in to comment.