k8s full refactoring to simpler and better api client

pull/265/head
Frostebite 2021-05-23 16:08:32 +01:00
parent 5929a988d3
commit 013e54aa7c
3 changed files with 155 additions and 127 deletions

136
dist/index.js vendored
View File

@ -762,11 +762,16 @@ class Kubernetes {
this.secretName = secretName; this.secretName = secretName;
this.jobName = jobName; this.jobName = jobName;
this.namespace = namespace; this.namespace = namespace;
// setup
yield Kubernetes.createSecret(); yield Kubernetes.createSecret();
yield Kubernetes.createPersistentVolumeClaim(); yield Kubernetes.createPersistentVolumeClaim();
// await Kubernetes.scheduleBuildJob(); // start
// await Kubernetes.watchBuildJobUntilFinished(); yield Kubernetes.scheduleBuildJob();
// await Kubernetes.cleanup(); // watch
yield Kubernetes.watchPersistentVolumeClaimUntilReady();
yield Kubernetes.watchBuildJobUntilFinished();
// cleanup
yield Kubernetes.cleanup();
core.setOutput('volume', pvcName); core.setOutput('volume', pvcName);
}); });
} }
@ -813,8 +818,6 @@ class Kubernetes {
}; };
yield this.kubeClient.createNamespacedPersistentVolumeClaim(this.namespace, pvc); yield this.kubeClient.createNamespacedPersistentVolumeClaim(this.namespace, pvc);
core.info('Persistent Volume created, waiting for ready state...'); core.info('Persistent Volume created, waiting for ready state...');
yield Kubernetes.watchPersistentVolumeClaimUntilReady();
core.info('Persistent Volume ready for claims');
}); });
} }
static watchPersistentVolumeClaimUntilReady() { static watchPersistentVolumeClaimUntilReady() {
@ -825,6 +828,9 @@ class Kubernetes {
if (((_a = queryResult.body.status) === null || _a === void 0 ? void 0 : _a.phase) === 'Pending') { if (((_a = queryResult.body.status) === null || _a === void 0 ? void 0 : _a.phase) === 'Pending') {
yield Kubernetes.watchPersistentVolumeClaimUntilReady(); yield Kubernetes.watchPersistentVolumeClaimUntilReady();
} }
else {
core.info('Persistent Volume ready for claims');
}
}); });
} }
static scheduleBuildJob() { static scheduleBuildJob() {
@ -999,64 +1005,68 @@ class Kubernetes {
core.info('Job created'); core.info('Job created');
}); });
} }
// static async watchBuildJobUntilFinished() { static watchBuildJobUntilFinished() {
// let podname; var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
// let ready = false; return __awaiter(this, void 0, void 0, function* () {
// while (!ready) { let podname;
// await new Promise((resolve) => setTimeout(resolve, pollInterval)); let ready = false;
// const pods = await this.kubeClient.api.v1.namespaces(this.namespace).pods.get(); while (!ready) {
// for (let index = 0; index < pods.body.items.length; index++) { yield new Promise((resolve) => setTimeout(resolve, pollInterval));
// const element = pods.body.items[index]; const pods = yield this.kubeClient.listNamespacedPod(this.namespace);
// if (element.metadata.labels['job-name'] === this.jobName && element.status.phase !== 'Pending') { for (let index = 0; index < pods.body.items.length; index++) {
// core.info('Pod no longer pending'); const element = pods.body.items[index];
// if (element.status.phase === 'Failure') { const jobname = (_b = (_a = element.metadata) === null || _a === void 0 ? void 0 : _a.labels) === null || _b === void 0 ? void 0 : _b['job-name'];
// core.error('Kubernetes job failed'); const phase = (_c = element.status) === null || _c === void 0 ? void 0 : _c.phase;
// } else { if (jobname === this.jobName && phase !== 'Pending') {
// ready = true; core.info('Pod no longer pending');
// podname = element.metadata.name; if (phase === 'Failure') {
// } core.error('Kubernetes job failed');
// } }
// } else {
// } ready = true;
// core.info(`Watching build job ${podname}`); podname = (_d = element.metadata) === null || _d === void 0 ? void 0 : _d.name;
// let logQueryTime; }
// let complete = false; }
// while (!complete) { }
// await new Promise((resolve) => setTimeout(resolve, pollInterval)); }
// const podStatus = await this.kubeClient.api.v1.namespaces(this.namespace).pod(podname).get(); core.info(`Watching build job ${podname}`);
// if (podStatus.body.status.phase !== 'Running') { let logQueryTime;
// complete = true; let complete = false;
// } while (!complete) {
// const logs = await this.kubeClient.api.v1 yield new Promise((resolve) => setTimeout(resolve, pollInterval));
// .namespaces(this.namespace) const podStatus = yield this.kubeClient.readNamespacedPod(podname, this.namespace);
// .pod(podname) if (((_f = (_e = podStatus.body) === null || _e === void 0 ? void 0 : _e.status) === null || _f === void 0 ? void 0 : _f.phase) !== 'Running') {
// .log.get({ complete = true;
// qs: { }
// sinceTime: logQueryTime, const logs = yield this.kubeClient.readNamespacedPodLog(podname, this.namespace, undefined, undefined, undefined, undefined, undefined, undefined, logQueryTime, undefined, true);
// timestamps: true, if (logs.body !== undefined) {
// }, const arrayOfLines = (_h = (_g = logs.body) === null || _g === void 0 ? void 0 : _g.match(/[^\n\r]+/g)) === null || _h === void 0 ? void 0 : _h.reverse();
// }); if (!arrayOfLines) {
// if (logs.body !== undefined) { continue;
// const arrayOfLines = logs.body.match(/[^\n\r]+/g).reverse(); }
// for (const element of arrayOfLines) { for (const element of arrayOfLines) {
// const [time, ...line] = element.split(' '); const [time, ...line] = element.split(' ');
// if (time !== logQueryTime) { if (time !== logQueryTime) {
// core.info(line.join(' ')); core.info(line.join(' '));
// } else { }
// break; else {
// } break;
// } }
// if (podStatus.body.status.phase === 'Failed') { }
// throw new Error('Kubernetes job failed'); if (((_k = (_j = podStatus.body) === null || _j === void 0 ? void 0 : _j.status) === null || _k === void 0 ? void 0 : _k.phase) === 'Failed') {
// } throw new Error('Kubernetes job failed');
// logQueryTime = arrayOfLines[0].split(' ')[0]; }
// } logQueryTime = arrayOfLines[0].split(' ')[0];
// } }
// } }
// static async cleanup() { });
// await this.kubeClient.apis.batch.v1.namespaces(this.namespace).jobs(this.jobName).delete(); }
// await this.kubeClient.api.v1.namespaces(this.namespace).secrets(this.secretName).delete(); static cleanup() {
// } return __awaiter(this, void 0, void 0, function* () {
yield this.kubeClientBatch.deleteNamespacedJob(this.jobName, this.namespace);
yield this.kubeClient.deleteNamespacedSecret(this.secretName, this.namespace);
});
}
static uuidv4() { static uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
const r = Math.trunc(Math.random() * 16); const r = Math.trunc(Math.random() * 16);

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

View File

@ -47,11 +47,19 @@ class Kubernetes {
this.jobName = jobName; this.jobName = jobName;
this.namespace = namespace; this.namespace = namespace;
// setup
await Kubernetes.createSecret(); await Kubernetes.createSecret();
await Kubernetes.createPersistentVolumeClaim(); await Kubernetes.createPersistentVolumeClaim();
// await Kubernetes.scheduleBuildJob();
// await Kubernetes.watchBuildJobUntilFinished(); // start
// await Kubernetes.cleanup(); await Kubernetes.scheduleBuildJob();
// watch
await Kubernetes.watchPersistentVolumeClaimUntilReady();
await Kubernetes.watchBuildJobUntilFinished();
// cleanup
await Kubernetes.cleanup();
core.setOutput('volume', pvcName); core.setOutput('volume', pvcName);
} }
@ -99,8 +107,6 @@ class Kubernetes {
}; };
await this.kubeClient.createNamespacedPersistentVolumeClaim(this.namespace, pvc); await this.kubeClient.createNamespacedPersistentVolumeClaim(this.namespace, pvc);
core.info('Persistent Volume created, waiting for ready state...'); core.info('Persistent Volume created, waiting for ready state...');
await Kubernetes.watchPersistentVolumeClaimUntilReady();
core.info('Persistent Volume ready for claims');
} }
static async watchPersistentVolumeClaimUntilReady() { static async watchPersistentVolumeClaimUntilReady() {
@ -109,6 +115,8 @@ class Kubernetes {
if (queryResult.body.status?.phase === 'Pending') { if (queryResult.body.status?.phase === 'Pending') {
await Kubernetes.watchPersistentVolumeClaimUntilReady(); await Kubernetes.watchPersistentVolumeClaimUntilReady();
} else {
core.info('Persistent Volume ready for claims');
} }
} }
@ -283,70 +291,80 @@ class Kubernetes {
core.info('Job created'); core.info('Job created');
} }
// static async watchBuildJobUntilFinished() { static async watchBuildJobUntilFinished() {
// let podname; let podname;
// let ready = false; let ready = false;
// while (!ready) { while (!ready) {
// await new Promise((resolve) => setTimeout(resolve, pollInterval)); await new Promise((resolve) => setTimeout(resolve, pollInterval));
// const pods = await this.kubeClient.api.v1.namespaces(this.namespace).pods.get(); const pods = await this.kubeClient.listNamespacedPod(this.namespace);
// for (let index = 0; index < pods.body.items.length; index++) { for (let index = 0; index < pods.body.items.length; index++) {
// const element = pods.body.items[index]; const element = pods.body.items[index];
// if (element.metadata.labels['job-name'] === this.jobName && element.status.phase !== 'Pending') { const jobname = element.metadata?.labels?.['job-name'];
// core.info('Pod no longer pending'); const phase = element.status?.phase;
// if (element.status.phase === 'Failure') { if (jobname === this.jobName && phase !== 'Pending') {
// core.error('Kubernetes job failed'); core.info('Pod no longer pending');
// } else { if (phase === 'Failure') {
// ready = true; core.error('Kubernetes job failed');
// podname = element.metadata.name; } else {
// } ready = true;
// } podname = element.metadata?.name;
// } }
// } }
}
}
// core.info(`Watching build job ${podname}`); core.info(`Watching build job ${podname}`);
// let logQueryTime; let logQueryTime;
// let complete = false; let complete = false;
// while (!complete) { while (!complete) {
// await new Promise((resolve) => setTimeout(resolve, pollInterval)); await new Promise((resolve) => setTimeout(resolve, pollInterval));
// const podStatus = await this.kubeClient.api.v1.namespaces(this.namespace).pod(podname).get(); const podStatus = await this.kubeClient.readNamespacedPod(podname, this.namespace);
// if (podStatus.body.status.phase !== 'Running') { if (podStatus.body?.status?.phase !== 'Running') {
// complete = true; complete = true;
// } }
// const logs = await this.kubeClient.api.v1 const logs = await this.kubeClient.readNamespacedPodLog(
// .namespaces(this.namespace) podname,
// .pod(podname) this.namespace,
// .log.get({ undefined,
// qs: { undefined,
// sinceTime: logQueryTime, undefined,
// timestamps: true, undefined,
// }, undefined,
// }); undefined,
// if (logs.body !== undefined) { logQueryTime,
// const arrayOfLines = logs.body.match(/[^\n\r]+/g).reverse(); undefined,
// for (const element of arrayOfLines) { true,
// const [time, ...line] = element.split(' '); );
// if (time !== logQueryTime) {
// core.info(line.join(' '));
// } else {
// break;
// }
// }
// if (podStatus.body.status.phase === 'Failed') { if (logs.body !== undefined) {
// throw new Error('Kubernetes job failed'); const arrayOfLines = logs.body?.match(/[^\n\r]+/g)?.reverse();
// } if (!arrayOfLines) {
continue;
}
for (const element of arrayOfLines) {
const [time, ...line] = element.split(' ');
if (time !== logQueryTime) {
core.info(line.join(' '));
} else {
break;
}
}
// logQueryTime = arrayOfLines[0].split(' ')[0]; if (podStatus.body?.status?.phase === 'Failed') {
// } throw new Error('Kubernetes job failed');
// } }
// }
// static async cleanup() { logQueryTime = arrayOfLines[0].split(' ')[0];
// await this.kubeClient.apis.batch.v1.namespaces(this.namespace).jobs(this.jobName).delete(); }
// await this.kubeClient.api.v1.namespaces(this.namespace).secrets(this.secretName).delete(); }
// } }
static async cleanup() {
await this.kubeClientBatch.deleteNamespacedJob(this.jobName, this.namespace);
await this.kubeClient.deleteNamespacedSecret(this.secretName, this.namespace);
}
static uuidv4() { static uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {