57 lines
1.6 KiB
TypeScript
57 lines
1.6 KiB
TypeScript
import { BatchV1beta1Api, V1beta1CronJob } from '@kubernetes/client-node';
|
|
import { Cron } from 'cron-converter';
|
|
import * as core from '@actions/core';
|
|
class KubernetesCleanupCronJob {
|
|
static async cleanup(api: BatchV1beta1Api, name: string, namespace: string) {
|
|
await api.deleteNamespacedCronJob('name', namespace);
|
|
}
|
|
static async createCleanupCronJob(kubeClientBatch: BatchV1beta1Api, name: string, namespace: string) {
|
|
try {
|
|
const batchJob = new V1beta1CronJob();
|
|
batchJob.kind = 'CronJob';
|
|
batchJob.metadata = {
|
|
name,
|
|
labels: {
|
|
app: 'unity-builder',
|
|
},
|
|
};
|
|
const cronInstance = new Cron();
|
|
const date = Date.now() + 1000 * 60 * 60;
|
|
const cronString = cronInstance.schedule(new Date(date)).toString();
|
|
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: cronString,
|
|
jobTemplate: {
|
|
spec: {
|
|
template: { spec },
|
|
},
|
|
},
|
|
};
|
|
|
|
core.info('creating cron job');
|
|
await kubeClientBatch.createNamespacedCronJob(namespace, batchJob);
|
|
core.info('created cron job');
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
}
|
|
}
|
|
export default KubernetesCleanupCronJob;
|