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

New Update on cdk_framework design #10

Open
wants to merge 13 commits into
base: dev/cdk
Choose a base branch
from
7 changes: 1 addition & 6 deletions cdk_framework/EKS/lib/app.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
#!/usr/bin/env node
import 'source-map-support/register';
import * as cdk from 'aws-cdk-lib';
import { ClusterStack } from './stacks/cluster-stack';
import { deployClusters } from './cluster-deployment';

const app = new cdk.App();

let clusterMap = new Map<string, ClusterStack>()

clusterMap = deployClusters(app);


const clusterMap = deployClusters(app);
57 changes: 37 additions & 20 deletions cdk_framework/EKS/lib/cluster-deployment.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
#!/usr/bin/env node
import 'source-map-support/register';
import * as cdk from 'aws-cdk-lib';
import { validateClustersConfig } from './utils/validate-cluster-config';
import { VPCStack } from './stacks/vpc-stack';
import { aws_eks as eks} from 'aws-cdk-lib';
import { ClusterStack } from './stacks/cluster-stack';
import { readFileSync} from 'fs';
import { EC2Stack } from './stacks/ec2-cluster-stack';
import { FargateStack } from './stacks/fargate-cluster-stack';
import { validateFileSchema } from './utils/validate-config-schema';
import { ClusterInterface } from './interfaces/cluster-interface';
import { ec2ClusterInterface } from './interfaces/ec2cluster-interface';
const yaml = require('js-yaml')


export function deployClusters(app: cdk.App) : Map<string, ClusterStack> {
export function deployClusters(app: cdk.App) : Map<string, FargateStack | EC2Stack> {
const REGION = process.env.REGION || 'us-west-2'

const route = process.env.CDK_CONFIG_PATH || __dirname + '/config/cluster-config/clusters.yml';
Expand All @@ -21,30 +24,44 @@ export function deployClusters(app: cdk.App) : Map<string, ClusterStack> {
const raw = readFileSync(route)
const configData = yaml.load(raw)

const eksClusterMap = new Map<string, ClusterStack>();
validateFileSchema(configData)

const eksClusterMap = new Map<string, FargateStack | EC2Stack>();

const vpcStack = new VPCStack(app, 'EKSVpc', {
env: {
region: REGION
}
})

validateClustersConfig(configData)
for(const [key, value] of Object.entries(configData['clusters'])){
const val = Object(value)
const versionKubernetes = eks.KubernetesVersion.of(String(val['version']));
const newStack = new ClusterStack(app, key + 'EKSCluster', {
launchType: (val['launch_type']),
name: key,
vpc: vpcStack.vpc,
version: versionKubernetes,
env: {
region: REGION
},
})


eksClusterMap.set(key, newStack)
// schemaValidator(configData)
for(const cluster of configData['clusters']){
let stack;
const clusterInterface = cluster as ClusterInterface
const versionKubernetes = eks.KubernetesVersion.of(clusterInterface.version);
if(clusterInterface.launch_type === 'ec2'){
const ec2Cluster = cluster as ec2ClusterInterface
stack = new EC2Stack(app, ec2Cluster.name + 'EKSCluster', {
name: ec2Cluster.name,
vpc: vpcStack.vpc,
version: versionKubernetes,
ec2_instance: ec2Cluster.ec2_instance,
node_size: ec2Cluster.node_size,
env: {
region: REGION
},
})
} else {
stack = new FargateStack(app, clusterInterface.name + 'EKSCluster', {
name: clusterInterface.name,
vpc: vpcStack.vpc,
version: versionKubernetes,
env: {
region: REGION
},
})
}
eksClusterMap.set(cluster['name'], stack)
}

return eksClusterMap
Expand Down
22 changes: 8 additions & 14 deletions cdk_framework/EKS/lib/config/cluster-config/clusters.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
---
clusters:
amdCluster02:
launch_type:
fargate:
version: 1.21
fargateCluster01:
launch_type:
fargate:
version: 1.21
t4gCluster01:
launch_type:
ec2:
ec2_instance: m5
node_size: large
version: 1.21
- name: ec2Cluster
version: "1.21"
launch_type: ec2
ec2_instance: m5
node_size: large
Danielzolty marked this conversation as resolved.
Show resolved Hide resolved
- name: fargateCluster
version: "1.21"
launch_type: fargate
5 changes: 5 additions & 0 deletions cdk_framework/EKS/lib/interfaces/cluster-interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface ClusterInterface{
name: string
launch_type: string
version: string
}
6 changes: 6 additions & 0 deletions cdk_framework/EKS/lib/interfaces/ec2cluster-interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {ClusterInterface} from "./cluster-interface"

export interface ec2ClusterInterface extends ClusterInterface{
ec2_instance: string
node_size: string
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import { Vpc } from 'aws-cdk-lib/aws-ec2';
import { KubernetesVersion} from 'aws-cdk-lib/aws-eks';
import { ManagedPolicy, Role, ServicePrincipal } from 'aws-cdk-lib/aws-iam';

// not used right now but will be needed for when validation is done
const supportedNodeSizes = new Set(['medium', 'large', 'xlarge', '2xlarge', '4xlarge', '8xlarge', '12xlarge', '16xlarge', '24xlarge', 'metal']);
const supportedT4gInstances = new Set(['nano', 'micro', 'small', 'medium', 'large', 'xlarge', '2xlarge'])

export class ClusterStack extends Stack {
cluster : eks.Cluster | eks.FargateCluster

constructor(scope: Construct, id: string, props: ClusterStackProps) {
export class EC2Stack extends Stack {
cluster : eks.Cluster

constructor(scope: Construct, id: string, props: EC2ClusterStackProps) {
super(scope, id, props);

const logging = [
Expand All @@ -19,6 +23,9 @@ export class ClusterStack extends Stack {
eks.ClusterLoggingTypes.SCHEDULER,
]

const ec2Instance = props.ec2_instance
const nodeSize = props.node_size

const workerRole = new Role(this, 'EKSWorkerRole', {
assumedBy: new ServicePrincipal('ec2.amazonaws.com'),
managedPolicies: [
Expand All @@ -32,40 +39,30 @@ export class ClusterStack extends Stack {
ManagedPolicy.fromAwsManagedPolicyName('AmazonEKS_CNI_Policy')
]
});
if(props.launchType['ec2'] !== undefined){
this.cluster = new eks.Cluster(this, props.name, {
clusterName: props.name,
vpc: props.vpc,
vpcSubnets: [{subnetType: ec2.SubnetType.PUBLIC}],
defaultCapacity: 0, // we want to manage capacity our selves
version: props.version,
clusterLogging: logging,

});
const instanceType = props.launchType['ec2']['ec2_instance']
const instanceSize = props.launchType['ec2']['node_size']
this.cluster.addNodegroupCapacity('ng-' + instanceType, {
instanceTypes: [new ec2.InstanceType(instanceType + '.' + instanceSize)],
minSize: 2,
nodeRole: workerRole
})
} else if (props.launchType['fargate'] !== undefined){
this.cluster = new eks.FargateCluster(this, props.name, {
clusterName: props.name,
vpc: props.vpc,
version: props.version,
clusterLogging: logging
});
}
this.cluster = new eks.Cluster(this, props.name, {
clusterName: props.name,
vpc: props.vpc,
vpcSubnets: [{subnetType: ec2.SubnetType.PUBLIC}],
defaultCapacity: 0, // we want to manage capacity our selves
version: props.version,
clusterLogging: logging,

});
this.cluster.addNodegroupCapacity('ng-' + ec2Instance, {
Danielzolty marked this conversation as resolved.
Show resolved Hide resolved
instanceTypes: [new ec2.InstanceType(ec2Instance + '.' + nodeSize)],
Danielzolty marked this conversation as resolved.
Show resolved Hide resolved
minSize: 2,
nodeRole: workerRole
})
this.cluster.awsAuth.addMastersRole(Role.fromRoleName(this, 'eks_admin_role', 'Admin'))
this.cluster.awsAuth.addMastersRole(Role.fromRoleName(this, 'he', 'Admin'))

}
}

export interface ClusterStackProps extends StackProps{
launchType: any;
export interface EC2ClusterStackProps extends StackProps{
name: string;
vpc: Vpc;
version: KubernetesVersion;
ec2_instance: string;
node_size: string
}
38 changes: 38 additions & 0 deletions cdk_framework/EKS/lib/stacks/fargate-cluster-stack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Stack, StackProps, aws_eks as eks} from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { Vpc } from 'aws-cdk-lib/aws-ec2';
import { KubernetesVersion} from 'aws-cdk-lib/aws-eks';
import { ManagedPolicy, Role, ServicePrincipal } from 'aws-cdk-lib/aws-iam';


export class FargateStack extends Stack {
cluster : eks.FargateCluster

constructor(scope: Construct, id: string, props: FargateClusterStackProps) {
super(scope, id, props);

const logging = [
eks.ClusterLoggingTypes.API,
eks.ClusterLoggingTypes.AUDIT,
eks.ClusterLoggingTypes.AUTHENTICATOR,
eks.ClusterLoggingTypes.CONTROLLER_MANAGER,
eks.ClusterLoggingTypes.SCHEDULER,
]


this.cluster = new eks.FargateCluster(this, props.name, {
clusterName: props.name,
vpc: props.vpc,
version: props.version,
clusterLogging: logging
});
this.cluster.awsAuth.addMastersRole(Role.fromRoleName(this, 'eks_admin_role', 'Admin'))

}
}

export interface FargateClusterStackProps extends StackProps{
name: string;
vpc: Vpc;
version: KubernetesVersion;
}
Loading