Skip to content

Commit

Permalink
[feat] change replaceMultiAddresReferences for replaceNetworkRef to m…
Browse files Browse the repository at this point in the history
…ake it m… (#285)

* chage replaceMultiAddresReferences for replaceNetworkRef to make it more flexible

* update colander image for tests

* fix multiAddress store in network node

* clean tests
  • Loading branch information
pepoviola authored Aug 10, 2022
1 parent 86c3830 commit a1253d9
Show file tree
Hide file tree
Showing 20 changed files with 91 additions and 105 deletions.
14 changes: 7 additions & 7 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ zombienet-restart-pause-resume:
- echo "${GH_DIR}"
- export DEBUG=zombie*
- export ZOMBIENET_INTEGRATION_TEST_IMAGE="docker.io/paritypr/polkadot-debug:master"
- export COL_IMAGE="docker.io/paritypr/colander:4519"
- export COL_IMAGE="docker.io/paritypr/colander:master"

script:
- /home/nonroot/zombie-net/scripts/ci/run-test-local-env-manager.sh
Expand All @@ -287,7 +287,7 @@ zombienet-logs-assertion:
- echo "${GH_DIR}"
- export DEBUG=zombie*
- export ZOMBIENET_INTEGRATION_TEST_IMAGE="docker.io/paritypr/polkadot-debug:master"
- export COL_IMAGE="docker.io/paritypr/colander:4519"
- export COL_IMAGE="docker.io/paritypr/colander:master"

script:
- /home/nonroot/zombie-net/scripts/ci/run-test-local-env-manager.sh
Expand All @@ -312,7 +312,7 @@ zombienet-system-event-assertion:
- echo "${GH_DIR}"
- export DEBUG=zombie*
- export ZOMBIENET_INTEGRATION_TEST_IMAGE="docker.io/paritypr/polkadot-debug:master"
- export COL_IMAGE="docker.io/paritypr/colander:4519"
- export COL_IMAGE="docker.io/paritypr/colander:master"

script:
- /home/nonroot/zombie-net/scripts/ci/run-test-local-env-manager.sh
Expand All @@ -337,7 +337,7 @@ zombienet-custom-scripts-assertion:
- echo "${GH_DIR}"
- export DEBUG=zombie*
- export ZOMBIENET_INTEGRATION_TEST_IMAGE="docker.io/paritypr/polkadot-debug:master"
- export COL_IMAGE="docker.io/paritypr/colander:4519"
- export COL_IMAGE="docker.io/paritypr/colander:master"

script:
- /home/nonroot/zombie-net/scripts/ci/run-test-local-env-manager.sh
Expand Down Expand Up @@ -368,7 +368,7 @@ zombienet-histogram-assertion:
- echo "${GH_DIR}"
- export DEBUG=zombie*
- export ZOMBIENET_INTEGRATION_TEST_IMAGE="docker.io/paritypr/polkadot-debug:master"
- export COL_IMAGE="docker.io/paritypr/colander:4519"
- export COL_IMAGE="docker.io/paritypr/colander:master"

script:
- /home/nonroot/zombie-net/scripts/ci/run-test-local-env-manager.sh
Expand Down Expand Up @@ -399,7 +399,7 @@ zombienet-update-genesis-config:
- echo "${GH_DIR}"
- export DEBUG=zombie*
- export ZOMBIENET_INTEGRATION_TEST_IMAGE="docker.io/paritypr/polkadot-debug:master"
- export COL_IMAGE="docker.io/paritypr/colander:4519"
- export COL_IMAGE="docker.io/paritypr/colander:master"

script:
- /home/nonroot/zombie-net/scripts/ci/run-test-local-env-manager.sh
Expand Down Expand Up @@ -430,7 +430,7 @@ zombienet-scale-net-config:
- echo "${GH_DIR}"
- export DEBUG=zombie*
- export ZOMBIENET_INTEGRATION_TEST_IMAGE="docker.io/paritypr/polkadot-debug:master"
- export COL_IMAGE="docker.io/paritypr/colander:4519"
- export COL_IMAGE="docker.io/paritypr/colander:master"

script:
- /home/nonroot/zombie-net/scripts/ci/run-test-local-env-manager.sh
Expand Down
78 changes: 40 additions & 38 deletions src/cmdGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,49 +124,51 @@ export async function genCumulusCollatorCmd(
}
}

// Arguments for the relay chain node part of the collator binary.
fullCmd.push(...["--", "--chain", `${cfgPath}/${chain}.json`, "--execution wasm"]);

if (argsFullNode) {
// Add any additional flags to the CLI
for (const [index, arg] of argsFullNode.entries()) {
if (collatorPorts[arg] >= 0) {
// port passed as argument, we need to ensure is not a default one because it will be
// use by the parachain part.
const selectedPort = parseInt(argsFullNode[index + 1], 10);
if ([P2P_PORT, RPC_HTTP_PORT, RPC_WS_PORT].includes(selectedPort)) {
console.log(
decorators.yellow(
`WARN: default port configured, changing to use a random free port`
)
);
const randomPort = await getRandomPort();
collatorPorts[arg] = randomPort;
argsFullNode[index + 1] = randomPort.toString();
if(fullCmd.findIndex(thisArg => thisArg.includes("relay-chain-rpc-url")) === -1 ) {
// Arguments for the relay chain node part of the collator binary.
fullCmd.push(...["--", "--chain", `${cfgPath}/${chain}.json`, "--execution wasm"]);

if (argsFullNode) {
// Add any additional flags to the CLI
for (const [index, arg] of argsFullNode.entries()) {
if (collatorPorts[arg] >= 0) {
// port passed as argument, we need to ensure is not a default one because it will be
// use by the parachain part.
const selectedPort = parseInt(argsFullNode[index + 1], 10);
if ([P2P_PORT, RPC_HTTP_PORT, RPC_WS_PORT].includes(selectedPort)) {
console.log(
decorators.yellow(
`WARN: default port configured, changing to use a random free port`
)
);
const randomPort = await getRandomPort();
collatorPorts[arg] = randomPort;
argsFullNode[index + 1] = randomPort.toString();
}
}
}
}

// check ports
for (const portArg of Object.keys(collatorPorts)) {
if (collatorPorts[portArg] === 0) {
const randomPort = await getRandomPort();
argsFullNode.push(portArg);
argsFullNode.push(randomPort.toString());
debug(`Added ${portArg} with value ${randomPort}`);
// check ports
for (const portArg of Object.keys(collatorPorts)) {
if (collatorPorts[portArg] === 0) {
const randomPort = await getRandomPort();
argsFullNode.push(portArg);
argsFullNode.push(randomPort.toString());
debug(`Added ${portArg} with value ${randomPort}`);
}
}
}

fullCmd = fullCmd.concat(argsFullNode);
debug(`Added ${argsFullNode} to collator`);
} else {
// ensure ports
for (const portArg of Object.keys(collatorPorts)) {
if (collatorPorts[portArg] === 0) {
const randomPort = await getRandomPort();
fullCmd.push(portArg);
fullCmd.push(randomPort.toString());
debug(`Added ${portArg} with value ${randomPort}`);
fullCmd = fullCmd.concat(argsFullNode);
debug(`Added ${argsFullNode} to collator`);
} else {
// ensure ports
for (const portArg of Object.keys(collatorPorts)) {
if (collatorPorts[portArg] === 0) {
const randomPort = await getRandomPort();
fullCmd.push(portArg);
fullCmd.push(randomPort.toString());
debug(`Added ${portArg} with value ${randomPort}`);
}
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,4 +356,11 @@ export class Network {
console.log(`\t\t Node prometheus link: ${node.prometheusUri}\n`);
console.log("---\n");
}

replaceWithNetworInfo(placeholder: string): string {
return placeholder.replace(/{{ZOMBIE:(.*?):(.*?)}}/ig, (_substring, nodeName, key: keyof NetworkNode) => {
const node = this.getNodeByName(nodeName);
return node[key];
});
}
}
5 changes: 4 additions & 1 deletion src/networkNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class NetworkNode implements NetworkNodeInterface {
name: string;
wsUri: string;
prometheusUri: string;
multiAddress: string;
apiInstance?: ApiPromise;
spec?: object | undefined;
cachedMetrics?: Metrics;
Expand All @@ -42,11 +43,13 @@ export class NetworkNode implements NetworkNodeInterface {
name: string,
wsUri: string,
prometheusUri: string,
userDefinedTypes: any = null
multiAddress: string,
userDefinedTypes: any = null,
) {
this.name = name;
this.wsUri = wsUri;
this.prometheusUri = prometheusUri;
this.multiAddress = multiAddress;

if (userDefinedTypes) this.userDefinedTypes = userDefinedTypes;
}
Expand Down
12 changes: 8 additions & 4 deletions src/orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export async function start(
initClient,
setupChainSpec,
getChainSpecRaw,
replaceMultiAddresReferences,
replaceNetworkRef,
} = Providers.get(networkSpec.settings.provider);

const client = initClient(credentials, namespace, tmpDir.path);
Expand Down Expand Up @@ -378,8 +378,8 @@ export async function start(
keystoreLocalDir = path.dirname(keystoreFiles[0]);
}

// replace all multiaddress references in command
replaceMultiAddresReferences(podDef, multiAddressByNode)
// replace all network references in command
replaceNetworkRef(podDef, network);

await client.spawnFromDef(
podDef,
Expand Down Expand Up @@ -416,6 +416,7 @@ export async function start(
"{{PORT}}",
PROMETHEUS_PORT.toString()
),
nodeMultiAddress,
userDefinedTypes
);
} else {
Expand All @@ -439,6 +440,7 @@ export async function start(
"{{PORT}}",
nodePrometheusPort.toString()
),
nodeMultiAddress,
userDefinedTypes
);
}
Expand Down Expand Up @@ -578,11 +580,13 @@ export async function start(
const IP = (options?.inCI) ? await client.getNodeIP(INTROSPECTOR_POD_NAME) : LOCALHOST;
const PORT = (options?.inCI) ? INTROSPECTOR_PORT : await client.startPortForwarding(INTROSPECTOR_PORT, INTROSPECTOR_POD_NAME);

// TODO: create a new kind `companion`
const introspectorNetworkNode = new NetworkNode(
INTROSPECTOR_POD_NAME,
"",
METRICS_URI_PATTERN.replace("{{IP}}", IP).replace(
"{{PORT}}",PORT.toString())
"{{PORT}}",PORT.toString()),
""
);

network.addNode(introspectorNetworkNode, Scope.COMPANION);
Expand Down
14 changes: 4 additions & 10 deletions src/providers/k8s/dynResourceDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { getUniqueName } from "../../configGenerator";
import { MultiAddressByNode, Node } from "../../types";
import { getSha256 } from "../../utils/misc-utils";
import { Network } from "../../network";

export async function genBootnodeDef(
namespace: string,
Expand Down Expand Up @@ -198,22 +199,15 @@ function jaegerAgentDef() {
}
}

export function replaceMultiAddresReferences(podDef: any, multiAddressByNode: MultiAddressByNode) {
export function replaceNetworkRef(podDef: any, network: Network) {
// replace command if needed in containers
for( const container of podDef.spec.containers) {
if(Array.isArray(container.command)){
const finalCommand = container.command.map((item: string) => {
return item.replace(/{{ZOMBIE:(.*?)?}}/ig, (_substring, nodeName) => {
return multiAddressByNode[nodeName];
});
});
const finalCommand = container.command.map((item: string) => network.replaceWithNetworInfo(item));
container.command = finalCommand;
} else {
container.command = container.command.replace(/{{ZOMBIE:(.*?)?}}/ig, (_substring: any, nodeName: string) => {
return multiAddressByNode[nodeName];
});
container.command = network.replaceWithNetworInfo(container.command);
}

}
}

Expand Down
4 changes: 2 additions & 2 deletions src/providers/k8s/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { KubeClient, initClient } from "./kubeClient";
import { genBootnodeDef, genNodeDef, replaceMultiAddresReferences } from "./dynResourceDefinition";
import { genBootnodeDef, genNodeDef, replaceNetworkRef } from "./dynResourceDefinition";
import { setupChainSpec, getChainSpecRaw } from "./chain-spec";

export const provider = {
Expand All @@ -9,5 +9,5 @@ export const provider = {
initClient,
setupChainSpec,
getChainSpecRaw,
replaceMultiAddresReferences,
replaceNetworkRef,
};
13 changes: 4 additions & 9 deletions src/providers/native/dynResourceDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getUniqueName } from "../../configGenerator";
import { MultiAddressByNode, Node } from "../../types";
import { getRandomPort } from "../../utils/net-utils";
import { getClient } from "../client";
import { Network } from "../../network";

const fs = require("fs").promises;

Expand Down Expand Up @@ -131,20 +132,14 @@ async function getPorts(rpc?: number, ws?:number, prometheus?:number) {
return ports;
}

export function replaceMultiAddresReferences(podDef: any, multiAddressByNode: MultiAddressByNode) {
export function replaceNetworkRef(podDef: any, network: Network) {
// replace command if needed
if(Array.isArray(podDef.spec.command)) {
const finalCommand = podDef.spec.command.map((item: string) => {
return item.replace(/{{ZOMBIE:(.*?)?}}/ig, (_substring, nodeName) => {
return multiAddressByNode[nodeName];
});
});
const finalCommand = podDef.spec.command.map((item: string) => network.replaceWithNetworInfo(item));
podDef.spec.command = finalCommand;
} else {
// string
podDef.spec.command = podDef.spec.command.replace(/{{ZOMBIE:(.*?)?}}/ig, (_substring: any, nodeName: string) => {
return multiAddressByNode[nodeName];
});
podDef.spec.command = network.replaceWithNetworInfo(podDef.spec.command);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/providers/native/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NativeClient, initClient } from "./nativeClient";
import { genBootnodeDef, genNodeDef, replaceMultiAddresReferences } from "./dynResourceDefinition";
import { genBootnodeDef, genNodeDef, replaceNetworkRef } from "./dynResourceDefinition";
import { setupChainSpec, getChainSpecRaw } from "./chain-spec";

export const provider = {
Expand All @@ -9,5 +9,5 @@ export const provider = {
initClient,
setupChainSpec,
getChainSpecRaw,
replaceMultiAddresReferences
replaceNetworkRef
};
14 changes: 4 additions & 10 deletions src/providers/podman/dynResourceDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { MultiAddressByNode, Node } from "../../types";
import { getRandomPort } from "../../utils/net-utils";
import { getClient } from "../client";
import { resolve } from "path";
import { Network } from "../../network";

const fs = require("fs").promises;

Expand Down Expand Up @@ -447,22 +448,15 @@ async function make_main_container(
return containerDef;
}

export function replaceMultiAddresReferences(podDef: any, multiAddressByNode: MultiAddressByNode) {
export function replaceNetworkRef(podDef: any, network: Network) {
// replace command if needed in containers
for( const container of podDef.spec.containers) {
if(Array.isArray(container.command)){
const finalCommand = container.command.map((item: string) => {
return item.replace(/{{ZOMBIE:(.*?)?}}/ig, (_substring, nodeName) => {
return multiAddressByNode[nodeName];
});
});
const finalCommand = container.command.map((item: string) => network.replaceWithNetworInfo(item))
container.command = finalCommand;
} else {
container.command = container.command.replace(/{{ZOMBIE:(.*?)?}}/ig, (_substring: any, nodeName: string) => {
return multiAddressByNode[nodeName];
});
container.command = network.replaceWithNetworInfo(container.command);
}

}
}

Expand Down
4 changes: 2 additions & 2 deletions src/providers/podman/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PodmanClient, initClient } from "./podmanClient";
import { genBootnodeDef, genNodeDef, replaceMultiAddresReferences } from "./dynResourceDefinition";
import { genBootnodeDef, genNodeDef, replaceNetworkRef } from "./dynResourceDefinition";
import { setupChainSpec, getChainSpecRaw } from "./chain-spec";

export const provider = {
Expand All @@ -9,5 +9,5 @@ export const provider = {
initClient,
setupChainSpec,
getChainSpecRaw,
replaceMultiAddresReferences,
replaceNetworkRef,
};
4 changes: 2 additions & 2 deletions src/utils/fs-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ export function readNetworkConfig(filepath: string): LaunchConfig {
const configBasePath = path.dirname(filepath);
const env = new Environment(new RelativeLoader([configBasePath]));

env.addFilter('zombie', function(nodeName){
return `{{ZOMBIE:${nodeName}}}`;
env.addFilter('zombie', function(nodeName, key){
return `{{ZOMBIE:${nodeName}:${key}}}`;
});

const temmplateContent = fs.readFileSync(filepath).toString();
Expand Down
Loading

0 comments on commit a1253d9

Please sign in to comment.