Log stream attempt #2

pull/273/head
Frostebite 2021-06-06 03:01:49 +01:00
parent 926e500b89
commit 464e5f0caa
3 changed files with 120 additions and 175 deletions

48
dist/index.js vendored
View File

@ -729,7 +729,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
});
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
// @ts-ignore
const k8s = __importStar(__webpack_require__(89679));
const core = __importStar(__webpack_require__(42186));
const base64 = __webpack_require__(85848);
@ -743,11 +742,6 @@ class Kubernetes {
const k8sApi = kc.makeApiClient(k8s.CoreV1Api);
const k8sBatchApi = kc.makeApiClient(k8s.BatchV1Api);
core.info('loaded from default');
// const kubeconfig = new KubeConfig();
// kubeconfig.loadFromString(base64.decode(buildParameters.kubeConfig));
// const backend = new Request({ kubeconfig });
// const kubeClient = new Client(backend);
// await kubeClient.loadSpec();
const buildId = Kubernetes.uuidv4();
const pvcName = `unity-builder-pvc-${buildId}`;
const secretName = `build-credentials-${buildId}`;
@ -1028,12 +1022,12 @@ class Kubernetes {
});
}
static watchBuildJobUntilFinished() {
var _a, _b;
var _a, _b, _c, _d;
return __awaiter(this, void 0, void 0, function* () {
try {
const pod = yield Kubernetes.watchPodUntilRunningAndRead();
core.info(`Watching build job ${(_a = pod === null || pod === void 0 ? void 0 : pod.metadata) === null || _a === void 0 ? void 0 : _a.name}`);
yield Kubernetes.streamLogs(((_b = pod === null || pod === void 0 ? void 0 : pod.metadata) === null || _b === void 0 ? void 0 : _b.name) || '', this.namespace);
yield Kubernetes.streamLogs(((_b = pod === null || pod === void 0 ? void 0 : pod.metadata) === null || _b === void 0 ? void 0 : _b.name) || '', this.namespace, ((_d = (_c = pod === null || pod === void 0 ? void 0 : pod.status) === null || _c === void 0 ? void 0 : _c.containerStatuses) === null || _d === void 0 ? void 0 : _d[0].name) || '');
}
catch (error) {
core.error('Failed while watching build job');
@ -1041,45 +1035,21 @@ class Kubernetes {
}
});
}
static streamLogs(name, namespace) {
var _a, _b;
static streamLogs(name, namespace, container) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
try {
let running = true;
let mostRecentLogTime = 999;
let mostRecentLine = '';
while (running) {
const pod = yield this.kubeClient.readNamespacedPod(name, namespace);
running = ((_a = pod.body.status) === null || _a === void 0 ? void 0 : _a.phase) === 'Running';
yield new Promise((resolve) => setTimeout(resolve, pollInterval));
core.info('Polling logs...');
let logs;
try {
logs = yield this.kubeClient.readNamespacedPodLog(name, namespace, undefined, undefined, undefined, undefined, undefined, undefined, mostRecentLogTime, undefined, true);
}
catch (error) {
core.info(error);
if (error.message === 'HTTP request failed') {
core.info('!warning - K8S HTTP FAILED');
continue;
}
}
const arrayOfLines = (_b = logs === null || logs === void 0 ? void 0 : logs.body.match(/[^\n\r]+/g)) === null || _b === void 0 ? void 0 : _b.reverse();
if (arrayOfLines) {
for (const element of arrayOfLines) {
const [time, ...line] = element.split(' ');
const lineString = line.join(' ');
const lineDate = Date.parse(time);
if (mostRecentLine !== lineString || lineDate > mostRecentLogTime) {
core.info(lineString);
mostRecentLogTime = lineDate;
mostRecentLine = lineString;
}
else {
break;
}
}
}
const logs = yield this.kubeClient.readNamespacedPodLog(name, namespace, container, true, undefined, undefined, undefined, undefined, undefined, undefined, true);
logs.response.on('data', (data) => {
core.info('LOGS RECEIVED');
core.info(data);
});
}
}
catch (error) {

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,3 @@
// @ts-ignore
import * as k8s from '@kubernetes/client-node';
import { BuildParameters } from '.';
import * as core from '@actions/core';
@ -25,12 +24,6 @@ class Kubernetes {
const k8sBatchApi = kc.makeApiClient(k8s.BatchV1Api);
core.info('loaded from default');
// const kubeconfig = new KubeConfig();
// kubeconfig.loadFromString(base64.decode(buildParameters.kubeConfig));
// const backend = new Request({ kubeconfig });
// const kubeClient = new Client(backend);
// await kubeClient.loadSpec();
const buildId = Kubernetes.uuidv4();
const pvcName = `unity-builder-pvc-${buildId}`;
const secretName = `build-credentials-${buildId}`;
@ -317,60 +310,42 @@ class Kubernetes {
try {
const pod = await Kubernetes.watchPodUntilRunningAndRead();
core.info(`Watching build job ${pod?.metadata?.name}`);
await Kubernetes.streamLogs(pod?.metadata?.name || '', this.namespace);
await Kubernetes.streamLogs(
pod?.metadata?.name || '',
this.namespace,
pod?.status?.containerStatuses?.[0].name || '',
);
} catch (error) {
core.error('Failed while watching build job');
throw error;
}
}
static async streamLogs(name: string, namespace: string) {
static async streamLogs(name: string, namespace: string, container: string) {
try {
let running = true;
let mostRecentLogTime: number = 999;
let mostRecentLine: string = '';
while (running) {
const pod = await this.kubeClient.readNamespacedPod(name, namespace);
running = pod.body.status?.phase === 'Running';
await new Promise((resolve) => setTimeout(resolve, pollInterval));
core.info('Polling logs...');
let logs;
try {
logs = await this.kubeClient.readNamespacedPodLog(
name,
namespace,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
mostRecentLogTime,
undefined,
true,
);
} catch (error) {
core.info(error);
if (error.message === 'HTTP request failed') {
core.info('!warning - K8S HTTP FAILED');
continue;
}
}
const arrayOfLines = logs?.body.match(/[^\n\r]+/g)?.reverse();
if (arrayOfLines) {
for (const element of arrayOfLines) {
const [time, ...line] = element.split(' ');
const lineString: string = line.join(' ');
const lineDate: number = Date.parse(time);
if (mostRecentLine !== lineString || lineDate > mostRecentLogTime) {
core.info(lineString);
mostRecentLogTime = lineDate;
mostRecentLine = lineString;
} else {
break;
}
}
}
const logs = await this.kubeClient.readNamespacedPodLog(
name,
namespace,
container,
true,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
true,
);
logs.response.on('data', (data) => {
core.info('LOGS RECEIVED');
core.info(data);
});
}
} catch (error) {
core.error(JSON.stringify(error, undefined, 4));