Skip to content

Commit

Permalink
Merge pull request #178 from threefoldtech/development_gateway
Browse files Browse the repository at this point in the history
Support wireguard network on gateways
  • Loading branch information
AhmedHanafy725 authored Apr 20, 2023
2 parents a069d71 + 639c3b1 commit 3830058
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 14 deletions.
5 changes: 3 additions & 2 deletions packages/grid_client/src/high_level/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class GatewayHL extends HighLevelBase {
node_id: number,
tls_passthrough: boolean,
backends: string[],
network: string,
metadata: string,
description: string,
fqdn = "",
Expand All @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions packages/grid_client/src/high_level/kubernetes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class KubernetesHL extends HighLevelBase {
qsfs_disks: QSFSDiskModel[] = [],
qsfsProjectName = "",
addAccess = false,
accessNodeId = 0,
ip = "",
corex = false,
solutionProviderID: number,
Expand Down Expand Up @@ -67,6 +68,7 @@ class KubernetesHL extends HighLevelBase {
qsfs_disks,
qsfsProjectName,
addAccess,
accessNodeId,
ip,
corex,
solutionProviderID,
Expand All @@ -93,6 +95,7 @@ class KubernetesHL extends HighLevelBase {
qsfs_disks: QSFSDiskModel[] = [],
qsfsProjectName = "",
addAccess = false,
accessNodeId = 0,
ip = "",
corex = false,
solutionProviderID: number,
Expand Down Expand Up @@ -133,6 +136,7 @@ class KubernetesHL extends HighLevelBase {
qsfs_disks,
qsfsProjectName,
addAccess,
accessNodeId,
ip,
corex,
solutionProviderID,
Expand Down
19 changes: 15 additions & 4 deletions packages/grid_client/src/high_level/machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class VMHL extends HighLevelBase {
qsfsDisks: QSFSDiskModel[] = [],
qsfsProjectName = "",
addAccess = false,
accessNodeId = 0,
ip = "",
corex = false,
solutionProviderID: number,
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/grid_client/src/modules/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -69,6 +70,7 @@ class GWModule extends BaseModule {
options.node_id,
options.tls_passthrough,
options.backends,
options.network,
options.metadata || metadata,
options.description,
"",
Expand Down
3 changes: 3 additions & 0 deletions packages/grid_client/src/modules/k8s.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!,
Expand Down Expand Up @@ -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!,
Expand Down Expand Up @@ -284,6 +286,7 @@ class K8sModule extends BaseModule {
options.qsfs_disks,
this.config.projectName,
false,
0,
options.ip,
options.corex,
options.solutionProviderID!,
Expand Down
2 changes: 2 additions & 0 deletions packages/grid_client/src/modules/machines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!,
Expand Down Expand Up @@ -178,6 +179,7 @@ class MachinesModule extends BaseModule {
options.qsfs_disks,
this.config.projectName,
false,
0,
options.ip,
options.corex,
options.solutionProviderID!,
Expand Down
3 changes: 3 additions & 0 deletions packages/grid_client/src/modules/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -210,6 +211,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[];
Expand All @@ -227,6 +229,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[];
Expand Down
20 changes: 13 additions & 7 deletions packages/grid_client/src/primitives/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class GWPrimitive {
tls_passthrough: boolean,
backends: string[],
name: string,
network?: string,
metadata = "",
description = "",
version = 0,
Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -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 };
6 changes: 5 additions & 1 deletion packages/grid_client/src/zos/gateway.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
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";

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 = "";
Expand All @@ -15,6 +16,7 @@ class GatewayFQDNProxy extends WorkloadData {
for (const backend of this.backends) {
out += backend;
}
if (this.network) out += this.network;
return out;
}
}
Expand All @@ -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 = "";
Expand All @@ -31,6 +34,7 @@ class GatewayNameProxy extends WorkloadData {
for (const backend of this.backends) {
out += backend;
}
if (this.network) out += this.network;
return out;
}
}
Expand Down

0 comments on commit 3830058

Please sign in to comment.