Improve k8s logging accuracy

pull/496/head
Frostebite 2023-02-02 22:16:55 +00:00
parent ac9288231e
commit 7f14855a64
3 changed files with 32 additions and 5 deletions

18
dist/index.js generated vendored
View File

@ -3312,6 +3312,13 @@ class Kubernetes {
// eslint-disable-next-line no-constant-condition // eslint-disable-next-line no-constant-condition
while (true) { while (true) {
try { try {
const existsAlready = yield this.doesJobExist(this.jobName);
if (!existsAlready) {
cloud_runner_logger_1.default.log('Job does not exist');
yield this.createNamespacedJob(commands, image, mountdir, workingdir, environment, secrets);
cloud_runner_logger_1.default.log('Watching pod until running');
yield kubernetes_task_runner_1.default.watchUntilPodRunning(this.kubeClient, this.podName, this.namespace);
}
cloud_runner_logger_1.default.log('Pod running, streaming logs'); cloud_runner_logger_1.default.log('Pod running, streaming logs');
output = yield kubernetes_task_runner_1.default.runTask(this.kubeConfig, this.kubeClient, this.jobName, this.podName, 'main', this.namespace); output = yield kubernetes_task_runner_1.default.runTask(this.kubeConfig, this.kubeClient, this.jobName, this.podName, 'main', this.namespace);
const running = yield kubernetes_pods_1.default.IsPodRunning(this.podName, this.namespace, this.kubeClient); const running = yield kubernetes_pods_1.default.IsPodRunning(this.podName, this.namespace, this.kubeClient);
@ -3360,17 +3367,24 @@ class Kubernetes {
} }
}); });
} }
doesJobExist(name) {
return __awaiter(this, void 0, void 0, function* () {
const jobs = yield this.kubeClientBatch.listNamespacedJob(this.namespace);
return jobs.body.items.some((x) => { var _a; return ((_a = x.metadata) === null || _a === void 0 ? void 0 : _a.name) === name; });
});
}
createNamespacedJob(commands, image, mountdir, workingdir, environment, secrets) { createNamespacedJob(commands, image, mountdir, workingdir, environment, secrets) {
var _a;
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
for (let index = 0; index < 3; index++) { for (let index = 0; index < 3; index++) {
try { try {
const jobSpec = kubernetes_job_spec_factory_1.default.getJobSpec(commands, image, mountdir, workingdir, environment, secrets, this.buildGuid, this.buildParameters, this.secretName, this.pvcName, this.jobName, k8s); const jobSpec = kubernetes_job_spec_factory_1.default.getJobSpec(commands, image, mountdir, workingdir, environment, secrets, this.buildGuid, this.buildParameters, this.secretName, this.pvcName, this.jobName, k8s);
yield new Promise((promise) => setTimeout(promise, 15000)); yield new Promise((promise) => setTimeout(promise, 15000));
yield this.kubeClientBatch.createNamespacedJob(this.namespace, jobSpec); const result = yield this.kubeClientBatch.createNamespacedJob(this.namespace, jobSpec);
cloud_runner_logger_1.default.log(`Build job created`); cloud_runner_logger_1.default.log(`Build job created`);
yield new Promise((promise) => setTimeout(promise, 5000)); yield new Promise((promise) => setTimeout(promise, 5000));
cloud_runner_logger_1.default.log('Job created'); cloud_runner_logger_1.default.log('Job created');
return; return (_a = result.body.metadata) === null || _a === void 0 ? void 0 : _a.name;
} }
catch (error) { catch (error) {
cloud_runner_logger_1.default.log(`Error occured creating job: ${error}`); cloud_runner_logger_1.default.log(`Error occured creating job: ${error}`);

2
dist/index.js.map generated vendored

File diff suppressed because one or more lines are too long

View File

@ -137,6 +137,13 @@ class Kubernetes implements ProviderInterface {
// eslint-disable-next-line no-constant-condition // eslint-disable-next-line no-constant-condition
while (true) { while (true) {
try { try {
const existsAlready = await this.doesJobExist(this.jobName);
if (!existsAlready) {
CloudRunnerLogger.log('Job does not exist');
await this.createNamespacedJob(commands, image, mountdir, workingdir, environment, secrets);
CloudRunnerLogger.log('Watching pod until running');
await KubernetesTaskRunner.watchUntilPodRunning(this.kubeClient, this.podName, this.namespace);
}
CloudRunnerLogger.log('Pod running, streaming logs'); CloudRunnerLogger.log('Pod running, streaming logs');
output = await KubernetesTaskRunner.runTask( output = await KubernetesTaskRunner.runTask(
this.kubeConfig, this.kubeConfig,
@ -193,6 +200,12 @@ class Kubernetes implements ProviderInterface {
} }
} }
private async doesJobExist(name) {
const jobs = await this.kubeClientBatch.listNamespacedJob(this.namespace);
return jobs.body.items.some((x) => x.metadata?.name === name);
}
private async createNamespacedJob( private async createNamespacedJob(
commands: string, commands: string,
image: string, image: string,
@ -218,12 +231,12 @@ class Kubernetes implements ProviderInterface {
k8s, k8s,
); );
await new Promise((promise) => setTimeout(promise, 15000)); await new Promise((promise) => setTimeout(promise, 15000));
await this.kubeClientBatch.createNamespacedJob(this.namespace, jobSpec); const result = await this.kubeClientBatch.createNamespacedJob(this.namespace, jobSpec);
CloudRunnerLogger.log(`Build job created`); CloudRunnerLogger.log(`Build job created`);
await new Promise((promise) => setTimeout(promise, 5000)); await new Promise((promise) => setTimeout(promise, 5000));
CloudRunnerLogger.log('Job created'); CloudRunnerLogger.log('Job created');
return; return result.body.metadata?.name;
} catch (error) { } catch (error) {
CloudRunnerLogger.log(`Error occured creating job: ${error}`); CloudRunnerLogger.log(`Error occured creating job: ${error}`);
throw error; throw error;