pull/273/head
Frostebite 2021-06-06 20:09:56 +01:00
parent 4d622063ea
commit a3745bd8c0
3 changed files with 36 additions and 47 deletions

37
dist/index.js vendored
View File

@ -942,7 +942,12 @@ class Kubernetes {
core.info('Job created'); core.info('Job created');
try { try {
yield Kubernetes.watchPersistentVolumeClaimUntilReady(); yield Kubernetes.watchPersistentVolumeClaimUntilReady();
yield Kubernetes.watchBuildJobUntilFinished(); // We watch the PVC first to allow some time for K8s to notice the job we created and setup a pod.
// TODO: Wait for something more reliable.
const pod = (yield this.kubeClient.listNamespacedPod(this.namespace)).body.items.find((x) => { var _a, _b; return ((_b = (_a = x.metadata) === null || _a === void 0 ? void 0 : _a.labels) === null || _b === void 0 ? void 0 : _b['job-name']) === this.jobName; });
Kubernetes.setPod(pod);
yield Kubernetes.watchUntilPodRunning();
yield Kubernetes.streamLogs();
} }
catch (error) { catch (error) {
core.error(error); core.error(error);
@ -1002,7 +1007,7 @@ class Kubernetes {
} }
}); });
} }
static watchPodUntilRunningAndRead() { static watchUntilPodRunning() {
var _a, _b; var _a, _b;
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
let ready = false; let ready = false;
@ -1016,7 +1021,7 @@ class Kubernetes {
if (phase === 'Running') { if (phase === 'Running') {
core.info('Pod no longer pending'); core.info('Pod no longer pending');
ready = true; ready = true;
return pod; return;
} }
if (phase !== 'Pending') { if (phase !== 'Pending') {
core.error('Kubernetes job failed'); core.error('Kubernetes job failed');
@ -1024,33 +1029,21 @@ class Kubernetes {
} }
}); });
} }
static watchBuildJobUntilFinished() { static setPod(pod) {
var _a, _b, _c, _d, _e, _f; var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () { this.podName = ((_a = pod === null || pod === void 0 ? void 0 : pod.metadata) === null || _a === void 0 ? void 0 : _a.name) || '';
try { this.containerName = ((_c = (_b = pod === null || pod === void 0 ? void 0 : pod.status) === null || _b === void 0 ? void 0 : _b.containerStatuses) === null || _c === void 0 ? void 0 : _c[0].name) || '';
this.podName =
((_b = (_a = (yield this.kubeClient.listNamespacedPod(this.namespace)).body.items.find((x) => { var _a, _b; return ((_b = (_a = x.metadata) === null || _a === void 0 ? void 0 : _a.labels) === null || _b === void 0 ? void 0 : _b['job-name']) === this.jobName; })) === null || _a === void 0 ? void 0 : _a.metadata) === null || _b === void 0 ? void 0 : _b.name) || '';
core.info(this.podName);
const pod = yield Kubernetes.watchPodUntilRunningAndRead();
core.info(`Watching build job ${(_c = pod === null || pod === void 0 ? void 0 : pod.metadata) === null || _c === void 0 ? void 0 : _c.name}`);
yield Kubernetes.streamLogs(((_d = pod === null || pod === void 0 ? void 0 : pod.metadata) === null || _d === void 0 ? void 0 : _d.name) || '', this.namespace, ((_f = (_e = pod === null || pod === void 0 ? void 0 : pod.status) === null || _e === void 0 ? void 0 : _e.containerStatuses) === null || _f === void 0 ? void 0 : _f[0].name) || '');
}
catch (error) {
core.error('Failed while watching build job');
throw error;
}
});
} }
static streamLogs(name, namespace, container) { static streamLogs() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
try { try {
core.info('Polling logs...'); core.info(`Streaming logs from pod: ${this.podName} container: ${this.containerName} namespace: ${this.namespace}`);
const stream = new stream_1.Writable(); const stream = new stream_1.Writable();
stream._write = (chunk, encoding, next) => { stream._write = (chunk, encoding, next) => {
core.info(chunk.toString()); core.info(chunk.toString());
next(); next();
}; };
yield new Promise((resolve) => new client_node_1.Log(this.kubeConfig).log(namespace, name, container, stream, resolve)); yield new Promise((resolve) => new client_node_1.Log(this.kubeConfig).log(this.namespace, this.podName, this.containerName, stream, resolve));
core.info('end of log stream'); core.info('end of log stream');
} }
catch (error) { catch (error) {

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

View File

@ -231,7 +231,14 @@ class Kubernetes {
try { try {
await Kubernetes.watchPersistentVolumeClaimUntilReady(); await Kubernetes.watchPersistentVolumeClaimUntilReady();
await Kubernetes.watchBuildJobUntilFinished(); // We watch the PVC first to allow some time for K8s to notice the job we created and setup a pod.
// TODO: Wait for something more reliable.
const pod = (await this.kubeClient.listNamespacedPod(this.namespace)).body.items.find(
(x) => x.metadata?.labels?.['job-name'] === this.jobName,
);
Kubernetes.setPod(pod);
await Kubernetes.watchUntilPodRunning();
await Kubernetes.streamLogs();
} catch (error) { } catch (error) {
core.error(error); core.error(error);
} finally { } finally {
@ -291,7 +298,7 @@ class Kubernetes {
} }
} }
static async watchPodUntilRunningAndRead() { static async watchUntilPodRunning() {
let ready = false; let ready = false;
while (!ready) { while (!ready) {
@ -304,7 +311,7 @@ class Kubernetes {
if (phase === 'Running') { if (phase === 'Running') {
core.info('Pod no longer pending'); core.info('Pod no longer pending');
ready = true; ready = true;
return pod; return;
} }
if (phase !== 'Pending') { if (phase !== 'Pending') {
core.error('Kubernetes job failed'); core.error('Kubernetes job failed');
@ -312,35 +319,24 @@ class Kubernetes {
} }
} }
static async watchBuildJobUntilFinished() { static setPod(pod: k8s.V1Pod | any) {
try { this.podName = pod?.metadata?.name || '';
this.podName = this.containerName = pod?.status?.containerStatuses?.[0].name || '';
(await this.kubeClient.listNamespacedPod(this.namespace)).body.items.find(
(x) => x.metadata?.labels?.['job-name'] === this.jobName,
)?.metadata?.name || '';
core.info(this.podName);
const pod = await Kubernetes.watchPodUntilRunningAndRead();
core.info(`Watching build job ${pod?.metadata?.name}`);
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, container: string) { static async streamLogs() {
try { try {
core.info('Polling logs...'); core.info(
`Streaming logs from pod: ${this.podName} container: ${this.containerName} namespace: ${this.namespace}`,
);
const stream = new Writable(); const stream = new Writable();
stream._write = (chunk, encoding, next) => { stream._write = (chunk, encoding, next) => {
core.info(chunk.toString()); core.info(chunk.toString());
next(); next();
}; };
await new Promise((resolve) => new Log(this.kubeConfig).log(namespace, name, container, stream, resolve)); await new Promise((resolve) =>
new Log(this.kubeConfig).log(this.namespace, this.podName, this.containerName, stream, resolve),
);
core.info('end of log stream'); core.info('end of log stream');
} catch (error) { } catch (error) {
core.error(JSON.stringify(error, undefined, 4)); core.error(JSON.stringify(error, undefined, 4));