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

Support mycelium on grid client deployments #2130

Merged
merged 34 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
f6f4ad6
add mycelium in gridclient
AlaaElattar Feb 6, 2024
b619b48
remove TODOS and debugging logs
AlaaElattar Feb 6, 2024
8b6cb94
separate validation and validation of hexseed to be shared in helpers…
AlaaElattar Feb 6, 2024
e659d78
modify generation of mycelium seed to be from ip
AlaaElattar Feb 7, 2024
bf13369
fix scripts with adding mycelium field with false
AlaaElattar Feb 7, 2024
b9894bf
update generating seed && handle scenario when updating existing netw…
AlaaElattar Feb 8, 2024
a721411
second issue fixed and working on adding mycelium from all solutions
AlaaElattar Feb 8, 2024
5470c09
remove my ssh key && mnemonic from conf file of scripts
AlaaElattar Feb 8, 2024
f5ac57e
fix build
AlaaElattar Feb 8, 2024
60f116f
support mycelium in all solutions
AlaaElattar Feb 11, 2024
042c345
change myce;ium to false in scripts
AlaaElattar Feb 11, 2024
fcc4e2e
move splitting id inside mycelium condition && remove debug logs
AlaaElattar Feb 11, 2024
ee7324f
rename mycelium_network_seed to myceliumNetworkSeed
AlaaElattar Feb 11, 2024
f34cd8e
rename to myceliumNetworkSeed
AlaaElattar Feb 11, 2024
1b5ee8b
check hex_key not empty && remove each true
AlaaElattar Feb 11, 2024
461c89d
check length of myceliumSeed && myceliumNetwrokSeed
AlaaElattar Feb 11, 2024
af5603e
rename mycelium seed to netwekAddNodeModel
AlaaElattar Feb 11, 2024
384feb2
fix renaming in addNode
AlaaElattar Feb 12, 2024
e34e049
support providing multiple seeds for network with each nodeId
AlaaElattar Feb 12, 2024
0857c58
provide 2 scripts with mycelium && fix bug in validator function && f…
AlaaElattar Feb 12, 2024
4404e2e
Merge branch 'development' into development_add_mycelium
AlaaElattar Feb 12, 2024
3c09e2c
fix renaming && apply comments
AlaaElattar Feb 12, 2024
8880615
modify zeroPadding to take only one number with length not array
AlaaElattar Feb 12, 2024
c5b3bc5
update renaming to myceliumNetworkSeeds
AlaaElattar Feb 12, 2024
b541a66
remove else and initialize seed random
AlaaElattar Feb 12, 2024
520d4e6
update mycelium seed inside MyceliumNetworkModel to seed
AlaaElattar Feb 12, 2024
babf42f
make seeds commented in k8s.ts to make user feels they're optional
AlaaElattar Feb 13, 2024
6eb6fe2
check for myceliumSeeds length
AlaaElattar Feb 13, 2024
fe13722
move generateHexSeed && zeroPadding to utils file
AlaaElattar Feb 13, 2024
7a1d869
update scripts && comment HexSeeds
AlaaElattar Feb 13, 2024
e43c1a1
add docs file for mycelium && move myelium md file and wg file in net…
AlaaElattar Feb 13, 2024
60e96c1
fix explanation says its overlay ipv6 not replacing it
AlaaElattar Feb 13, 2024
d51b9c7
add example for updating existing network with mycelium
AlaaElattar Feb 13, 2024
cf48d4c
fix typos
AlaaElattar Feb 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/grid_client/scripts/add_node_to_network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ async function main() {
name: "wedtest",
ipRange: "10.249.0.0/16",
nodeId: 14,
mycelium: false,
};
// if the network is not created, it will create one and add this node to it.
await addNode(grid3, node);
Expand Down
1 change: 1 addition & 0 deletions packages/grid_client/scripts/add_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ async function main() {
public_ip: false,
public_ip6: false,
planetary: true,
mycelium: true,
AlaaElattar marked this conversation as resolved.
Show resolved Hide resolved
};

//Add Worker
Expand Down
1 change: 1 addition & 0 deletions packages/grid_client/scripts/caprover_leader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ async function main() {
public_ip: true,
public_ip6: false,
planetary: false,
mycelium: false,
cpu: 4,
memory: 1024 * 4,
rootfs_size: 0,
Expand Down
1 change: 1 addition & 0 deletions packages/grid_client/scripts/caprover_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ async function main() {
public_ip: true,
public_ip6: false,
planetary: false,
mycelium: false,
cpu: 4,
memory: 1024 * 4,
rootfs_size: 0,
Expand Down
1 change: 1 addition & 0 deletions packages/grid_client/scripts/dynamic_single_vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ async function main() {
public_ip: false,
public_ip6: false,
planetary: true,
mycelium: false,
cpu: 1,
memory: 1024,
rootfs_size: 0,
Expand Down
2 changes: 2 additions & 0 deletions packages/grid_client/scripts/kubernetes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ async function main() {
public_ip: false,
public_ip6: false,
planetary: true,
mycelium: true,
},
],
workers: [
Expand All @@ -74,6 +75,7 @@ async function main() {
public_ip: false,
public_ip6: false,
planetary: true,
mycelium: true,
},
],
metadata: "",
Expand Down
2 changes: 2 additions & 0 deletions packages/grid_client/scripts/kubernetes_with_qsfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ async function main() {
public_ip: false,
public_ip6: false,
planetary: true,
mycelium: false,
qsfs_disks: [
{
qsfs_zdbs_name: qsfs_name,
Expand All @@ -127,6 +128,7 @@ async function main() {
public_ip: false,
public_ip6: false,
planetary: true,
mycelium: false,
},
],
metadata: "",
Expand Down
1 change: 1 addition & 0 deletions packages/grid_client/scripts/mass_deployments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ async function main() {
vm.disks = [];
vm.public_ip = publicIp;
vm.planetary = true;
vm.mycelium = false;
vm.cpu = cru;
vm.memory = mru;
vm.rootfs_size = rootFs;
Expand Down
1 change: 1 addition & 0 deletions packages/grid_client/scripts/multiple_vms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ async function main() {
public_ip: false,
public_ip6: false,
planetary: true,
mycelium: false,
cpu: 1,
memory: 1024,
rootfs_size: 0,
Expand Down
3 changes: 2 additions & 1 deletion packages/grid_client/scripts/single_vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async function main() {
machines: [
{
name: "testvm",
node_id: 17,
node_id: 11,
disks: [
{
name: "wedDisk",
Expand All @@ -46,6 +46,7 @@ async function main() {
public_ip: false,
public_ip6: false,
planetary: true,
mycelium: true,
AlaaElattar marked this conversation as resolved.
Show resolved Hide resolved
cpu: 1,
memory: 1024 * 2,
rootfs_size: 0,
Expand Down
1 change: 1 addition & 0 deletions packages/grid_client/scripts/vm_with_gpu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ async function main() {
public_ip: false,
public_ip6: false,
planetary: true,
mycelium: false,
cpu: 8,
memory: 1024 * 16,
rootfs_size: 0,
Expand Down
1 change: 1 addition & 0 deletions packages/grid_client/scripts/vm_with_qsfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ async function main() {
public_ip: false,
public_ip6: false,
planetary: true,
mycelium: false,
cpu: 1,
memory: 1024,
rootfs_size: 0,
Expand Down
1 change: 1 addition & 0 deletions packages/grid_client/scripts/wireguard_vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function createMachineModel(node: number) {
node_id: node,
public_ip: false,
planetary: true,
mycelium: false,
cpu: 1,
memory: 1024 * 2,
rootfs_size: 0,
Expand Down
23 changes: 22 additions & 1 deletion packages/grid_client/src/helpers/validator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ValidationError } from "@threefold/types";
import { plainToInstance } from "class-transformer";
import { validate } from "class-validator";
import * as crypto from "crypto";

async function validateObject(obj) {
const errors = await validate(obj);
Expand All @@ -23,4 +24,24 @@ function validateInput(target, propertyKey: string, descriptor: PropertyDescript
};
}

export { validateObject, validateInput };
function generateRandomHexSeed(length: number, numbers: number[]) {
// If no given numbers, generate all random
if (numbers.length == 0) {
const bytes = crypto.randomBytes(length);
return bytes.toString("hex");
}

const hexSeed = numbers[0].toString(16).padStart(length, "0") + numbers[1].toString(16).padStart(length, "0");

return hexSeed;
AlaaElattar marked this conversation as resolved.
Show resolved Hide resolved
}

function validateHexSeed(seed: string, length: number): boolean {
const hexSeedRegex = new RegExp(`^[0-9A-Fa-f]{${length}}$`);
if (!hexSeedRegex.test(seed)) {
throw new ValidationError(`Invalid seed. It should be a ${length}-character hexadecimal string.`);
}
return true;
}

export { validateObject, validateInput, generateRandomHexSeed, validateHexSeed };
12 changes: 12 additions & 0 deletions packages/grid_client/src/high_level/kubernetes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ class KubernetesHL extends HighLevelBase {
publicIp: boolean,
publicIp6: boolean,
planetary: boolean,
mycelium: boolean,
myceliumSeed: string,
network: Network,
networkSeed: string,
sshKey: string,
metadata = "",
description = "",
Expand Down Expand Up @@ -61,7 +64,10 @@ class KubernetesHL extends HighLevelBase {
publicIp,
publicIp6,
planetary,
mycelium,
myceliumSeed!,
network,
networkSeed!,
"/sbin/zinit init",
env,
metadata,
Expand Down Expand Up @@ -90,7 +96,10 @@ class KubernetesHL extends HighLevelBase {
publicIp: boolean,
publicIp6: boolean,
planetary: boolean,
mycelium: boolean,
myceliumSeed: string,
network: Network,
networkSeed: string,
sshKey: string,
metadata = "",
description = "",
Expand Down Expand Up @@ -131,7 +140,10 @@ class KubernetesHL extends HighLevelBase {
publicIp,
publicIp6,
planetary,
mycelium,
myceliumSeed,
network,
networkSeed,
"/sbin/zinit init",
env,
metadata,
Expand Down
48 changes: 46 additions & 2 deletions packages/grid_client/src/high_level/machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Addr } from "netaddr";

import { events } from "../helpers/events";
import { randomChoice } from "../helpers/utils";
import { generateRandomHexSeed, validateHexSeed } from "../helpers/validator";
import { DiskModel, QSFSDiskModel } from "../modules/models";
import { qsfs_zdbs } from "../modules/qsfs_zdbs";
import {
Expand Down Expand Up @@ -33,7 +34,10 @@ class VMHL extends HighLevelBase {
publicIp: boolean,
publicIp6: boolean,
planetary: boolean,
mycelium: boolean,
myceliumSeed: string,
network: Network,
networkSeed: string,
AlaaElattar marked this conversation as resolved.
Show resolved Hide resolved
entrypoint: string,
env: Record<string, unknown>,
metadata = "",
Expand Down Expand Up @@ -212,10 +216,32 @@ class VMHL extends HighLevelBase {

access_node_id = accessNodeId;
}
access_net_workload = await network.addNode(access_node_id, networkMetadata, description, accessNodeSubnet);
access_net_workload = await network.addNode(
access_node_id,
mycelium,
networkMetadata,
description,
accessNodeSubnet,
networkSeed,
);
wgConfig = await network.addAccess(access_node_id, true);
}
const znet_workload = await network.addNode(nodeId, networkMetadata, description, userIPsubnet);
// If node exits on network check if mycelium needs to be added or not
if (network.nodeExists(nodeId)) {
const deployment = await network.checkMycelium(nodeId, mycelium, networkSeed);
if (deployment) {
deployments.push(new TwinDeployment(deployment, Operations.update, 0, 0, network));
}
}

const znet_workload = await network.addNode(
nodeId,
mycelium,
networkMetadata,
description,
userIPsubnet,
networkSeed,
);
if ((await network.exists()) && (znet_workload || access_net_workload)) {
// update network
for (const deployment of network.deployments) {
Expand Down Expand Up @@ -263,6 +289,22 @@ class VMHL extends HighLevelBase {
} else {
machine_ip = network.getFreeIP(nodeId);
}

// Split machine_ip to get last 2 numbers to be used in mycelium hex seed
const parts = machine_ip.split("/");
const ipPart = parts[0];
const ipNumbers = ipPart.split(".").map(part => parseInt(part, 10));
const lastTwoNumbers = ipNumbers.slice(-2);
AlaaElattar marked this conversation as resolved.
Show resolved Hide resolved

// Validate mycelium seed If provided, if not generate it.
if (mycelium) {
if (myceliumSeed) {
validateHexSeed(myceliumSeed, 6);
} else {
myceliumSeed = generateRandomHexSeed(6, lastTwoNumbers);
}
}

events.emit("logs", `Creating a vm on node: ${nodeId}, network: ${network.name} with private ip: ${machine_ip}`);
workloads.push(
vm.create(
Expand All @@ -275,6 +317,8 @@ class VMHL extends HighLevelBase {
network.name,
machine_ip,
planetary,
mycelium,
myceliumSeed,
ipName,
entrypoint,
env,
Expand Down
13 changes: 11 additions & 2 deletions packages/grid_client/src/high_level/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@ import { HighLevelBase } from "./base";
import { Operations, TwinDeployment } from "./models";

class NetworkHL extends HighLevelBase {
async addNode(networkName: string, ipRange: string, nodeId: number, solutionProviderId: number, description = "") {
async addNode(
networkName: string,
ipRange: string,
nodeId: number,
solutionProviderId: number,
mycelium: boolean,
description = "",
subnet = "",
networkSeed = "",
) {
const network = new Network(networkName, ipRange, this.config);
await network.load();
const networkMetadata = JSON.stringify({
Expand All @@ -16,7 +25,7 @@ class NetworkHL extends HighLevelBase {
projectName: this.config.projectName,
});

const workload = await network.addNode(nodeId, networkMetadata, description);
const workload = await network.addNode(nodeId, mycelium, networkMetadata, description, subnet, networkSeed);
if (!workload) {
throw new ValidationError(`Node ${nodeId} already exists on network ${networkName}.`);
}
Expand Down
4 changes: 3 additions & 1 deletion packages/grid_client/src/modules/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,9 @@ class BaseModule {
message: workload.result.message,
flist: data.flist,
publicIP: await this._getMachinePubIP(deploymentName, deployments, data.network.public_ip),
planetary: data.network.planetary ? (workload.result.data as ZmachineResult).ygg_ip : "",
planetary: data.network.planetary
? (workload.result.data as ZmachineResult).ygg_ip || (workload.result.data as ZmachineResult).planetary_ip
: "",
interfaces: data.network.interfaces.map(n => ({
network: n.network,
ip: n.ip,
Expand Down
9 changes: 9 additions & 0 deletions packages/grid_client/src/modules/k8s.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ class K8sModule extends BaseModule {
master.public_ip,
master.public_ip6,
master.planetary,
master.mycelium,
master.myceliumSeed!,
network,
options.network.network_seed!,
MohamedElmdary marked this conversation as resolved.
Show resolved Hide resolved
options.ssh_key,
options.metadata || metadata,
options.description,
Expand Down Expand Up @@ -148,7 +151,10 @@ class K8sModule extends BaseModule {
worker.public_ip,
worker.public_ip6,
worker.planetary,
worker.mycelium,
worker.myceliumSeed!,
network,
options.network.network_seed!,
options.ssh_key,
options.metadata || metadata,
options.description,
Expand Down Expand Up @@ -286,7 +292,10 @@ class K8sModule extends BaseModule {
options.public_ip,
options.public_ip6,
options.planetary,
options.mycelium,
options.myceliumSeed!,
network,
options.mycelium_network_seed!,
masterWorkload.data["env"]["SSH_KEY"],
masterWorkload.metadata,
masterWorkload.description,
Expand Down
7 changes: 7 additions & 0 deletions packages/grid_client/src/modules/machines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ class MachinesModule extends BaseModule {
machine.public_ip,
machine.public_ip6!,
machine.planetary,
machine.mycelium,
machine.myceliumSeed!,
network,
options.network.network_seed!,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the same here the network's seed will be used for all the vms and will lead to conflict in the routing

machine.entrypoint,
machine.env,
options.metadata || metadata,
Expand Down Expand Up @@ -104,6 +107,7 @@ class MachinesModule extends BaseModule {
async getObj(deploymentName: string) {
const deployments = await this._get(deploymentName);
const workloads = await this._getWorkloadsByTypes(deploymentName, deployments, [WorkloadTypes.zmachine]);
console.log("Deployments: ", workloads);
AlaaElattar marked this conversation as resolved.
Show resolved Hide resolved
const promises = workloads.map(
async workload => await this._getZmachineData(deploymentName, deployments, workload),
);
Expand Down Expand Up @@ -177,7 +181,10 @@ class MachinesModule extends BaseModule {
options.public_ip,
options.public_ip6!,
options.planetary,
options.mycelium,
options.myceliumSeed!,
network,
options.mycelium_network_seed!,
options.entrypoint,
options.env,
workload.metadata,
Expand Down
Loading
Loading