From ca7d40002a6da0a2932e67a7ab3d05e3529045be Mon Sep 17 00:00:00 2001 From: ahmedhanafy725 Date: Thu, 20 Apr 2023 16:28:38 +0200 Subject: [PATCH 1/2] grid client: Support wireguard network on gateways --- .../grid_client/src/high_level/gateway.ts | 5 +++-- packages/grid_client/src/modules/gateway.ts | 2 ++ packages/grid_client/src/modules/models.ts | 2 ++ .../grid_client/src/primitives/gateway.ts | 20 ++++++++++++------- packages/grid_client/src/zos/gateway.ts | 6 +++++- 5 files changed, 25 insertions(+), 10 deletions(-) diff --git a/packages/grid_client/src/high_level/gateway.ts b/packages/grid_client/src/high_level/gateway.ts index b3577eb88e..c9ad0f2991 100644 --- a/packages/grid_client/src/high_level/gateway.ts +++ b/packages/grid_client/src/high_level/gateway.ts @@ -8,6 +8,7 @@ class GatewayHL extends HighLevelBase { node_id: number, tls_passthrough: boolean, backends: string[], + network: string, metadata: string, description: string, fqdn = "", @@ -17,9 +18,9 @@ class GatewayHL extends HighLevelBase { const gw = new GWPrimitive(); const workloads = []; if (fqdn != "") { - workloads.push(gw.createFQDN(fqdn, tls_passthrough, backends, name, metadata, description)); + workloads.push(gw.createFQDN(fqdn, tls_passthrough, backends, name, network, metadata, description)); } else { - workloads.push(gw.createName(name, tls_passthrough, backends, metadata, description)); + workloads.push(gw.createName(name, tls_passthrough, backends, network, metadata, description)); } const deploymentFactory = new DeploymentFactory(this.config); const deployment = deploymentFactory.create(workloads, 1626394539, metadata, description, 0); diff --git a/packages/grid_client/src/modules/gateway.ts b/packages/grid_client/src/modules/gateway.ts index 9bd757b86b..559a48c798 100644 --- a/packages/grid_client/src/modules/gateway.ts +++ b/packages/grid_client/src/modules/gateway.ts @@ -42,6 +42,7 @@ class GWModule extends BaseModule { options.node_id, options.tls_passthrough, options.backends, + options.network, options.metadata || metadata, options.description, options.fqdn, @@ -69,6 +70,7 @@ class GWModule extends BaseModule { options.node_id, options.tls_passthrough, options.backends, + options.network, options.metadata || metadata, options.description, "", diff --git a/packages/grid_client/src/modules/models.ts b/packages/grid_client/src/modules/models.ts index 33400d6679..5a7bea48e2 100644 --- a/packages/grid_client/src/modules/models.ts +++ b/packages/grid_client/src/modules/models.ts @@ -210,6 +210,7 @@ class GatewayFQDNModel { @Expose() @IsInt() @Min(1) node_id: number; @Expose() @IsString() @IsNotEmpty() fqdn: string; @Expose() @IsBoolean() tls_passthrough: boolean; + @Expose() @IsString() @IsOptional() network?: string; @Expose() @IsString() @IsOptional() metadata?: string; @Expose() @IsString() @IsOptional() description?: string; @Expose() @ArrayNotEmpty() @IsUrl({ protocols: ["http", "https"] }, { each: true }) backends: string[]; @@ -227,6 +228,7 @@ class BaseGatewayNameModel { class GatewayNameModel extends BaseGatewayNameModel { @Expose() @IsInt() @Min(1) node_id: number; @Expose() @IsBoolean() tls_passthrough: boolean; + @Expose() @IsString() @IsOptional() network?: string; @Expose() @IsString() @IsOptional() metadata?: string; @Expose() @IsString() @IsOptional() description?: string; @Expose() @ArrayNotEmpty() @IsUrl({ protocols: ["http", "https"] }, { each: true }) backends: string[]; diff --git a/packages/grid_client/src/primitives/gateway.ts b/packages/grid_client/src/primitives/gateway.ts index dceab2b130..f4d8f9743a 100644 --- a/packages/grid_client/src/primitives/gateway.ts +++ b/packages/grid_client/src/primitives/gateway.ts @@ -7,6 +7,7 @@ class GWPrimitive { tls_passthrough: boolean, backends: string[], name: string, + network?: string, metadata = "", description = "", version = 0, @@ -15,6 +16,7 @@ class GWPrimitive { fqdnObj.fqdn = fqdn; fqdnObj.tls_passthrough = tls_passthrough; fqdnObj.backends = backends; + fqdnObj.network = network; const fqdn_workload = new Workload(); fqdn_workload.version = version; @@ -32,31 +34,34 @@ class GWPrimitive { tls_passthrough: boolean, backends: string[], name: string, + network?: string, metadata = "", description = "", old_version = 1, ): Workload { - return this.createFQDN(fqdn, tls_passthrough, backends, name, metadata, description, old_version + 1); + return this.createFQDN(fqdn, tls_passthrough, backends, name, network, metadata, description, old_version + 1); } createName( name: string, tls_passthrough: boolean, backends: string[], + network?: string, metadata = "", description = "", version = 0, ): Workload { - const nameOnj = new GatewayNameProxy(); - nameOnj.name = name; - nameOnj.tls_passthrough = tls_passthrough; - nameOnj.backends = backends; + const nameObj = new GatewayNameProxy(); + nameObj.name = name; + nameObj.tls_passthrough = tls_passthrough; + nameObj.backends = backends; + nameObj.network = network; const name_workload = new Workload(); name_workload.version = version; name_workload.name = name; name_workload.type = WorkloadTypes.gatewaynameproxy; - name_workload.data = nameOnj; + name_workload.data = nameObj; name_workload.metadata = metadata; name_workload.description = description; @@ -67,11 +72,12 @@ class GWPrimitive { name: string, tls_passthrough: boolean, backends: string[], + network?: string, metadata = "", description = "", old_version = 1, ): Workload { - return this.createName(name, tls_passthrough, backends, metadata, description, old_version + 1); + return this.createName(name, tls_passthrough, backends, network, metadata, description, old_version + 1); } } export { GWPrimitive }; diff --git a/packages/grid_client/src/zos/gateway.ts b/packages/grid_client/src/zos/gateway.ts index e24fd11014..866577d321 100644 --- a/packages/grid_client/src/zos/gateway.ts +++ b/packages/grid_client/src/zos/gateway.ts @@ -1,5 +1,5 @@ import { Expose } from "class-transformer"; -import { ArrayNotEmpty, IsBoolean, IsFQDN, IsNotEmpty, IsString, IsUrl } from "class-validator"; +import { ArrayNotEmpty, IsBoolean, IsFQDN, IsNotEmpty, IsOptional, IsString, IsUrl } from "class-validator"; import { WorkloadData, WorkloadDataResult } from "./workload_base"; @@ -7,6 +7,7 @@ class GatewayFQDNProxy extends WorkloadData { @Expose() @IsFQDN() fqdn: string; @Expose() @IsBoolean() tls_passthrough: boolean; @Expose() @ArrayNotEmpty() @IsUrl({ protocols: ["http", "https"] }, { each: true }) backends: string[]; + @Expose() @IsString() @IsOptional() network: string; challenge(): string { let out = ""; @@ -15,6 +16,7 @@ class GatewayFQDNProxy extends WorkloadData { for (const backend of this.backends) { out += backend; } + if (this.network) out += this.network; return out; } } @@ -23,6 +25,7 @@ class GatewayNameProxy extends WorkloadData { @Expose() @IsString() @IsNotEmpty() name: string; @Expose() @IsBoolean() tls_passthrough: boolean; @Expose() @ArrayNotEmpty() @IsUrl({ protocols: ["http", "https"] }, { each: true }) backends: string[]; + @Expose() @IsString() @IsOptional() network: string; challenge(): string { let out = ""; @@ -31,6 +34,7 @@ class GatewayNameProxy extends WorkloadData { for (const backend of this.backends) { out += backend; } + if (this.network) out += this.network; return out; } } From 639c3b18fff9e085205cee1f98bf2d39a45fa45c Mon Sep 17 00:00:00 2001 From: ahmedhanafy725 Date: Thu, 20 Apr 2023 17:14:23 +0200 Subject: [PATCH 2/2] grid client: Add access node id on the network to select the access node to be used --- .../grid_client/src/high_level/kubernetes.ts | 4 ++++ .../grid_client/src/high_level/machine.ts | 19 +++++++++++++++---- packages/grid_client/src/modules/k8s.ts | 3 +++ packages/grid_client/src/modules/machines.ts | 2 ++ packages/grid_client/src/modules/models.ts | 1 + 5 files changed, 25 insertions(+), 4 deletions(-) diff --git a/packages/grid_client/src/high_level/kubernetes.ts b/packages/grid_client/src/high_level/kubernetes.ts index 24bf8ab452..2f7d295bba 100644 --- a/packages/grid_client/src/high_level/kubernetes.ts +++ b/packages/grid_client/src/high_level/kubernetes.ts @@ -27,6 +27,7 @@ class KubernetesHL extends HighLevelBase { qsfs_disks: QSFSDiskModel[] = [], qsfsProjectName = "", addAccess = false, + accessNodeId = 0, ip = "", corex = false, solutionProviderID: number, @@ -67,6 +68,7 @@ class KubernetesHL extends HighLevelBase { qsfs_disks, qsfsProjectName, addAccess, + accessNodeId, ip, corex, solutionProviderID, @@ -93,6 +95,7 @@ class KubernetesHL extends HighLevelBase { qsfs_disks: QSFSDiskModel[] = [], qsfsProjectName = "", addAccess = false, + accessNodeId = 0, ip = "", corex = false, solutionProviderID: number, @@ -133,6 +136,7 @@ class KubernetesHL extends HighLevelBase { qsfs_disks, qsfsProjectName, addAccess, + accessNodeId, ip, corex, solutionProviderID, diff --git a/packages/grid_client/src/high_level/machine.ts b/packages/grid_client/src/high_level/machine.ts index c40ff9a4a4..305db9623a 100644 --- a/packages/grid_client/src/high_level/machine.ts +++ b/packages/grid_client/src/high_level/machine.ts @@ -40,6 +40,7 @@ class VMHL extends HighLevelBase { qsfsDisks: QSFSDiskModel[] = [], qsfsProjectName = "", addAccess = false, + accessNodeId = 0, ip = "", corex = false, solutionProviderID: number, @@ -157,15 +158,25 @@ class VMHL extends HighLevelBase { } } } - if (!Object.keys(accessNodes).includes(nodeId.toString()) && !hasAccessNode && addAccess) { + if ( + (!Object.keys(accessNodes).includes(nodeId.toString()) || nodeId !== accessNodeId) && + !hasAccessNode && + addAccess + ) { // add node to any access node and deploy it - const filteredAccessNodes = []; + const filteredAccessNodes: number[] = []; for (const accessNodeId of Object.keys(accessNodes)) { if (accessNodes[accessNodeId]["ipv4"]) { - filteredAccessNodes.push(accessNodeId); + filteredAccessNodes.push(+accessNodeId); } } - const access_node_id = Number(randomChoice(filteredAccessNodes)); + let access_node_id = randomChoice(filteredAccessNodes); + if (accessNodeId) { + if (!filteredAccessNodes.includes(accessNodeId)) + throw Error(`Node ${accessNodeId} is not an access not or maybe it's down`); + + access_node_id = accessNodeId; + } access_net_workload = await network.addNode(access_node_id, networkMetadata, description, accessNodeSubnet); wgConfig = await network.addAccess(access_node_id, true); } diff --git a/packages/grid_client/src/modules/k8s.ts b/packages/grid_client/src/modules/k8s.ts index 877e71f443..0c7929d1a8 100644 --- a/packages/grid_client/src/modules/k8s.ts +++ b/packages/grid_client/src/modules/k8s.ts @@ -109,6 +109,7 @@ class K8sModule extends BaseModule { master.qsfs_disks, this.config.projectName, options.network.addAccess, + options.network.accessNodeId, master.ip, master.corex, master.solutionProviderID!, @@ -151,6 +152,7 @@ class K8sModule extends BaseModule { worker.qsfs_disks, this.config.projectName, options.network.addAccess, + options.network.accessNodeId, worker.ip, worker.corex, worker.solutionProviderID!, @@ -284,6 +286,7 @@ class K8sModule extends BaseModule { options.qsfs_disks, this.config.projectName, false, + 0, options.ip, options.corex, options.solutionProviderID!, diff --git a/packages/grid_client/src/modules/machines.ts b/packages/grid_client/src/modules/machines.ts index 66785dc9b2..cc903726f8 100644 --- a/packages/grid_client/src/modules/machines.ts +++ b/packages/grid_client/src/modules/machines.ts @@ -65,6 +65,7 @@ class MachinesModule extends BaseModule { machine.qsfs_disks, this.config.projectName, options.network.addAccess, + options.network.accessNodeId, machine.ip, machine.corex, machine.solutionProviderID!, @@ -178,6 +179,7 @@ class MachinesModule extends BaseModule { options.qsfs_disks, this.config.projectName, false, + 0, options.ip, options.corex, options.solutionProviderID!, diff --git a/packages/grid_client/src/modules/models.ts b/packages/grid_client/src/modules/models.ts index 5a7bea48e2..95436e7e90 100644 --- a/packages/grid_client/src/modules/models.ts +++ b/packages/grid_client/src/modules/models.ts @@ -75,6 +75,7 @@ class NetworkModel { @Expose() @IsString() @IsNotEmpty() @IsAlphanumeric() @MaxLength(NameLength) name: string; @Expose() @IsString() @IsNotEmpty() ip_range: string; @Expose() @IsBoolean() @IsOptional() addAccess?: boolean; + @Expose() @IsInt() @Min(1) @IsOptional() accessNodeId?: number; } class BaseGetDeleteModel {