Minor Adjustments

This commit is contained in:
Dunemask 2023-03-19 13:53:37 -04:00 committed by dunemask
parent ba8e6ded26
commit a90c28dd76
60 changed files with 8240 additions and 8 deletions

View file

@ -0,0 +1,58 @@
import fs from "node:fs";
import { URL } from "node:url";
import path from "node:path";
const {
QUALITEER_EXECUTOR_URL,
QUALITEER_EXECUTOR_USE_SCRIPT,
QUALITEER_EXECUTOR_BIN,
QUALITEER_EXECUTOR_BIN_URL,
} = process.env;
const executorUrl = QUALITEER_EXECUTOR_URL;
const executorAsScript = QUALITEER_EXECUTOR_USE_SCRIPT === "true";
const executorBin = QUALITEER_EXECUTOR_BIN ?? `qltr-executor`;
const executorBinFetchUrl = QUALITEER_EXECUTOR_BIN_URL;
const jobsDir = "jobs/";
const jobsPath = path.resolve(jobsDir);
const k8sFolder = new URL(".", import.meta.url).pathname;
const defaultsFilePath = path.resolve(k8sFolder, "k8s-job.json");
const defaults = JSON.parse(fs.readFileSync(defaultsFilePath));
function commandBuilder(jobId, jobRequest) {
const executorPayload = JSON.stringify({
jobId,
jobRequest,
url: executorUrl,
});
const payload = Buffer.from(executorPayload, "utf8").toString("base64");
return [`./${executorBin}`, payload];
}
export function jobBuilder(jobRequest) {
const { resources, name, image, id: jobId } = jobRequest;
// Safety Checks
if (!jobId) throw Error("'jobId' required!");
if (!image) throw Error("'image' required!");
// Apply configuration
const job = { ...defaults };
job.metadata.name = `qltr-${jobId}`;
const container = job.spec.template.spec.containers[0];
container.name = job.metadata.name;
container.command = commandBuilder(jobId, jobRequest);
container.image = JSON.stringify(image);
// Apply resources
job.resources = { ...job.resources, ...resources };
return job;
}
export const createFile = (job) => {
const { name } = job.metadata;
if (!fs.existsSync(jobsPath)) fs.mkdirSync(jobsPath);
const filePath = path.resolve(jobsDir, `${name}.json`);
fs.writeFileSync(filePath, JSON.stringify(job));
return filePath;
};
export const deleteFile = (filePath) => fs.unlinkSync(filePath);

View 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);
});

View file

@ -0,0 +1,20 @@
import cp from "node:child_process";
import fs from "node:fs";
import path from "node:path";
import { jobBuilder, createFile, deleteFile } from "./k8s-common.js";
// Constants
const internalEngine = path.resolve("./lib/jobs/k8s/k8s-internal-engine.js");
// Functions
const applyFileInternally = (filePath) => {
const job = fs.readFileSync(filePath, { encoding: "utf8" });
cp.fork(internalEngine, [job]);
};
export default async function createJobInternally(jobRequest) {
const job = jobBuilder(jobRequest);
const filePath = createFile(job);
applyFileInternally(filePath);
deleteFile(filePath);
}

View file

@ -0,0 +1,34 @@
{
"apiVersion": "batch/v1",
"kind": "Job",
"metadata": {
"name": "qltr-job-test-suite-1"
},
"spec": {
"ttlSecondsAfterFinished": 2,
"template": {
"spec": {
"containers": [
{
"name": "qltr-job-test-suite-1",
"image": "node:latest",
"imagePullPolicy": "Always",
"command": ["node", "--version"],
"envFrom": [
{
"configMapRef": {
"name": "qualiteer-job-environment"
}
}
]
}
],
"imagePullSecrets": [
{ "name": "usw-registry-secret", "namespace": "default" }
],
"restartPolicy": "Never"
}
},
"backoffLimit": 4
}
}

19
libold/server/k8s/k8s.js Normal file
View file

@ -0,0 +1,19 @@
import k8s from "@kubernetes/client-node";
import { INFO, ERR } from "../util/logging.js";
import { jobBuilder, createFile, deleteFile } from "./k8s-common.js";
export default async function createJob(jobRequest) {
//console.log(await jobRequest.tests);
const job = jobBuilder(jobRequest);
job.spec.template.spec.containers[0].image =
"registry.dunemask.net/garden/dev/reed:latest";
const kc = new k8s.KubeConfig();
kc.loadFromCluster();
const batchV1Api = kc.makeApiClient(k8s.BatchV1Api);
const batchV1beta1Api = kc.makeApiClient(k8s.BatchV1beta1Api);
const jobName = job.metadata.name;
batchV1Api
.createNamespacedJob("dunestorm-dunemask", job)
.then((res) => INFO("K8S", `Job ${jobName} created!`))
.catch((err) => ERR("K8S", err));
}