1import * as _ from 'lodash-es' 15import { spawnSync } from 'child_process' 25export const resources = Object.assign(async ({resource}: {resource: KubeResource}) => { 26 const ctx = kubeCoalesceCtx.getStore() 28 const {resourcesA} = ctx 29 resourcesA.push(resource) 33}, {cliDescript: 'show json arr of k8s resources that would be created/applied'}) 35export const awaitrollout = Object.assign(async ({resource, cluster_name}: ResActionProps) => { 38}, {cliAdvanced: true}) 40export const delpvcs = async ({resource, cluster_name}: ResActionProps) => { 42 const name = resource.metadata?.name 43 if (isPvc(resource)) process.stdout.write(`k8s ${cluster_name} delpvc: ${name} (${shortKind(resource.kind)})`) 44 await resPlainAction({resource, action: 'delete', cluster_name, noDelPvcs: false}) 50delpvcs.cliSchema = forceCli 52export const pods = async ({resource, cluster_name}: ResActionProps) => { 54 await kubePods({cluster_name, labelSel: `name=${resource.metadata.name}`}) 57const isCnpgCluster = (res: KubeResource) => res.apiVersion === 'postgresql.cnpg.io/v1' && res.kind === 'Cluster' 59export const restart = async ({resource, cluster_name}: ResActionProps) => { 60 const {name} = resource.metadata 63 spawnSync('kubectl', ['rollout', 'restart', `deployment/${name}`], { 65 env: {...process.env, KUBECONFIG: kubeConfigPath}, 68 } else if (isCnpgCluster(resource)) { 72restart.cliDescript = 'rollout restart the k8s deployment' 74export const fexec = Object.assign(async ({resource, cluster_name}: ResActionProps) => { 76 const allPods = await getAllPods({cluster_name, labels: {name: resource.metadata.name}}) 78 const podIdx = podArg ? parseInt(podArg, 10) - 1 : 0 80 podArg, validRange: `1-${allPods.length}`, podNames: _.map(allPods, 'podName'), 83 const kubeEnv = { ...process.env, KUBECONFIG: kubeConfigPath } 86 const result = spawnSync('kubectl', ['exec', selectedPod.podName, '--', 'sh', '-c', execCmd], { 87 stdio: 'inherit', env: kubeEnv, 89 process.exitCode = result.status ?? 1 91 spawnSync('kubectl', ['exec', selectedPod.podName, '-it', '--', 'sh'], { 92 stdio: 'inherit', env: kubeEnv, 96 cliSchema: {pod: {type: 'string' as const, desc: 'N (1-based)'}}, 97 cliAcceptsPositional: true, 98 cliDescript: `exec into pod, or run cmd: fexec 'ls -la' --pod=2`, 101export const logs = Object.assign(async ({resource}: ResActionProps) => { 103 const name = resource.metadata.name 106 cliSchema: {pod: {type: 'string' as const, desc: 'N or "all"'}, follow: {flag: true as const, alias: 'f', desc: 'stream logs (-f)'}}, 107 cliDescript: 'stream logs', 110export const busybox = Object.assign(async (props: ResActionProps) => { 111 const {resource, cluster_name} = props 112 if (resource.kind != 'Deployment') return 114 const {containers} = (resource as any).spec?.template?.spec || {} 115 let lastContainer = _.last(containers) as Record<string, unknown> | undefined 116 if (!lastContainer) { 117 lastContainer = {name: 'busybox'} 118 _.set(resource, 'spec.template.spec.containers', [lastContainer]) 120 Object.assign(lastContainer, {image: 'busybox', command: ['sleep', '99999'], args: []}) 124busybox.cliDescript = 'replaces pod w busybox (when its crashing & you need to exec in)'