1import type { V1Volume, V1VolumeMount, V1EnvVarSource } from '@kubernetes/client-node'2import { resourceForJobType } from './jobTypeF.mts'3import { isActionSupported } from './isActionSupportedF.mts'4import { read2Resource } from './read2ResourceF.mts'5import { mergeConcatA } from '../../sharedF/moDashF/mergeConcatAF.mts'6import { waitForDeploymentRollout } from './waitForDeployRolloutAI.mts'7import { getVolumesForSecrets } from '../getVolumesForSecretsF.mts'8import { mergeVolsMnts } from './mergeVolsMntsF.mts'9import { resourcesAction } from './resourcesActionF.mts'10import type { KubeResource } from './ctxF/klusterCtxF.mts'11import { watchFlogs } from './logsF/watchFlogsF.mts'12import { isApplyishAction } from './applyishActionF.mts'14type MkJobDeployProps = {15 action: string16 name: string17 secretNames?: string[]18 envConf?: Record<string, string>19 cluster_name: string20 taskCmd: string21 image: string22 dockreg_host?: string23 jobType: { kind: string, [k: string]: unknown }24 appConfig: { monorDataPvcH?: Record<string, unknown>, colocateWithPod?: string, [k: string]: unknown }25 git_sha: string26 envLocal: Record<string, string | V1EnvVarSource>27 getVolMntsForPvc?: (opts: {monorDataPvcH?: Record<string, unknown>}) => Promise<{resources: KubeResource[], volumes: V1Volume[], volumeMounts: V1VolumeMount[]}>28}30export const mkJobDeployment = async ({31 action, name, secretNames, envConf, cluster_name, taskCmd, image, dockreg_host, jobType,32 appConfig, git_sha, envLocal, getVolMntsForPvc,33}: MkJobDeployProps) => {35 let {promises,envConfHash,...volMnts0} = getVolumesForSecrets({secretNames, envConf, cluster_name, name, action} as any)37 const {monorDataPvcH, colocateWithPod} = appConfig38 let {resources, ...volMnts1} = getVolMntsForPvc39 ? await getVolMntsForPvc({monorDataPvcH})40 : {resources: [] as any[], volumes: [], volumeMounts: []}41 const {volumes, volumeMounts} = mergeVolsMnts(volMnts0 as any, volMnts1 as any)43 const hasPvc = resources.length > 045 let resource = await resourceForJobType({46 action, jobType: jobType as any, name, cluster_name, dockreg_host, image, taskCmd, volumes, volumeMounts, git_sha, envLocal,47 hasPvc, colocateWithPod,48 })49 const templateAnnotations = {50 metadata: {51 annotations: {52 envConfHash53 }54 }55 }56 if (jobType.kind == 'CronJob') {57 resource = mergeConcatA(resource as object, {58 spec: {59 jobTemplate: {60 spec: {61 template: templateAnnotations62 }63 }64 }65 })66 } else {67 resource = mergeConcatA(resource as object, {68 spec: {69 template: templateAnnotations70 }71 })72 }74 resources.push(resource as KubeResource)76 if (isApplyishAction(action)) {77 const existingJob = await read2Resource({resource: resource as any, cluster_name})78 if (existingJob && existingJob.kind != resource?.kind) {79 await resourcesAction({resources: [existingJob as KubeResource], action: 'delete'})80 }81 }83 if (isActionSupported(action)) {84 promises.push(resourcesAction({resources, action, cluster_name, noDelPvcs: true}))85 await Promise.all(promises)86 }88 if (isApplyishAction(action) && resource?.kind == 'Deployment') {89 await waitForDeploymentRollout({resource: resource as any, cluster_name})90 if (action == 'apply') await watchFlogs()91 }92 return resource93}