diff --git a/packages/terraform/enum/ResourcePriority.ts b/packages/terraform/enum/ResourcePriority.ts index 220fdea4..7143b50e 100644 --- a/packages/terraform/enum/ResourcePriority.ts +++ b/packages/terraform/enum/ResourcePriority.ts @@ -14,4 +14,5 @@ export enum ResourcePriority { OBJECT_STORAGE_BUCKET = 13, REDIS = 14, REDIS_CONFIG_GROUP = 15, + NKS_CLUSTER = 16, } diff --git a/packages/terraform/interface/KsCluster.ts b/packages/terraform/interface/KsCluster.ts new file mode 100644 index 00000000..3b797455 --- /dev/null +++ b/packages/terraform/interface/KsCluster.ts @@ -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; +} diff --git a/packages/terraform/model/NCloudNKsCluster.ts b/packages/terraform/model/NCloudNKsCluster.ts new file mode 100644 index 00000000..f7d4369e --- /dev/null +++ b/packages/terraform/model/NCloudNKsCluster.ts @@ -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; + } +} diff --git a/packages/terraform/type/ResourceManager.ts b/packages/terraform/type/ResourceManager.ts index 3877e4fc..194d954d 100644 --- a/packages/terraform/type/ResourceManager.ts +++ b/packages/terraform/type/ResourceManager.ts @@ -2,6 +2,10 @@ 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; private readonly regionMap: Map>; @@ -9,11 +13,13 @@ export class ResourceManager { 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); } @@ -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 { diff --git a/packages/terraform/util/dependency.ts b/packages/terraform/util/dependency.ts index 793160b2..04be4087 100644 --- a/packages/terraform/util/dependency.ts +++ b/packages/terraform/util/dependency.ts @@ -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, }, @@ -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', @@ -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, @@ -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', @@ -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, @@ -93,8 +93,8 @@ 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, }, }); @@ -102,8 +102,8 @@ export const createLoginKeyDependency = (properties: any): CloudCanvasNode => ({ 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, }, diff --git a/packages/terraform/util/resource.ts b/packages/terraform/util/resource.ts index 4cefab76..30480b98 100644 --- a/packages/terraform/util/resource.ts +++ b/packages/terraform/util/resource.ts @@ -10,6 +10,7 @@ import { export const processDependencies = (node: any): any[] => { if ( + !['server', 'loadbalancer', 'db-mysql', 'redis'].includes( node.type.toLowerCase(), ) diff --git a/packages/terraform/util/resourceParser.ts b/packages/terraform/util/resourceParser.ts index 433232fd..08775324 100644 --- a/packages/terraform/util/resourceParser.ts +++ b/packages/terraform/util/resourceParser.ts @@ -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; @@ -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}`); }