Skip to content

Commit

Permalink
Merge pull request #184 from boostcampwm-2024/feature/convertTerraform
Browse files Browse the repository at this point in the history
Feature/convert terraform
  • Loading branch information
p1n9d3v authored Dec 4, 2024
2 parents edbf44d + 5200a74 commit 4cb96b9
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 12 deletions.
1 change: 1 addition & 0 deletions packages/terraform/enum/ResourcePriority.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export enum ResourcePriority {
OBJECT_STORAGE_BUCKET = 13,
REDIS = 14,
REDIS_CONFIG_GROUP = 15,
NKS_CLUSTER = 16,
}
13 changes: 13 additions & 0 deletions packages/terraform/interface/KsCluster.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export interface KsCluster {
hypervisorType?: string;
clusterType: string;
k8sVersion?: string;
loginKeyName: string;
lbPrivateSubnetNo?: string;
lbPublicSubnetNo?: string;
kubeNetworkPlugin?: string;
subnetNoList: string[];
vpcNo: string;
publicNetwork?: boolean;
zone: string;
}
82 changes: 82 additions & 0 deletions packages/terraform/model/NCloudNKsCluster.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { KsCluster } from '../interface/KsCluster';
import { NCloudModel } from '../interface/NCloudModel';
import { ResourcePriority } from '../enum/ResourcePriority';

export class NCloudNKsCluster implements KsCluster, NCloudModel {
name?: string;
serviceType: string;
priority: ResourcePriority;
hypervisorCode?: string;
clusterType: string;
k8sVersion: string;
loginKeyName: string;
lbPublicSubnetNo?: string | undefined;
lbPrivateSubnetNo?: string | undefined;
kubeNetworkPlugin?: string | undefined;
subnetNoList: string[];
vpcNo: string;
publicNetwork?: boolean | undefined;
zone: string;

constructor(json: any) {
this.serviceType = 'ncloud_nks_cluster';
this.priority = ResourcePriority.NKS_CLUSTER;
this.name = json.name;
if (json.hypervisorCode) {
this.hypervisorCode = json.hypervisorCode;
}
this.clusterType = json.clusterType;
this.k8sVersion = json.k8sVersion;
this.loginKeyName = `ncloud_login_key.${json.loginKeyName.toLowerCase()}.key_name`;
if (json.lbPublicSubnetNo) {
this.lbPublicSubnetNo = json.lbPublicSubnetNo;
}
if (json.lbPrivateSubnetNo) {
this.lbPrivateSubnetNo = json.lbPrivateSubnetNo;
}
if (json.kubeNetworkPlugin) {
this.kubeNetworkPlugin = json.kubeNetworkPlugin;
}
if (Array.isArray(json.subnetNoList)) {
this.subnetNoList = json.subnetNoList.map(
(name: string) => `ncloud_subnet.${name.toLowerCase()}.id`,
);
} else {
this.subnetNoList = [
`ncloud_subnet.${json.subnetNoList.toLowerCase()}.id`,
];
}
this.vpcNo = `ncloud_vpc.${json.vpcNo.toLowerCase()}.id`;
if (json.publicNetwork) {
this.publicNetwork = json.publicNetwork;
}
this.zone = json.zone;
}
getProperties() {
const properties: { [key: string]: any } = {
name: this.name,
cluster_type: this.clusterType,
k8s_version: this.k8sVersion,
login_key_name: this.loginKeyName,
subnet_no_list: this.subnetNoList,
vpc_no: this.vpcNo,
zone: this.zone,
};
if (this.hypervisorCode) {
properties.hypervisor_code = this.hypervisorCode;
}
if (this.lbPublicSubnetNo) {
properties.lb_public_subnet_no = this.lbPublicSubnetNo;
}
if (this.lbPrivateSubnetNo) {
properties.lb_private_subnet_no = this.lbPrivateSubnetNo;
}
if (this.kubeNetworkPlugin) {
properties.kube_network_plugin = this.kubeNetworkPlugin;
}
if (this.publicNetwork) {
properties.public_network = this.publicNetwork;
}
return properties;
}
}
15 changes: 10 additions & 5 deletions packages/terraform/type/ResourceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@ import { NCloudModel } from '../interface/NCloudModel';

export class ResourceManager {
private resources: Array<{ resource: NCloudModel; region?: string }>;
private resourceMap: Map<
string | undefined,
{ resource: NCloudModel; region?: string }
>;
private readonly nameMap: Map<string, string>;
private readonly regionMap: Map<string, Set<string>>;

constructor() {
this.resources = [];
this.nameMap = new Map();
this.regionMap = new Map();
this.resourceMap = new Map();
}

addResource(resource: NCloudModel, region?: string): void {
this.resources.push({ resource, region });

if (!this.resourceMap.has(resource.name)) {
this.resources.push({ resource, region });
}
if (resource.name) {
this.nameMap.set(resource.serviceType, resource.name);
}
Expand All @@ -24,12 +30,11 @@ export class ResourceManager {
}
this.regionMap.get(resource.serviceType)?.add(region);
}
this.resourceMap.set(resource.name, { resource, region });
}

getResources(): Array<{ resource: NCloudModel; region?: string }> {
return [...this.resources].sort(
(a, b) => a.resource.priority - b.resource.priority,
);
return [...this.resources];
}

getNameMap(): Map<string, string> {
Expand Down
14 changes: 7 additions & 7 deletions packages/terraform/util/dependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { CloudCanvasNode } from '../interface/CloudCanvasNode';
export const createVpcDependency = (properties: any): CloudCanvasNode => ({
id: `vpc-${properties.vpc}`,
type: 'VPC',
name: properties.vpc,
properties: {
name: properties.vpc,
cidrBlock: properties.vpcCidr || '172.16.0.0/16',
region: properties.region,
},
Expand Down Expand Up @@ -35,8 +35,8 @@ export const createSubnetDependency = (
{
id: `subnet-${properties.subnet}`,
type: 'Subnet',
name: properties.subnet,
properties: {
name: properties.subnet,
subnet: properties.subnetCidr || '172.16.10.0/24',
zone: properties.zone || getZoneByRegion(properties.region),
subnetType: properties.subnetType || 'PUBLIC',
Expand All @@ -57,8 +57,8 @@ export const createAcgDependencies = (
{
id: `acg-${properties.acg}`,
type: 'ACG',
name: properties.acg,
properties: {
name: properties.acg,
description: `Security group for ${nodeName}`,
vpcName: properties.vpc,
region: properties.region,
Expand All @@ -67,8 +67,8 @@ export const createAcgDependencies = (
{
id: `acgrule-${properties.acg}`,
type: 'ACGRule',
name: `${properties.acg}-rule`,
properties: {
name: `${properties.acg}-rule`,
acgName: properties.acg,
protocol: 'TCP',
ip_block: '0.0.0.0/0',
Expand All @@ -82,8 +82,8 @@ export const createAcgDependencies = (
export const createNicDependency = (properties: any): CloudCanvasNode => ({
id: `nic-${properties.nic}`,
type: 'NetworkInterface',
name: properties.nic,
properties: {
name: properties.nic,
subnetName: properties.subnet,
acgName: properties.acg,
region: properties.region,
Expand All @@ -93,17 +93,17 @@ export const createNicDependency = (properties: any): CloudCanvasNode => ({
export const createLoginKeyDependency = (properties: any): CloudCanvasNode => ({
id: `loginkey-${properties.loginKey}`,
type: 'LoginKey',
name: properties.loginKey,
properties: {
name: properties.loginKey,
region: properties.region,
},
});

export const createRedisConfigGroup = (properties: any): CloudCanvasNode => ({
id: `redisconfig-${properties.configGroup}`,
type: 'RedisConfigGroup',
name: properties.configGroup,
properties: {
name: properties.configGroup,
redisVersion: properties.redisVersion,
region: properties.region,
},
Expand Down
1 change: 1 addition & 0 deletions packages/terraform/util/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {

export const processDependencies = (node: any): any[] => {
if (

!['server', 'loadbalancer', 'db-mysql', 'redis'].includes(
node.type.toLowerCase(),
)
Expand Down
17 changes: 17 additions & 0 deletions packages/terraform/util/resourceParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { NCloudLaunchConfiguration } from '../model/NCloudLaunchConfiguration';
import { NCloudMySQL } from '../model/NCloudMySQL';
import { NCloudObjectStorageBucket } from '../model/NCloudObjectStorageBucket';
import { NCloudRedis } from '../model/NCloudRedis';
import { NCloudNKsCluster } from '../model/NCloudNKsCluster';

export function parseToNCloudModel(resource: any): NCloudModel {
const { type, properties } = resource;
Expand Down Expand Up @@ -140,6 +141,22 @@ export function parseToNCloudModel(resource: any): NCloudModel {
configGroupNo: properties.configGroup,
mode: properties.mode,
});

case 'nkscluster':
return new NCloudNKsCluster({
name: name,
hypervisorCode: properties.hypervisorCode,
clusterType: properties.clusterType,
k8sVersion: properties.k8sVersion,
loginKeyName: properties.loginKeyName,
lbPrivateSubnetNo: properties.lbPrivateSubnet,
lbPublicSubnetNo: properties.lbPublicSubnet,
subnetNoList: properties.subnet,
vpcNo: properties.vpc,
subnetNo: properties.subnet,
publicNetwork: properties.publicNetwork,
zone: properties.zone,
});
default:
throw new Error(`Unsupported resource type: ${type}`);
}
Expand Down

0 comments on commit 4cb96b9

Please sign in to comment.