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 })); Object.defineProperty(exports, "__esModule", ({ value: true }));
// @ts-ignore
const k8s = __importStar(__webpack_require__(89679)); const k8s = __importStar(__webpack_require__(89679));
const core = __importStar(__webpack_require__(42186)); const core = __importStar(__webpack_require__(42186));
const base64 = __webpack_require__(85848); const base64 = __webpack_require__(85848);
@ -743,11 +742,6 @@ class Kubernetes {
const k8sApi = kc.makeApiClient(k8s.CoreV1Api); const k8sApi = kc.makeApiClient(k8s.CoreV1Api);
const k8sBatchApi = kc.makeApiClient(k8s.BatchV1Api); const k8sBatchApi = kc.makeApiClient(k8s.BatchV1Api);
core.info('loaded from default'); 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 buildId = Kubernetes.uuidv4();
const pvcName = `unity-builder-pvc-${buildId}`; const pvcName = `unity-builder-pvc-${buildId}`;
const secretName = `build-credentials-${buildId}`; const secretName = `build-credentials-${buildId}`;
@ -1028,12 +1022,12 @@ class Kubernetes {
}); });
} }
static watchBuildJobUntilFinished() { static watchBuildJobUntilFinished() {
var _a, _b; var _a, _b, _c, _d;
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
try { try {
const pod = yield Kubernetes.watchPodUntilRunningAndRead(); 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}`); 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) { catch (error) {
core.error('Failed while watching build job'); core.error('Failed while watching build job');
@ -1041,45 +1035,21 @@ class Kubernetes {
} }
}); });
} }
static streamLogs(name, namespace) { static streamLogs(name, namespace, container) {
var _a, _b; var _a;
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
try { try {
let running = true; let running = true;
let mostRecentLogTime = 999;
let mostRecentLine = '';
while (running) { while (running) {
const pod = yield this.kubeClient.readNamespacedPod(name, namespace); const pod = yield this.kubeClient.readNamespacedPod(name, namespace);
running = ((_a = pod.body.status) === null || _a === void 0 ? void 0 : _a.phase) === 'Running'; running = ((_a = pod.body.status) === null || _a === void 0 ? void 0 : _a.phase) === 'Running';
yield new Promise((resolve) => setTimeout(resolve, pollInterval)); yield new Promise((resolve) => setTimeout(resolve, pollInterval));
core.info('Polling logs...'); core.info('Polling logs...');
let logs; const logs = yield this.kubeClient.readNamespacedPodLog(name, namespace, container, true, undefined, undefined, undefined, undefined, undefined, undefined, true);
try { logs.response.on('data', (data) => {
logs = yield this.kubeClient.readNamespacedPodLog(name, namespace, undefined, undefined, undefined, undefined, undefined, undefined, mostRecentLogTime, undefined, true); core.info('LOGS RECEIVED');
} core.info(data);
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;
}
}
}
} }
} }
catch (error) { 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 * as k8s from '@kubernetes/client-node';
import { BuildParameters } from '.'; import { BuildParameters } from '.';
import * as core from '@actions/core'; import * as core from '@actions/core';
@ -25,12 +24,6 @@ class Kubernetes {
const k8sBatchApi = kc.makeApiClient(k8s.BatchV1Api); const k8sBatchApi = kc.makeApiClient(k8s.BatchV1Api);
core.info('loaded from default'); 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 buildId = Kubernetes.uuidv4();
const pvcName = `unity-builder-pvc-${buildId}`; const pvcName = `unity-builder-pvc-${buildId}`;
const secretName = `build-credentials-${buildId}`; const secretName = `build-credentials-${buildId}`;
@ -317,60 +310,42 @@ class Kubernetes {
try { try {
const pod = await Kubernetes.watchPodUntilRunningAndRead(); const pod = await Kubernetes.watchPodUntilRunningAndRead();
core.info(`Watching build job ${pod?.metadata?.name}`); 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) { } catch (error) {
core.error('Failed while watching build job'); core.error('Failed while watching build job');
throw error; throw error;
} }
} }
static async streamLogs(name: string, namespace: string) { static async streamLogs(name: string, namespace: string, container: string) {
try { try {
let running = true; let running = true;
let mostRecentLogTime: number = 999;
let mostRecentLine: string = '';
while (running) { while (running) {
const pod = await this.kubeClient.readNamespacedPod(name, namespace); const pod = await this.kubeClient.readNamespacedPod(name, namespace);
running = pod.body.status?.phase === 'Running'; running = pod.body.status?.phase === 'Running';
await new Promise((resolve) => setTimeout(resolve, pollInterval)); await new Promise((resolve) => setTimeout(resolve, pollInterval));
core.info('Polling logs...'); core.info('Polling logs...');
let logs; const logs = await this.kubeClient.readNamespacedPodLog(
try {
logs = await this.kubeClient.readNamespacedPodLog(
name, name,
namespace, namespace,
container,
true,
undefined, undefined,
undefined, undefined,
undefined, undefined,
undefined, undefined,
undefined, undefined,
undefined, undefined,
mostRecentLogTime,
undefined,
true, true,
); );
} catch (error) { logs.response.on('data', (data) => {
core.info(error); core.info('LOGS RECEIVED');
if (error.message === 'HTTP request failed') { core.info(data);
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;
}
}
}
} }
} catch (error) { } catch (error) {
core.error(JSON.stringify(error, undefined, 4)); core.error(JSON.stringify(error, undefined, 4));