1import * as _ from 'lodash-es' 2import { spawnSync } from 'child_process' 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 }) => { 18 const shouldDelete = args[1] == 'delete' 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] 26 const finishedAt = _.get(state, 'terminated.finishedAt') 27 if (!finishedAt) return 28 return new Date(finishedAt).getTime() < luxUtcNow().minus({ minutes: 10 }).toMillis() 31 const delPodNames = _.map(delPods, 'metadata.name') 32 console.log(delPodNames) 42cleanpods.cliDescript = 'delete old terminated pods' 44export const delpod: CliAction = async ({ cluster_name, args }) => { 48delpod.cliDescript = 'delete pod by fuzzy name match' 50export const fexec: CliAction = async ({ cluster_name, args, kubeConfigPath }) => { 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 }) => { 62 const cmdA = ['kubectl', 'logs', podName, ..._.slice(args, 1)] 65flogs.cliDescript = 'logs with fuzzy pod name match' 67export const sshlanconfig: CliAction = async () => { 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'