-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactored into a NodeResource for kubernetes provider
- Loading branch information
Showing
3 changed files
with
264 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
199 changes: 199 additions & 0 deletions
199
javascript/packages/orchestrator/src/providers/k8s/resources/nodeResource.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
import { genCmd, genCumulusCollatorCmd } from "../../../cmdGenerator"; | ||
import { | ||
FINISH_MAGIC_FILE, | ||
P2P_PORT, | ||
PROMETHEUS_PORT, | ||
RPC_HTTP_PORT, | ||
RPC_WS_PORT, | ||
TRANSFER_CONTAINER_NAME, | ||
TRANSFER_CONTAINER_WAIT_LOG, | ||
} from "../../../constants"; | ||
import { Node } from "../../../types"; | ||
import { | ||
Container, | ||
ContainerPort, | ||
PodSpec, | ||
Volume, | ||
VolumeMount, | ||
} from "./types"; | ||
|
||
export class NodeResource { | ||
constructor( | ||
protected readonly namespace: string, | ||
protected readonly nodeSetupConfig: Node, | ||
) {} | ||
|
||
public async generateSpec() { | ||
const volumes = await this.generateVolumes(); | ||
const volumeMounts = this.generateVolumesMounts(); | ||
const containersPorts = await this.generateContainersPorts(); | ||
const initContainers = this.generateInitContainers(); | ||
const containers = await this.generateContainers( | ||
volumeMounts, | ||
containersPorts, | ||
); | ||
|
||
return this.generatePodSpec(initContainers, containers, volumes); | ||
} | ||
|
||
private async generateVolumes(): Promise<Volume[]> { | ||
return [ | ||
{ name: "tmp-cfg" }, | ||
{ name: "tmp-data" }, | ||
{ name: "tmp-relay-data" }, | ||
]; | ||
} | ||
|
||
private generateVolumesMounts() { | ||
return [ | ||
{ name: "tmp-cfg", mountPath: "/cfg", readOnly: false }, | ||
{ name: "tmp-data", mountPath: "/data", readOnly: false }, | ||
{ name: "tmp-relay-data", mountPath: "/relay-data", readOnly: false }, | ||
]; | ||
} | ||
|
||
private async generateContainersPorts(): Promise<ContainerPort[]> { | ||
return [ | ||
{ containerPort: PROMETHEUS_PORT, name: "prometheus" }, | ||
{ containerPort: RPC_HTTP_PORT, name: "rpc-http" }, | ||
{ containerPort: RPC_WS_PORT, name: "rpc-ws" }, | ||
{ containerPort: P2P_PORT, name: "p2p" }, | ||
]; | ||
} | ||
|
||
private generateContainerCommand(): Promise<string[]> { | ||
if (this.nodeSetupConfig.zombieRole === "cumulus-collator") { | ||
return genCumulusCollatorCmd(this.nodeSetupConfig); | ||
} | ||
|
||
return genCmd(this.nodeSetupConfig); | ||
} | ||
|
||
private generateInitContainers(): Container[] { | ||
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(" && "), | ||
], | ||
}, | ||
]; | ||
} | ||
|
||
private shouldAddJaegerContainer() { | ||
const { zombieRole, jaegerUrl } = this.nodeSetupConfig; | ||
const isNodeOrCumulusCollator = | ||
zombieRole === "node" || zombieRole === "cumulus-collator"; | ||
const isJaegerUrlDefined = jaegerUrl && jaegerUrl === "localhost:6831"; | ||
|
||
return isNodeOrCumulusCollator && isJaegerUrlDefined; | ||
} | ||
|
||
private generateJaegerContainer(): Container { | ||
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" }, | ||
}, | ||
}; | ||
} | ||
|
||
private async generateContainers( | ||
volumeMounts: VolumeMount[], | ||
ports: ContainerPort[], | ||
): Promise<Container[]> { | ||
const { image, name, env, resources } = this.nodeSetupConfig; | ||
const containers: Container[] = [ | ||
{ | ||
image, | ||
name, | ||
imagePullPolicy: "Always", | ||
ports, | ||
env, | ||
volumeMounts, | ||
command: await this.generateContainerCommand(), | ||
resources: resources?.resources, | ||
}, | ||
]; | ||
|
||
if (this.shouldAddJaegerContainer()) { | ||
containers.push(this.generateJaegerContainer()); | ||
} | ||
|
||
return containers; | ||
} | ||
|
||
protected generatePodSpec( | ||
initContainers: Container[], | ||
containers: Container[], | ||
volumes: Volume[], | ||
): PodSpec { | ||
const { name, validator } = this.nodeSetupConfig; | ||
|
||
return { | ||
apiVersion: "v1", | ||
kind: "Pod", | ||
metadata: { | ||
name, | ||
labels: { | ||
"zombie-role": validator ? "authority" : "full-node", | ||
app: "zombienet", | ||
"app.kubernetes.io/name": this.namespace, | ||
"app.kubernetes.io/instance": name, | ||
"zombie-ns": this.namespace, | ||
}, | ||
annotations: { | ||
"prometheus.io/scrape": "true", | ||
"prometheus.io/port": `${PROMETHEUS_PORT}`, | ||
}, | ||
}, | ||
spec: { | ||
hostname: name, | ||
containers, | ||
initContainers, | ||
restartPolicy: "Never", | ||
volumes, | ||
securityContext: { | ||
fsGroup: 1000, | ||
runAsUser: 1000, | ||
runAsGroup: 1000, | ||
}, | ||
}, | ||
}; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
javascript/packages/orchestrator/src/providers/k8s/resources/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { envVars, Resources } from "../../../types"; | ||
|
||
type ContainerResource = Resources["resources"]; | ||
|
||
export interface VolumeMount { | ||
name: string; | ||
mountPath: string; | ||
readOnly: boolean; | ||
} | ||
|
||
export interface Volume { | ||
name: string; | ||
} | ||
|
||
export interface ContainerPort { | ||
containerPort: number; | ||
name?: "prometheus" | "rpc-http" | "rpc-ws" | "p2p"; | ||
protocol?: "UDP" | "TCP"; | ||
} | ||
|
||
export interface Container { | ||
image: string; | ||
name: string; | ||
imagePullPolicy?: "Always"; | ||
volumeMounts?: VolumeMount[]; | ||
ports?: ContainerPort[]; | ||
command: string[]; | ||
args?: string[]; | ||
env?: envVars[]; | ||
resources?: ContainerResource; | ||
} | ||
|
||
export interface PodSpec { | ||
apiVersion: "v1"; | ||
kind: "Pod"; | ||
metadata: { | ||
name: string; | ||
labels: { | ||
"zombie-role": string; | ||
app: string; | ||
"app.kubernetes.io/name": string; | ||
"app.kubernetes.io/instance": string; | ||
"zombie-ns": string; | ||
}; | ||
annotations?: { | ||
"prometheus.io/scrape": "true"; | ||
"prometheus.io/port": string; | ||
}; | ||
}; | ||
spec: { | ||
hostname: string; | ||
restartPolicy: "Never" | "OnFailure"; | ||
containers: Container[]; | ||
initContainers?: Container[]; | ||
volumes?: Volume[]; | ||
securityContext?: { | ||
fsGroup: number; | ||
runAsUser: number; | ||
runAsGroup: number; | ||
}; | ||
}; | ||
} |