1import * as _ from 'lodash-es'2import { backupSeenKubeResource } from '../backupKubeF.mts'3import { applyResource, delete2Resource, inspect2KubeRes, objectApiMethodCatch } from '../kubeClientF.mts'4import { getKlusterCtx, type KubeResource } from '../ctxF/klusterCtxF.mts'5import { isDeleteSafeResource, isMutableRes } from '../isKubeResF.mts'6import { read2Resource } from '../read2ResourceF.mts'7import { getOfflineReason } from '../ctxF/klusterReachabilityAI.mts'8import { assertDefined } from '../../../sharedF/assertsF/assertDefinedF.mts'10type ResAction = 'apply' | 'delete' | 'info' | 'skipdel' | 'recreate' | 'patch' | 'replace'11type ResPlainActionProps = {12 action?: ResAction13 resource: KubeResource14 cluster_name?: string15 noDelPvcs?: boolean16}18export const resPlainAction = async ({action, resource, cluster_name: clusterNameArg, noDelPvcs=true}: ResPlainActionProps) => {19 if (action == 'delete' && noDelPvcs && isDeleteSafeResource(resource)) {20 action = 'skipdel'21 }23 resource = _.clone(resource)24 delete (resource.metadata as any)?.managedFields26 const ctx = getKlusterCtx() || {}27 const {k8sNamespace} = ctx28 if (k8sNamespace) {29 resource.metadata.namespace = k8sNamespace30 }32 const cluster_name = clusterNameArg || ctx.cluster_name!33 action ||= ctx.action as ResAction35 if (action == 'skipdel') {36 const {name} = resource.metadata, {kind} = resource37 return {action: 'skipdel', cluster_name, name, kind}38 }40 if (action == 'info') {41 assertDefined(resource)42 if (isMutableRes(resource)) return43 const offlineReason = getOfflineReason(cluster_name)44 if (offlineReason) {45 console.log('offline', inspect2KubeRes({resource, cluster_name}), `(${offlineReason})`)46 return47 }48 const readResource = await read2Resource({resource, cluster_name})49 await backupSeenKubeResource({resource: readResource as Parameters<typeof backupSeenKubeResource>[0]['resource'], cluster_name})51 const exists = !!readResource52 console.log(exists ? 'exists ' : 'noexist', inspect2KubeRes({resource, cluster_name}))53 return readResource // for doesExist check in cnpg qs54 }56 if (action == 'apply') {57 const result = await applyResource({resource, cluster_name})58 await backupSeenKubeResource({resource, cluster_name})59 return result60 }61 if (action == 'delete') {62 return await delete2Resource({resource, cluster_name})63 }64 if (_.includes(['recreate', 'patch', 'replace'], action)) {65 await objectApiMethodCatch({resource, method_name: action, cluster_name})66 return67 }68}