Fixed gitignore
This commit is contained in:
parent
338000684b
commit
61072ee032
16 changed files with 721 additions and 1 deletions
14
lib/jobs/k8s/k8s-bypass.js
Normal file
14
lib/jobs/k8s/k8s-bypass.js
Normal file
|
@ -0,0 +1,14 @@
|
|||
import { INFO, ERR, OK, VERB } from "../../util/logging.js";
|
||||
import cp from "node:child_process";
|
||||
|
||||
const jobStr = process.argv.slice(2)[0];
|
||||
const job = JSON.parse(jobStr);
|
||||
const { command } = job.spec.template.spec.containers[0];
|
||||
INFO("EXEC", "Internal Executor Starting!");
|
||||
cp.exec(command, (error, stdout, stderr) => {
|
||||
if (error) ERR("EXEC", error);
|
||||
//if(stdout) VERB("EXEC-STDOUT", stdout);
|
||||
//if(stderr) VERB("EXEC-STDERR", stderr);
|
||||
OK("EXEC", "Internal Executor Finished!");
|
||||
process.exit(error ? 1 : 0);
|
||||
});
|
33
lib/jobs/k8s/k8s-job.json
Normal file
33
lib/jobs/k8s/k8s-job.json
Normal file
|
@ -0,0 +1,33 @@
|
|||
{
|
||||
"apiVersion": "batch/v1",
|
||||
"kind": "Job",
|
||||
"metadata": {
|
||||
"name": "qltr-job-test-suite-1"
|
||||
},
|
||||
"spec": {
|
||||
"template": {
|
||||
"spec": {
|
||||
"containers": [
|
||||
{
|
||||
"resources": {
|
||||
"requests": {
|
||||
"memory": "64MI",
|
||||
"cpu": "250m"
|
||||
},
|
||||
"limits": {
|
||||
"memory": "128MI",
|
||||
"cpu": "500m"
|
||||
}
|
||||
},
|
||||
"name": "qltr-job-test-suite-1",
|
||||
"image": "node",
|
||||
"imagePullPolicy": "Always",
|
||||
"command": ["node", "--version"]
|
||||
}
|
||||
],
|
||||
"restartPolicy": "Never"
|
||||
}
|
||||
},
|
||||
"backoffLimit": 4
|
||||
}
|
||||
}
|
84
lib/jobs/k8s/kubernetes.js
Normal file
84
lib/jobs/k8s/kubernetes.js
Normal file
|
@ -0,0 +1,84 @@
|
|||
import cp from "child_process";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
|
||||
const internalDeploy = process.env.INTERNAL_DEPLOY === "true";
|
||||
const executorUrl = process.env.EXECUTOR_URL;
|
||||
const executorScriptOnly = process.env.EXECUTOR_SCRIPT_ONLY === "true";
|
||||
const executorBin =
|
||||
process.env.EXECUTOR_BIN ?? `qltr-executor${executorScriptOnly ? ".js" : ""}`;
|
||||
|
||||
const qualiteerUrl =
|
||||
process.env.QUALITEER_URL ?? "file:///home/runner/Qualiteer/bin/executor";
|
||||
|
||||
const kubCmd = "kubectl apply -f";
|
||||
const jobsDir = "jobs/";
|
||||
const defaults = JSON.parse(
|
||||
fs.readFileSync(path.resolve("./lib/jobs/k8s/k8s-job.json"))
|
||||
);
|
||||
|
||||
const wrapCommand = (jobId, command) => {
|
||||
const bin = executorScriptOnly
|
||||
? `node ${executorBin}`
|
||||
: `chmod +x ${executorBin} && ./${executorBin}`;
|
||||
const cmd = command.map((arg) => JSON.stringify(arg));
|
||||
const curlCmd = `curl -o qltr-executor ${executorUrl} && ${bin} ${qualiteerUrl} ${jobId} ${cmd.join(
|
||||
" "
|
||||
)}`;
|
||||
return curlCmd;
|
||||
};
|
||||
|
||||
const createFile = (job) => {
|
||||
const { name } = job.metadata;
|
||||
const jobsPath = path.resolve(jobsDir);
|
||||
if (!fs.existsSync(jobsPath)) fs.mkdirSync(jobsPath);
|
||||
const filePath = path.resolve(jobsDir, `${name}.json`);
|
||||
fs.writeFileSync(filePath, JSON.stringify(job));
|
||||
return filePath;
|
||||
};
|
||||
|
||||
const applyFileInternally = (filePath) => {
|
||||
const job = fs.readFileSync(filePath, { encoding: "utf8" });
|
||||
cp.fork(path.resolve("./lib/jobs/k8s/k8s-bypass.js"), [job]);
|
||||
};
|
||||
|
||||
const applyFile = async (filePath) => {
|
||||
const command = `${kubCmd} ${filePath}`;
|
||||
return new Promise((res, rej) =>
|
||||
cp.exec(command, (err, stdout, stderr) => (err && rej(err)) || res(stdout))
|
||||
);
|
||||
};
|
||||
|
||||
const deleteFile = (filePath) => fs.unlinkSync(filePath);
|
||||
|
||||
const jobBuilder = (jobRequest) => {
|
||||
const { resources, name, image, command, id: jobId } = jobRequest;
|
||||
|
||||
// Safety Checks
|
||||
if (!jobId) throw Error("'jobId' required!");
|
||||
if (!name) throw Error("'name' required!");
|
||||
if (!command) throw Error("'command' required!");
|
||||
if (!image) throw Error("'image' required!");
|
||||
|
||||
if (!Array.isArray(command)) throw Error("'command' must be an array!");
|
||||
|
||||
// Apply configuration
|
||||
const job = { ...defaults };
|
||||
job.metadata.name = `qltr-${name}-${jobId}`;
|
||||
const container = job.spec.template.spec.containers[0];
|
||||
container.name = job.metadata.name;
|
||||
container.command = wrapCommand(jobId, command);
|
||||
container.image = JSON.stringify(image);
|
||||
|
||||
// Apply resources
|
||||
job.resources = { ...job.resources, ...resources };
|
||||
return job;
|
||||
};
|
||||
|
||||
export default async function createJob(jobRequest) {
|
||||
const job = jobBuilder(jobRequest);
|
||||
const filePath = createFile(job);
|
||||
if (!internalDeploy) await applyFile(filePath);
|
||||
else await applyFileInternally(filePath);
|
||||
deleteFile(filePath);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue