Interrupt k8s logs when logs found

This commit is contained in:
Frostebite 2023-10-02 23:58:34 +01:00
parent 9cb82940fb
commit 4b072164b5
3 changed files with 14 additions and 22 deletions

BIN
dist/index.js generated vendored

Binary file not shown.

BIN
dist/index.js.map generated vendored

Binary file not shown.

View File

@ -32,7 +32,6 @@ class KubernetesTaskRunner {
? ` -f -c ${containerName}` ? ` -f -c ${containerName}`
: ` --previous`; : ` --previous`;
let logs;
const callback = (outputChunk: string) => { const callback = (outputChunk: string) => {
output += outputChunk; output += outputChunk;
@ -47,15 +46,28 @@ class KubernetesTaskRunner {
CloudRunnerLogger.log(`Loghash found`); CloudRunnerLogger.log(`Loghash found`);
} }
if (chunk.includes(`LOGS:`)) { if (chunk.includes(`LOGS:`)) {
CloudRunnerLogger.log(`LOGS: found`);
// remove "LOGS: " and decode base64 remaining // remove "LOGS: " and decode base64 remaining
const unpacked = Buffer.from(chunk.split(`LOGS: `)[1], 'base64').toString('ascii'); const unpacked = Buffer.from(chunk.split(`LOGS: `)[1], 'base64').toString('ascii');
const result = RemoteClientLogger.HandleLogFull(unpacked); const result = RemoteClientLogger.HandleLogFull(unpacked);
CloudRunnerLogger.log(`Logs found HandleLogChunkLineResult:${result}`); CloudRunnerLogger.log(`Logs found HandleLogChunkLineResult:${result}`);
if (result) {
FollowLogStreamService.DidReceiveEndOfTransmission = true;
} }
return;
}
({ shouldReadLogs, shouldCleanup, output } = FollowLogStreamService.handleIteration(
chunk,
shouldReadLogs,
shouldCleanup,
output,
));
} }
}; };
try { try {
logs = await CloudRunnerSystem.Run(`kubectl logs ${podName}${extraFlags}`, false, true, callback); await CloudRunnerSystem.Run(`kubectl logs ${podName}${extraFlags}`, false, true, callback);
} catch (error: any) { } catch (error: any) {
await new Promise((resolve) => setTimeout(resolve, 3000)); await new Promise((resolve) => setTimeout(resolve, 3000));
const continueStreaming = await KubernetesPods.IsPodRunning(podName, namespace, kubeClient); const continueStreaming = await KubernetesPods.IsPodRunning(podName, namespace, kubeClient);
@ -70,26 +82,6 @@ class KubernetesTaskRunner {
} }
throw error; throw error;
} }
const splitLogs = logs.split(`\n`);
for (const chunk of splitLogs) {
const message = CloudRunner.buildParameters.cloudRunnerDebug ? chunk : chunk.split(`Z `)[1];
// if line contains "LOGS: " then stop
if (message.includes(`LOGS:`)) {
CloudRunnerLogger.log(`LOGS: found`);
continue;
}
({ shouldReadLogs, shouldCleanup, output } = FollowLogStreamService.handleIteration(
message,
shouldReadLogs,
shouldCleanup,
output,
));
const result = RemoteClientLogger.HandleLog(message);
if (result) {
FollowLogStreamService.DidReceiveEndOfTransmission = true;
}
}
if (FollowLogStreamService.DidReceiveEndOfTransmission) { if (FollowLogStreamService.DidReceiveEndOfTransmission) {
CloudRunnerLogger.log('end of log stream'); CloudRunnerLogger.log('end of log stream');
break; break;