Skip to content

Commit

Permalink
feat: refactored into a BootNodeResource with NodeResource base class…
Browse files Browse the repository at this point in the history
… for native provider
  • Loading branch information
l0r1s committed Apr 4, 2023
1 parent 3448873 commit ea60645
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 82 deletions.
Original file line number Diff line number Diff line change
@@ -1,59 +1,18 @@
import { getRandomPort, makeDir } from "@zombienet/utils";
import { genCmd } from "../../cmdGenerator";
import { getRandomPort } from "@zombienet/utils";
import { getUniqueName } from "../../configGenerator";
import {
P2P_PORT,
PROMETHEUS_PORT,
RPC_HTTP_PORT,
RPC_WS_PORT,
} from "../../constants";
import { Network } from "../../network";
import { Node, envVars } from "../../types";
import { Node } from "../../types";
import { getClient } from "../client";
import { NodeResource } from "./resources";

interface processEnvironment {
[key: string]: string;
}
import { BootNodeResource, NodeResource } from "./resources";

export async function genBootnodeDef(
namespace: string,
nodeSetup: Node,
): Promise<any> {
const client = getClient();
const name = nodeSetup.name;
const { rpcPort, wsPort, prometheusPort, p2pPort } = nodeSetup;
const ports = await getPorts(rpcPort, wsPort, prometheusPort, p2pPort);

const cfgPath = `${client.tmpDir}/${name}/cfg`;
await makeDir(cfgPath, true);
const bootNodeResource = new BootNodeResource(client, namespace, nodeSetup);

const dataPath = `${client.tmpDir}/${name}/data`;
await makeDir(dataPath, true);

const command = await genCmd(nodeSetup, cfgPath, dataPath, false);
return {
metadata: {
name: "bootnode",
namespace: namespace,
labels: {
name: namespace,
instance: "bootnode",
"zombie-role": "bootnode",
app: "zombienet",
"zombie-ns": namespace,
},
},
spec: {
cfgPath: `${client.tmpDir}/${nodeSetup.name}/cfg`,
ports,
command,
env: nodeSetup.env.reduce((memo, item: envVars) => {
memo[item.name] = item.value;
return memo;
}, {} as processEnvironment),
},
};
return bootNodeResource.generateSpec();
}

export async function genNodeDef(
Expand All @@ -66,42 +25,6 @@ export async function genNodeDef(
return nodeResource.generateSpec();
}

async function getPorts(
rpc?: number,
ws?: number,
prometheus?: number,
p2p?: number,
) {
const ports = [
{
containerPort: PROMETHEUS_PORT,
name: "prometheus",
flag: "--prometheus-port",
hostPort: prometheus || (await getRandomPort()),
},
{
containerPort: RPC_HTTP_PORT,
name: "rpc",
flag: "--rpc-port",
hostPort: rpc || (await getRandomPort()),
},
{
containerPort: RPC_WS_PORT,
name: "ws",
flag: "--ws-port",
hostPort: ws || (await getRandomPort()),
},
{
containerPort: P2P_PORT,
name: "p2p",
flag: "--port",
hostPort: p2p || (await getRandomPort()),
},
];

return ports;
}

export function replaceNetworkRef(podDef: any, network: Network) {
// replace command if needed
if (Array.isArray(podDef.spec.command)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { makeDir } from "@zombienet/utils";
import { genCmd } from "../../../cmdGenerator";
import { NodeResource } from "./node.resource";
import { NodeSpec, Port, ProcessEnvironment, ZombieRole } from "./types";

export class BootNodeResource extends NodeResource {
protected async createDirectories() {
try {
await makeDir(this.configPath, true);
await makeDir(this.dataPath, true);
} catch {
throw new Error(
`Error generating directories for ${this.nodeSetupConfig.name} resource`,
);
}
}

protected generateCommand() {
return genCmd(this.nodeSetupConfig, this.configPath, this.dataPath, false);
}

protected getZombieRole(): ZombieRole {
return "bootnode";
}

protected generateNodeSpec(
ports: Port[],
command: string[],
zombieRole: ZombieRole,
env: ProcessEnvironment,
): NodeSpec {
return {
metadata: {
name: "bootnode",
namespace: this.namespace,
labels: {
name: this.namespace,
instance: "bootnode",
"zombie-role": zombieRole,
app: "zombienet",
"zombie-ns": this.namespace,
},
},
spec: {
cfgPath: this.configPath,
ports,
command,
env,
},
};
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { BootNodeResource } from "./bootnode.resource";
export { NodeResource } from "./node.resource";

0 comments on commit ea60645

Please sign in to comment.