🌳
pt0/deployF/dockerF/dockRegHealthAI.mts
1import fs from 'fs'
9import { BatchV1Api } from '@kubernetes/client-node'
13type CreateRegistryProps = {
14 dockreg_host: string
15 cluster_name: string
16 sizeGb?: number
19export const createRegistry = async ({dockreg_host, cluster_name, sizeGb = 200}: CreateRegistryProps): Promise<boolean> => {
20 const registryName = dockreg_host.split('.')[0]
22 const kanikoCluster = cluster_name
23 const registryCluster = getKlusterCtx().cluster_name || cluster_name
24 const kanikoCredsFile = genRegCredSecName(dockreg_host, kanikoCluster)
25 const registryCredsFile = genRegCredSecName(dockreg_host, registryCluster)
26 if (kanikoCredsFile !== registryCredsFile) {
27 const kanikoCredsPath = `${secretsDir}/${kanikoCredsFile}`
28 if (fs.existsSync(kanikoCredsPath)) {
29 fs.copyFileSync(kanikoCredsPath, `${secretsDir}/${registryCredsFile}`)
30 }
31 }
33 const existingCtx = getKlusterCtx()
34 const clusterOnlyCtx = {
35 ...existingCtx,
36 cluster_name: existingCtx.cluster_name || cluster_name,
37 dockreg_host,
38 nonWcHostname: undefined,
39 wcHostname: undefined,
40 }
42 await dockBuildCtx.run({dockName: 'docker'}, async () => {
43 await klusterCtx.run(clusterOnlyCtx as any, async () => {
44 console.log(` creating ${registryName}...`)
45 await actionCtx.run({action: 'apply'}, () =>
46 k8sDockerReg({dockreg_host, sizeGb} as any)
47 )
48 })
49 })
51 for (let i = 0; i < 45; i++) {
52 const pod = await klusterCtx.run({cluster_name} as any, () => getSinglePod({cluster_name, labels: {name: registryName}}))
53 if (pod) return true
54 await sleep(2000)
55 }
56 return false
59export const deleteFailedKanikoJobs = async ({name, cluster_name}: {name: string, cluster_name: string}) => {
60 const {kubeConfig} = await getKubeApis({cluster_name})
61 const batchApi = kubeConfig.makeApiClient(BatchV1Api)
62 const {items: jobs} = await batchApi.listNamespacedJob({namespace: 'default', labelSelector: `cluster_name=${cluster_name}`})
63 const failedJobs = jobs.filter(job => {
64 const jobName = job.metadata?.name
65 if (!jobName?.includes(name)) return false
66 return job.status?.failed
67 })
68 if (failedJobs.length) {
69 console.log(` deleting ${failedJobs.length} failed kaniko job(s)...`)
70 for (const job of failedJobs) {
71 const jobName = job.metadata?.name!
72 try {
73 await batchApi.deleteNamespacedJob({name: jobName, namespace: 'default', propagationPolicy: 'Background'})
74 } catch {}
75 }
76 }