🌳
pt0/deployF/k8sF/backupKubeF.mts
1import * as _ from 'lodash-es'
8import { type WithClusterAndResource, type KubeResource } from './ctxF/klusterCtxF.mts'
10type BackupResource = KubeResource & { status?: unknown, metadata: { name: string, namespace?: string, resourceVersion?: string, annotations?: Record<string, string> } }
11type BackupProps = WithClusterAndResource & { resource: BackupResource }
13export const getResBackDir = ({resource, cluster_name}: BackupProps) => {
14 const { kind, metadata } = resource
15 const {namespace='default'} = metadata
18 ptDiskDir,
19 'k8s-snapshots',
20 [cluster_name, namespace].join('-'),
21 kind!,
22 metadata.name,
23 )
26const getResVersPath = ({resource, cluster_name}: BackupProps) => {
27 const res = _.clone(resource)
28 delete res.status
29 const {metadata} = res
30 delete metadata.resourceVersion
31 const {annotations} = res.metadata
32 if (annotations) {
33 delete annotations['kubectl.kubernetes.io/last-applied-configuration']
34 }
35 const resourceVersion = calcReqHash(JSON.stringify(res))
38 getResBackDir({resource: res, cluster_name}),
39 `${resourceVersion}.json`,
40 )
43export const backupSeenKubeResource = async ({resource, cluster_name}: {resource?: BackupResource | null, cluster_name: string}) => {
44 if (!resource) return // TODO save "that it didn't exist"
46 const resVersPath = getResVersPath({resource, cluster_name})
48 const {cacheHit} = await jsonMemoFile(resVersPath, () => {
49 mkdirParentP(resVersPath)
50 return resource
51 })
52 if (!cacheHit) {
53 betVerboseLog('backupSeenKubeResource', resVersPath)
54 }