| 
									
										
										
										
											2021-06-26 01:50:03 +00:00
										 |  |  | import { BatchV1beta1Api, V1beta1CronJob } from '@kubernetes/client-node'; | 
					
						
							| 
									
										
										
										
											2021-06-26 02:13:41 +00:00
										 |  |  | import * as core from '@actions/core'; | 
					
						
							| 
									
										
										
										
											2021-06-26 01:50:03 +00:00
										 |  |  | class KubernetesCleanupCronJob { | 
					
						
							|  |  |  |   static async cleanup(api: BatchV1beta1Api, name: string, namespace: string) { | 
					
						
							|  |  |  |     await api.deleteNamespacedCronJob('name', namespace); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-06-26 02:00:49 +00:00
										 |  |  |   static async 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 spec = { | 
					
						
							| 
									
										
										
										
											2021-06-26 02:52:37 +00:00
										 |  |  |         restartPolicy: 'Never', | 
					
						
							| 
									
										
										
										
											2021-06-26 01:54:37 +00:00
										 |  |  |         containers: [ | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             name: 'main', | 
					
						
							|  |  |  |             image: 'bitnami/kubectl', | 
					
						
							|  |  |  |             imagePullPolicy: '', | 
					
						
							|  |  |  |             command: ['/bin/sh'], | 
					
						
							|  |  |  |             args: [ | 
					
						
							|  |  |  |               '-c', | 
					
						
							|  |  |  |               `
 | 
					
						
							|  |  |  |               echo "delete the kubernetes resources" | 
					
						
							|  |  |  |               kubectl get pods | 
					
						
							|  |  |  |               `,
 | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |       }; | 
					
						
							| 
									
										
										
										
											2021-07-01 21:36:57 +00:00
										 |  |  |       const hours = new Date().getUTCHours() + 3; | 
					
						
							| 
									
										
										
										
											2021-06-26 01:54:37 +00:00
										 |  |  |       batchJob.spec = { | 
					
						
							| 
									
										
										
										
											2021-07-01 21:36:57 +00:00
										 |  |  |         schedule: `0 ${hours > 24 ? hours - 24 : hours} * * *`, | 
					
						
							| 
									
										
										
										
											2021-06-26 01:54:37 +00:00
										 |  |  |         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 02:13:41 +00:00
										 |  |  |       core.info('creating cron job'); | 
					
						
							| 
									
										
										
										
											2021-06-26 02:00:49 +00:00
										 |  |  |       await kubeClientBatch.createNamespacedCronJob(namespace, batchJob); | 
					
						
							| 
									
										
										
										
											2021-06-26 02:13:41 +00:00
										 |  |  |       core.info('created cron job'); | 
					
						
							| 
									
										
										
										
											2021-06-26 01:54:37 +00:00
										 |  |  |     } catch (error) { | 
					
						
							|  |  |  |       throw error; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-26 01:50:03 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | export default KubernetesCleanupCronJob; |