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

Refacto/kubernetes resources #904

Merged
merged 2 commits into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -1,222 +1,24 @@
import { getRandomPort, getSha256 } from "@zombienet/utils";
import { genCmd, genCumulusCollatorCmd } from "../../cmdGenerator";
import { getUniqueName } from "../../configGenerator";
import {
FINISH_MAGIC_FILE,
P2P_PORT,
PROMETHEUS_PORT,
RPC_HTTP_PORT,
RPC_WS_PORT,
TMP_DONE,
TRANSFER_CONTAINER_NAME,
TRANSFER_CONTAINER_WAIT_LOG,
WAIT_UNTIL_SCRIPT_SUFIX,
} from "../../constants";
import { TMP_DONE, WAIT_UNTIL_SCRIPT_SUFIX } from "../../constants";
import { Network } from "../../network";
import { Node } from "../../types";
import { BootNodeResource, NodeResource } from "./resources";

export async function genBootnodeDef(
namespace: string,
nodeSetup: Node,
): Promise<any> {
const [volume_mounts, devices] = make_volume_mounts();
const container = await make_main_container(nodeSetup, volume_mounts);
const transferContainter = make_transfer_containter();
return {
apiVersion: "v1",
kind: "Pod",
metadata: {
name: "bootnode",
labels: {
"app.kubernetes.io/name": namespace,
"app.kubernetes.io/instance": "bootnode",
"zombie-role": "bootnode",
app: "zombienet",
},
},
spec: {
hostname: "bootnode",
containers: [container],
initContainers: [transferContainter],
restartPolicy: "Never",
volumes: devices,
securityContext: {
fsGroup: 1000,
runAsUser: 1000,
runAsGroup: 1000,
},
},
};
const bootNodeResource = new BootNodeResource(namespace, nodeSetup);
return bootNodeResource.generateSpec();
}

export async function genNodeDef(
namespace: string,
nodeSetup: Node,
): Promise<any> {
const [volume_mounts, devices] = make_volume_mounts();
const container = await make_main_container(nodeSetup, volume_mounts);
const transferContainter = make_transfer_containter();

const containersToRun = [container];
if (
(nodeSetup.zombieRole === "node" ||
nodeSetup.zombieRole === "cumulus-collator") &&
nodeSetup.jaegerUrl &&
nodeSetup.jaegerUrl === "localhost:6831"
) {
// add sidecar
containersToRun.push(jaegerAgentDef());
}

return {
apiVersion: "v1",
kind: "Pod",
metadata: {
name: nodeSetup.name,
labels: {
"zombie-role": nodeSetup.validator ? "authority" : "full-node",
app: "zombienet",
"app.kubernetes.io/name": namespace,
"app.kubernetes.io/instance": nodeSetup.name,
},
annotations: {
"prometheus.io/scrape": "true",
"prometheus.io/port": PROMETHEUS_PORT + "", //force string
},
},
spec: {
hostname: nodeSetup.name,
containers: containersToRun,
initContainers: [transferContainter],
restartPolicy: "Never",
volumes: devices,
securityContext: {
fsGroup: 1000,
runAsUser: 1000,
runAsGroup: 1000,
},
},
};
}

function make_transfer_containter(): any {
return {
name: TRANSFER_CONTAINER_NAME,
image: "docker.io/alpine",
imagePullPolicy: "Always",
volumeMounts: [
{ name: "tmp-cfg", mountPath: "/cfg", readOnly: false },
{ name: "tmp-data", mountPath: "/data", readOnly: false },
{ name: "tmp-relay-data", mountPath: "/relay-data", readOnly: false },
],
command: [
"ash",
"-c",
[
"wget github.com/moparisthebest/static-curl/releases/download/v7.83.1/curl-amd64 -O /cfg/curl",
"echo downloaded",
"chmod +x /cfg/curl",
"echo chmoded",
"wget github.com/uutils/coreutils/releases/download/0.0.17/coreutils-0.0.17-x86_64-unknown-linux-musl.tar.gz -O /cfg/coreutils-0.0.17-x86_64-unknown-linux-musl.tar.gz",
"cd /cfg",
"tar -xvzf ./coreutils-0.0.17-x86_64-unknown-linux-musl.tar.gz",
"cp ./coreutils-0.0.17-x86_64-unknown-linux-musl/coreutils /cfg/coreutils",
"chmod +x /cfg/coreutils",
"rm -rf ./coreutils-0.0.17-x86_64-unknown-linux-musl",
"echo coreutils downloaded",
`until [ -f ${FINISH_MAGIC_FILE} ]; do echo ${TRANSFER_CONTAINER_WAIT_LOG}; sleep 1; done; echo copy files has finished`,
].join(" && "),
],
};
}

function make_volume_mounts(): [any, any] {
const volume_mounts = [
{ name: "tmp-cfg", mountPath: "/cfg", readOnly: false },
{ name: "tmp-data", mountPath: "/data", readOnly: false },
{ name: "tmp-relay-data", mountPath: "/relay-data", readOnly: false },
];

const devices = [
{ name: "tmp-cfg" },
{ name: "tmp-data" },
{ name: "tmp-relay-data" },
];

return [volume_mounts, devices];
}

async function make_main_container(
nodeSetup: Node,
volume_mounts: any[],
): Promise<any> {
const ports = [
{ containerPort: PROMETHEUS_PORT, name: "prometheus" },
{ containerPort: RPC_HTTP_PORT, name: "rpc-http" },
{ containerPort: RPC_WS_PORT, name: "rpc-ws" },
{ containerPort: P2P_PORT, name: "p2p" },
];

let computedCommand;
if (nodeSetup.zombieRole === "cumulus-collator") {
computedCommand = await genCumulusCollatorCmd(nodeSetup);
} else {
computedCommand = await genCmd(nodeSetup);
}

const containerDef: any = {
image: nodeSetup.image,
name: nodeSetup.name,
imagePullPolicy: "Always",
ports,
env: nodeSetup.env,
volumeMounts: volume_mounts,
command: computedCommand,
};

if (nodeSetup.resources) containerDef.resources = nodeSetup.resources;

return containerDef;
}

function jaegerAgentDef() {
return {
name: "jaeger-agent",
image: "jaegertracing/jaeger-agent:1.28.0",
ports: [
{
containerPort: 5775,
protocol: "UDP",
},
{
containerPort: 5778,
protocol: "TCP",
},
{
containerPort: 6831,
protocol: "UDP",
},
{
containerPort: 6832,
protocol: "UDP",
},
],
command: [
"/go/bin/agent-linux",
"--reporter.type=grpc",
"--reporter.grpc.host-port=tempo-tempo-distributed-distributor.tempo.svc.cluster.local:14250",
],
resources: {
limits: {
memory: "50M",
cpu: "100m",
},
requests: {
memory: "50M",
cpu: "100m",
},
},
};
const nodeResource = new NodeResource(namespace, nodeSetup);
return nodeResource.generateSpec();
}

export function replaceNetworkRef(podDef: any, network: Network) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Node } from "../../../types";
import { NodeResource } from "./nodeResource";
import { Container, PodSpec, Volume } from "./types";

export class BootNodeResource extends NodeResource {
constructor(namespace: string, nodeSetupConfig: Node) {
super(namespace, nodeSetupConfig);
}

protected generatePodSpec(
initContainers: Container[],
containers: Container[],
volumes: Volume[],
): PodSpec {
return {
apiVersion: "v1",
kind: "Pod",
metadata: {
name: "bootnode",
labels: {
"app.kubernetes.io/name": this.namespace,
"app.kubernetes.io/instance": "bootnode",
"zombie-role": "bootnode",
app: "zombienet",
"zombie-ns": this.namespace,
},
},
spec: {
hostname: "bootnode",
containers,
initContainers,
restartPolicy: "Never",
volumes,
securityContext: {
fsGroup: 1000,
runAsUser: 1000,
runAsGroup: 1000,
},
},
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { BootNodeResource } from "./bootnodeResource";
export { NodeResource } from "./nodeResource";
Loading