🌳
pt0/deployF/k8sF/resActionsF/resPlainActionF.mts
1import * as _ from 'lodash-es'
4import { getKlusterCtx, type KubeResource } from '../ctxF/klusterCtxF.mts'
10type ResAction = 'apply' | 'delete' | 'info' | 'skipdel' | 'recreate' | 'patch' | 'replace'
11type ResPlainActionProps = {
12 action?: ResAction
13 resource: KubeResource
14 cluster_name?: string
15 noDelPvcs?: boolean
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)?.managedFields
26 const ctx = getKlusterCtx() || {}
27 const {k8sNamespace} = ctx
28 if (k8sNamespace) {
29 resource.metadata.namespace = k8sNamespace
30 }
32 const cluster_name = clusterNameArg || ctx.cluster_name!
33 action ||= ctx.action as ResAction
35 if (action == 'skipdel') {
36 const {name} = resource.metadata, {kind} = resource
37 return {action: 'skipdel', cluster_name, name, kind}
38 }
40 if (action == 'info') {
41 assertDefined(resource)
42 if (isMutableRes(resource)) return
43 const offlineReason = getOfflineReason(cluster_name)
44 if (offlineReason) {
45 console.log('offline', inspect2KubeRes({resource, cluster_name}), `(${offlineReason})`)
46 return
47 }
48 const readResource = await read2Resource({resource, cluster_name})
49 await backupSeenKubeResource({resource: readResource as Parameters<typeof backupSeenKubeResource>[0]['resource'], cluster_name})
51 const exists = !!readResource
52 console.log(exists ? 'exists ' : 'noexist', inspect2KubeRes({resource, cluster_name}))
53 return readResource // for doesExist check in cnpg qs
54 }
56 if (action == 'apply') {
57 const result = await applyResource({resource, cluster_name})
58 await backupSeenKubeResource({resource, cluster_name})
59 return result
60 }
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 return
67 }