k8s full refactoring to simpler and better api client
parent
5929a988d3
commit
013e54aa7c
|
|
@ -762,11 +762,16 @@ class Kubernetes {
|
|||
this.secretName = secretName;
|
||||
this.jobName = jobName;
|
||||
this.namespace = namespace;
|
||||
// setup
|
||||
yield Kubernetes.createSecret();
|
||||
yield Kubernetes.createPersistentVolumeClaim();
|
||||
// await Kubernetes.scheduleBuildJob();
|
||||
// await Kubernetes.watchBuildJobUntilFinished();
|
||||
// await Kubernetes.cleanup();
|
||||
// start
|
||||
yield Kubernetes.scheduleBuildJob();
|
||||
// watch
|
||||
yield Kubernetes.watchPersistentVolumeClaimUntilReady();
|
||||
yield Kubernetes.watchBuildJobUntilFinished();
|
||||
// cleanup
|
||||
yield Kubernetes.cleanup();
|
||||
core.setOutput('volume', pvcName);
|
||||
});
|
||||
}
|
||||
|
|
@ -813,8 +818,6 @@ class Kubernetes {
|
|||
};
|
||||
yield this.kubeClient.createNamespacedPersistentVolumeClaim(this.namespace, pvc);
|
||||
core.info('Persistent Volume created, waiting for ready state...');
|
||||
yield Kubernetes.watchPersistentVolumeClaimUntilReady();
|
||||
core.info('Persistent Volume ready for claims');
|
||||
});
|
||||
}
|
||||
static watchPersistentVolumeClaimUntilReady() {
|
||||
|
|
@ -825,6 +828,9 @@ class Kubernetes {
|
|||
if (((_a = queryResult.body.status) === null || _a === void 0 ? void 0 : _a.phase) === 'Pending') {
|
||||
yield Kubernetes.watchPersistentVolumeClaimUntilReady();
|
||||
}
|
||||
else {
|
||||
core.info('Persistent Volume ready for claims');
|
||||
}
|
||||
});
|
||||
}
|
||||
static scheduleBuildJob() {
|
||||
|
|
@ -999,64 +1005,68 @@ class Kubernetes {
|
|||
core.info('Job created');
|
||||
});
|
||||
}
|
||||
// static async watchBuildJobUntilFinished() {
|
||||
// let podname;
|
||||
// let ready = false;
|
||||
// while (!ready) {
|
||||
// await new Promise((resolve) => setTimeout(resolve, pollInterval));
|
||||
// const pods = await this.kubeClient.api.v1.namespaces(this.namespace).pods.get();
|
||||
// for (let index = 0; index < pods.body.items.length; index++) {
|
||||
// const element = pods.body.items[index];
|
||||
// if (element.metadata.labels['job-name'] === this.jobName && element.status.phase !== 'Pending') {
|
||||
// core.info('Pod no longer pending');
|
||||
// if (element.status.phase === 'Failure') {
|
||||
// core.error('Kubernetes job failed');
|
||||
// } else {
|
||||
// ready = true;
|
||||
// podname = element.metadata.name;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// core.info(`Watching build job ${podname}`);
|
||||
// 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();
|
||||
// if (podStatus.body.status.phase !== 'Running') {
|
||||
// complete = true;
|
||||
// }
|
||||
// const logs = await this.kubeClient.api.v1
|
||||
// .namespaces(this.namespace)
|
||||
// .pod(podname)
|
||||
// .log.get({
|
||||
// qs: {
|
||||
// sinceTime: logQueryTime,
|
||||
// timestamps: true,
|
||||
// },
|
||||
// });
|
||||
// if (logs.body !== undefined) {
|
||||
// const arrayOfLines = logs.body.match(/[^\n\r]+/g).reverse();
|
||||
// for (const element of arrayOfLines) {
|
||||
// const [time, ...line] = element.split(' ');
|
||||
// if (time !== logQueryTime) {
|
||||
// core.info(line.join(' '));
|
||||
// } else {
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// if (podStatus.body.status.phase === 'Failed') {
|
||||
// throw new Error('Kubernetes job failed');
|
||||
// }
|
||||
// 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 watchBuildJobUntilFinished() {
|
||||
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
let podname;
|
||||
let ready = false;
|
||||
while (!ready) {
|
||||
yield new Promise((resolve) => setTimeout(resolve, pollInterval));
|
||||
const pods = yield this.kubeClient.listNamespacedPod(this.namespace);
|
||||
for (let index = 0; index < pods.body.items.length; 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 phase = (_c = element.status) === null || _c === void 0 ? void 0 : _c.phase;
|
||||
if (jobname === this.jobName && phase !== 'Pending') {
|
||||
core.info('Pod no longer pending');
|
||||
if (phase === 'Failure') {
|
||||
core.error('Kubernetes job failed');
|
||||
}
|
||||
else {
|
||||
ready = true;
|
||||
podname = (_d = element.metadata) === null || _d === void 0 ? void 0 : _d.name;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
core.info(`Watching build job ${podname}`);
|
||||
let logQueryTime;
|
||||
let complete = false;
|
||||
while (!complete) {
|
||||
yield new Promise((resolve) => setTimeout(resolve, pollInterval));
|
||||
const podStatus = yield this.kubeClient.readNamespacedPod(podname, this.namespace);
|
||||
if (((_f = (_e = podStatus.body) === null || _e === void 0 ? void 0 : _e.status) === null || _f === void 0 ? void 0 : _f.phase) !== 'Running') {
|
||||
complete = true;
|
||||
}
|
||||
const logs = yield this.kubeClient.readNamespacedPodLog(podname, this.namespace, undefined, undefined, undefined, undefined, undefined, undefined, logQueryTime, undefined, 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) {
|
||||
continue;
|
||||
}
|
||||
for (const element of arrayOfLines) {
|
||||
const [time, ...line] = element.split(' ');
|
||||
if (time !== logQueryTime) {
|
||||
core.info(line.join(' '));
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
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];
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
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() {
|
||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
||||
const r = Math.trunc(Math.random() * 16);
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
|
|
@ -47,11 +47,19 @@ class Kubernetes {
|
|||
this.jobName = jobName;
|
||||
this.namespace = namespace;
|
||||
|
||||
// setup
|
||||
await Kubernetes.createSecret();
|
||||
await Kubernetes.createPersistentVolumeClaim();
|
||||
// await Kubernetes.scheduleBuildJob();
|
||||
// await Kubernetes.watchBuildJobUntilFinished();
|
||||
// await Kubernetes.cleanup();
|
||||
|
||||
// start
|
||||
await Kubernetes.scheduleBuildJob();
|
||||
|
||||
// watch
|
||||
await Kubernetes.watchPersistentVolumeClaimUntilReady();
|
||||
await Kubernetes.watchBuildJobUntilFinished();
|
||||
|
||||
// cleanup
|
||||
await Kubernetes.cleanup();
|
||||
|
||||
core.setOutput('volume', pvcName);
|
||||
}
|
||||
|
|
@ -99,8 +107,6 @@ class Kubernetes {
|
|||
};
|
||||
await this.kubeClient.createNamespacedPersistentVolumeClaim(this.namespace, pvc);
|
||||
core.info('Persistent Volume created, waiting for ready state...');
|
||||
await Kubernetes.watchPersistentVolumeClaimUntilReady();
|
||||
core.info('Persistent Volume ready for claims');
|
||||
}
|
||||
|
||||
static async watchPersistentVolumeClaimUntilReady() {
|
||||
|
|
@ -109,6 +115,8 @@ class Kubernetes {
|
|||
|
||||
if (queryResult.body.status?.phase === 'Pending') {
|
||||
await Kubernetes.watchPersistentVolumeClaimUntilReady();
|
||||
} else {
|
||||
core.info('Persistent Volume ready for claims');
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -283,70 +291,80 @@ class Kubernetes {
|
|||
core.info('Job created');
|
||||
}
|
||||
|
||||
// static async watchBuildJobUntilFinished() {
|
||||
// let podname;
|
||||
// let ready = false;
|
||||
// while (!ready) {
|
||||
// await new Promise((resolve) => setTimeout(resolve, pollInterval));
|
||||
// const pods = await this.kubeClient.api.v1.namespaces(this.namespace).pods.get();
|
||||
// for (let index = 0; index < pods.body.items.length; index++) {
|
||||
// const element = pods.body.items[index];
|
||||
// if (element.metadata.labels['job-name'] === this.jobName && element.status.phase !== 'Pending') {
|
||||
// core.info('Pod no longer pending');
|
||||
// if (element.status.phase === 'Failure') {
|
||||
// core.error('Kubernetes job failed');
|
||||
// } else {
|
||||
// ready = true;
|
||||
// podname = element.metadata.name;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
static async watchBuildJobUntilFinished() {
|
||||
let podname;
|
||||
let ready = false;
|
||||
while (!ready) {
|
||||
await new Promise((resolve) => setTimeout(resolve, pollInterval));
|
||||
const pods = await this.kubeClient.listNamespacedPod(this.namespace);
|
||||
for (let index = 0; index < pods.body.items.length; index++) {
|
||||
const element = pods.body.items[index];
|
||||
const jobname = element.metadata?.labels?.['job-name'];
|
||||
const phase = element.status?.phase;
|
||||
if (jobname === this.jobName && phase !== 'Pending') {
|
||||
core.info('Pod no longer pending');
|
||||
if (phase === 'Failure') {
|
||||
core.error('Kubernetes job failed');
|
||||
} else {
|
||||
ready = true;
|
||||
podname = element.metadata?.name;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// core.info(`Watching build job ${podname}`);
|
||||
// let logQueryTime;
|
||||
// let complete = false;
|
||||
// while (!complete) {
|
||||
// await new Promise((resolve) => setTimeout(resolve, pollInterval));
|
||||
core.info(`Watching build job ${podname}`);
|
||||
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();
|
||||
// if (podStatus.body.status.phase !== 'Running') {
|
||||
// complete = true;
|
||||
// }
|
||||
const podStatus = await this.kubeClient.readNamespacedPod(podname, this.namespace);
|
||||
if (podStatus.body?.status?.phase !== 'Running') {
|
||||
complete = true;
|
||||
}
|
||||
|
||||
// const logs = await this.kubeClient.api.v1
|
||||
// .namespaces(this.namespace)
|
||||
// .pod(podname)
|
||||
// .log.get({
|
||||
// qs: {
|
||||
// sinceTime: logQueryTime,
|
||||
// timestamps: true,
|
||||
// },
|
||||
// });
|
||||
// if (logs.body !== undefined) {
|
||||
// const arrayOfLines = logs.body.match(/[^\n\r]+/g).reverse();
|
||||
// for (const element of arrayOfLines) {
|
||||
// const [time, ...line] = element.split(' ');
|
||||
// if (time !== logQueryTime) {
|
||||
// core.info(line.join(' '));
|
||||
// } else {
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
const logs = await this.kubeClient.readNamespacedPodLog(
|
||||
podname,
|
||||
this.namespace,
|
||||
undefined,
|
||||
undefined,
|
||||
undefined,
|
||||
undefined,
|
||||
undefined,
|
||||
undefined,
|
||||
logQueryTime,
|
||||
undefined,
|
||||
true,
|
||||
);
|
||||
|
||||
// if (podStatus.body.status.phase === 'Failed') {
|
||||
// throw new Error('Kubernetes job failed');
|
||||
// }
|
||||
if (logs.body !== undefined) {
|
||||
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() {
|
||||
// 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();
|
||||
// }
|
||||
logQueryTime = arrayOfLines[0].split(' ')[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static async cleanup() {
|
||||
await this.kubeClientBatch.deleteNamespacedJob(this.jobName, this.namespace);
|
||||
await this.kubeClient.deleteNamespacedSecret(this.secretName, this.namespace);
|
||||
}
|
||||
|
||||
static uuidv4() {
|
||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
||||
|
|
|
|||
Loading…
Reference in New Issue