🌳
pt0/deployF/k8sF/kubeCliActionsF/ptActionsAI.mts
1import * as _ from 'lodash-es'
2import { spawnSync } from 'child_process'
8export { netbench }
11import type { V1Pod } from '@kubernetes/client-node'
13type CliActionProps = { cluster_name: string, args: string[], kubeConfigPath?: string }
14type CliAction = { (props: CliActionProps): Promise<void>, cliDescript: string }
16export const cleanpods: CliAction = async ({ cluster_name, args }) => {
17 const name = args[0]
18 const shouldDelete = args[1] == 'delete'
20 const allPods = await getAppPods({ cluster_name, name })
21 const delPods = _.filter(allPods, (pod: V1Pod) => {
22 const containerStatuses = pod.status?.containerStatuses
23 if (!containerStatuses || containerStatuses.length != 1) return
24 const { started, state } = containerStatuses[0]
25 if (started) return
26 const finishedAt = _.get(state, 'terminated.finishedAt')
27 if (!finishedAt) return
28 return new Date(finishedAt).getTime() < luxUtcNow().minus({ minutes: 10 }).toMillis()
29 })
31 const delPodNames = _.map(delPods, 'metadata.name')
32 console.log(delPodNames)
34 if (!shouldDelete) {
35 process.exit(0)
36 }
38 const resources = resourcesAsPods(delPods as object[])
39 await resourcesActionLite({ resources, action: 'delete', cluster_name })
40 process.exit(0)
42cleanpods.cliDescript = 'delete old terminated pods'
44export const delpod: CliAction = async ({ cluster_name, args }) => {
45 const name = args[0]
46 await delPodFuzzy({ name, cluster_name })
48delpod.cliDescript = 'delete pod by fuzzy name match'
50export const fexec: CliAction = async ({ cluster_name, args, kubeConfigPath }) => {
51 const name = args[0]
52 const podName = await getMostRecentPod({ cluster_name, name })
53 const execArgs = args.length > 1 ? _.slice(args, 1) : ['sh']
54 const cmdA = ['kubectl', 'exec', podName, '-it', '--', ...execArgs]
55 spawnSync(cmdA[0], cmdA.slice(1), { stdio: 'inherit', env: { ...process.env, KUBECONFIG: kubeConfigPath } })
57fexec.cliDescript = 'exec with fuzzy pod name match'
59export const flogs: CliAction = async ({ cluster_name, args, kubeConfigPath }) => {
60 const name = args[0]
61 const podName = await getMostRecentPod({ cluster_name, name })
62 const cmdA = ['kubectl', 'logs', podName, ..._.slice(args, 1)]
63 await runLogsWithRetry({ kubeConfigPath: kubeConfigPath!, cmdA, noOutCmd: true })
65flogs.cliDescript = 'logs with fuzzy pod name match'
67export const sshlanconfig: CliAction = async () => {
68 const cfg = getKlusterCtx()
69 console.log(JSON.stringify({nodeIpsExtHost: cfg.nodeIpsExtHost, lanNodeIp: cfg.lanNodeIp, clusterVip: cfg.clusterVip}))
71sshlanconfig.cliDescript = 'output SSH LAN config as JSON for MCP git retry'