unity-builder/src/model/remote-builder/kubernetes-cleanup-cronjob.ts

53 lines
1.4 KiB
TypeScript
Raw Normal View History

2021-06-26 01:50:03 +00:00
import { BatchV1beta1Api, V1beta1CronJob } from '@kubernetes/client-node';
import { Cron } from 'cron-converter';
class KubernetesCleanupCronJob {
static async cleanup(api: BatchV1beta1Api, name: string, namespace: string) {
await api.deleteNamespacedCronJob('name', namespace);
}
static createCleanupCronJob(kubeClientBatch: BatchV1beta1Api, name: string, namespace: string) {
2021-06-26 01:54:37 +00:00
try {
const batchJob = new V1beta1CronJob();
batchJob.kind = 'CronJob';
batchJob.metadata = {
name,
labels: {
app: 'unity-builder',
2021-06-26 01:50:03 +00:00
},
2021-06-26 01:54:37 +00:00
};
const cronInstance = new Cron();
const date = Date.now() + 1000 * 60 * 60;
const spec = {
containers: [
{
name: 'main',
image: 'bitnami/kubectl',
imagePullPolicy: '',
command: ['/bin/sh'],
args: [
'-c',
`
echo "delete the kubernetes resources"
kubectl get pods
`,
],
restartPolicy: '',
},
],
};
batchJob.spec = {
schedule: cronInstance.schedule(new Date(date)).toString(),
jobTemplate: {
spec: {
template: { spec },
},
2021-06-26 01:50:03 +00:00
},
2021-06-26 01:54:37 +00:00
};
2021-06-26 01:50:03 +00:00
2021-06-26 01:54:37 +00:00
kubeClientBatch.createNamespacedCronJob(namespace, batchJob);
} catch (error) {
throw error;
}
2021-06-26 01:50:03 +00:00
}
}
export default KubernetesCleanupCronJob;