polling logs from kubernetes api

pull/265/head
Frostebite 2021-05-28 18:37:30 +01:00
parent d8ea2bd701
commit c281250358
3 changed files with 70 additions and 27 deletions

42
dist/index.js vendored
View File

@ -1009,7 +1009,7 @@ class Kubernetes {
core.info('Job created'); core.info('Job created');
}); });
} }
static watchPodUntilReadyAndRead() { static watchPodUntilReadyAndRead(statusFilter) {
var _a, _b, _c; var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
let ready = false; let ready = false;
@ -1020,7 +1020,7 @@ class Kubernetes {
const element = pods.body.items[index]; const element = pods.body.items[index];
const jobname = (_b = (_a = element.metadata) === null || _a === void 0 ? void 0 : _a.labels) === null || _b === void 0 ? void 0 : _b['job-name']; const jobname = (_b = (_a = element.metadata) === null || _a === void 0 ? void 0 : _a.labels) === null || _b === void 0 ? void 0 : _b['job-name'];
const phase = (_c = element.status) === null || _c === void 0 ? void 0 : _c.phase; const phase = (_c = element.status) === null || _c === void 0 ? void 0 : _c.phase;
if (jobname === this.jobName && phase !== 'Pending') { if (jobname === this.jobName && phase !== statusFilter) {
core.info('Pod no longer pending'); core.info('Pod no longer pending');
if (phase === 'Failure') { if (phase === 'Failure') {
core.error('Kubernetes job failed'); core.error('Kubernetes job failed');
@ -1035,17 +1035,37 @@ class Kubernetes {
}); });
} }
static watchBuildJobUntilFinished() { static watchBuildJobUntilFinished() {
var _a, _b, _c, _d, _e, _f, _g; var _a, _b, _c, _d;
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
const pod = (yield Kubernetes.watchPodUntilReadyAndRead()) || {}; const pod = (yield Kubernetes.watchPodUntilReadyAndRead('Pending')) || {};
core.info(`Watching build job ${(_a = pod.metadata) === null || _a === void 0 ? void 0 : _a.name} ${JSON.stringify((_c = (_b = pod.status) === null || _b === void 0 ? void 0 : _b.containerStatuses) === null || _c === void 0 ? void 0 : _c[0].state, undefined, 4)}`); core.info(`Watching build job ${(_a = pod.metadata) === null || _a === void 0 ? void 0 : _a.name} ${JSON.stringify((_c = (_b = pod.status) === null || _b === void 0 ? void 0 : _b.containerStatuses) === null || _c === void 0 ? void 0 : _c[0].state, undefined, 4)}`);
core.info(JSON.stringify({ yield Kubernetes.streamLogs(((_d = pod.metadata) === null || _d === void 0 ? void 0 : _d.name) || '', this.namespace);
name: ((_d = pod.metadata) === null || _d === void 0 ? void 0 : _d.name) || '', });
namespace: this.namespace, }
container: (_f = (_e = pod.status) === null || _e === void 0 ? void 0 : _e.containerStatuses) === null || _f === void 0 ? void 0 : _f[0].containerID, static streamLogs(name, namespace) {
}, undefined, 4)); var _a, _b;
const logs = yield this.kubeClient.readNamespacedPodLog(((_g = pod.metadata) === null || _g === void 0 ? void 0 : _g.name) || '', this.namespace); return __awaiter(this, void 0, void 0, function* () {
core.info(logs.body); let running = true;
let logQueryTime;
while (running) {
const logs = yield this.kubeClient.readNamespacedPodLog(name, namespace, undefined, undefined, undefined, undefined, undefined, undefined, logQueryTime, undefined, true);
core.info(logs.body);
const arrayOfLines = (_a = logs.body.match(/[^\n\r]+/g)) === null || _a === void 0 ? void 0 : _a.reverse();
if (arrayOfLines) {
for (const element of arrayOfLines) {
const [time, ...line] = element.split(' ');
if (time !== logQueryTime) {
core.info(line.join(' '));
}
else {
break;
}
}
logQueryTime = arrayOfLines[0].split(' ')[0];
}
const pod = yield this.kubeClient.readNamespacedPod(name, namespace);
running = ((_b = pod.body.status) === null || _b === void 0 ? void 0 : _b.phase) === 'Running';
}
}); });
} }
static cleanup() { static cleanup() {

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

View File

@ -295,7 +295,7 @@ class Kubernetes {
core.info('Job created'); core.info('Job created');
} }
static async watchPodUntilReadyAndRead() { static async watchPodUntilReadyAndRead(statusFilter: string) {
let ready = false; let ready = false;
while (!ready) { while (!ready) {
@ -305,7 +305,7 @@ class Kubernetes {
const element = pods.body.items[index]; const element = pods.body.items[index];
const jobname = element.metadata?.labels?.['job-name']; const jobname = element.metadata?.labels?.['job-name'];
const phase = element.status?.phase; const phase = element.status?.phase;
if (jobname === this.jobName && phase !== 'Pending') { if (jobname === this.jobName && phase !== statusFilter) {
core.info('Pod no longer pending'); core.info('Pod no longer pending');
if (phase === 'Failure') { if (phase === 'Failure') {
core.error('Kubernetes job failed'); core.error('Kubernetes job failed');
@ -319,7 +319,7 @@ class Kubernetes {
} }
static async watchBuildJobUntilFinished() { static async watchBuildJobUntilFinished() {
const pod = (await Kubernetes.watchPodUntilReadyAndRead()) || {}; const pod = (await Kubernetes.watchPodUntilReadyAndRead('Pending')) || {};
core.info( core.info(
`Watching build job ${pod.metadata?.name} ${JSON.stringify( `Watching build job ${pod.metadata?.name} ${JSON.stringify(
@ -328,19 +328,42 @@ class Kubernetes {
4, 4,
)}`, )}`,
); );
core.info( await Kubernetes.streamLogs(pod.metadata?.name || '', this.namespace);
JSON.stringify( }
{
name: pod.metadata?.name || '', static async streamLogs(name: string, namespace: string) {
namespace: this.namespace, let running = true;
container: pod.status?.containerStatuses?.[0].containerID, let logQueryTime;
}, while (running) {
const logs = await this.kubeClient.readNamespacedPodLog(
name,
namespace,
undefined, undefined,
4, undefined,
), undefined,
); undefined,
const logs = await this.kubeClient.readNamespacedPodLog(pod.metadata?.name || '', this.namespace); undefined,
core.info(logs.body); undefined,
logQueryTime,
undefined,
true,
);
core.info(logs.body);
const arrayOfLines = logs.body.match(/[^\n\r]+/g)?.reverse();
if (arrayOfLines) {
for (const element of arrayOfLines) {
const [time, ...line] = element.split(' ');
if (time !== logQueryTime) {
core.info(line.join(' '));
} else {
break;
}
}
logQueryTime = arrayOfLines[0].split(' ')[0];
}
const pod = await this.kubeClient.readNamespacedPod(name, namespace);
running = pod.body.status?.phase === 'Running';
}
} }
static async cleanup() { static async cleanup() {