1import { getKlusterCtx, type KubeResource } from './ctxF/klusterCtxF.mts'2import { allPromCalls } from '../../sharedF/moDashF/promF/allPromCallsF.mts'3import { isPvc } from './isKubeResF.mts'4import { res1Action } from './resActionsF/res1ActionF.mts'5import { read2Resource } from './read2ResourceF.mts'6import { checkIngressTlsSecretsExist } from './checkIngressTlsSecretsExistAI.mts'7import { pollUntil } from './throwTimedOutAI.mts'9import { fmtDeleteSummary, fmtApplySummary, actionLabel } from './fmtDeleteSummaryAI.mts'10import { logElapsed } from '../cliF/cliTimerCtxF.mts'11import { assertDefined } from '../../sharedF/assertsF/assertDefinedF.mts'12import { cliFlag } from '../../serverF/cliArgAI.mts'13import { applyHistoryGuard } from '../testsF/recordTestResultAI.mts'14import { getAppCfg } from './ctxF/appCfgCtxF.mts'15import { isApplyishAction } from './applyishActionF.mts'17type ResourcesActionCoreProps = {18 resources: KubeResource[]19 action?: string20 cluster_name?: string21 noDelPvcs?: boolean22 skipTlsCheck?: boolean23 isQuiet?: boolean24}26export const resourcesActionCore = async ({27 resources, action, cluster_name,28 noDelPvcs=true, skipTlsCheck=false, isQuiet=false,29}: ResourcesActionCoreProps) => {30 const ctx = getKlusterCtx()31 action ||= ctx.action32 cluster_name ||= ctx.cluster_name33 assertDefined(action)34 assertDefined(cluster_name)36 if (isApplyishAction(action) && cliFlag('--history')) {37 await applyHistoryGuard(getAppCfg().importMetaUrl)38 return39 }41 if (!skipTlsCheck && isApplyishAction(action)) {42 await checkIngressTlsSecretsExist({resources, cluster_name})43 }45 const ret = await allPromCalls(resources, (resource) => {46 return res1Action({resource, action, cluster_name, noDelPvcs})47 })49 if (isApplyishAction(action)) {50 const summaryLines = fmtApplySummary(ret as any)51 if (!isQuiet) for (const {cluster, action: act, line} of summaryLines) {52 logElapsed(`k8s ${cluster} ${actionLabel(act)}: ${line}`)53 }54 }56 if (action === 'delete') {57 const {deleted} = fmtDeleteSummary(ret as any)59 for (const {cluster, line} of deleted) {60 if (!isQuiet) process.stdout.write(`k8s ${cluster} delete: ${line}`)61 await allPromCalls(resources, async (resource) => {63 const resName = `${resource.kind} ${resource.metadata?.name}`64 await pollUntil({predicate: () => read2Resource({resource, cluster_name}), timeoutLabel: `delete ${resName}`, isQuiet})65 })66 if (!isQuiet) console.log(' done')67 }68 }70 return ret71}