1import * as _ from 'lodash-es'2import { eptKubeCli } from '../cliF/eptKubeCliF.mts'3import { get1ClusterPods } from '../getClusterPodsF.mts'4import { resourcesActionLite } from '../resourcesActionLiteF.mts'5import { assertDefined } from '../../../sharedF/assertsF/assertDefinedF.mts'6import { throwIf } from '../../../sharedF/throwErrorF/throwIfF.mts'7import type { WithClusterName, WithClusterAndName } from '../ctxF/klusterCtxF.mts'8import type { V1Pod } from '@kubernetes/client-node'10type Get1ClusterPodsProps = WithClusterName & { labels?: Record<string, string>, fuzzyPodName?: string, limit?: number }11const get1Pods = get1ClusterPods as (props: Get1ClusterPodsProps) => Promise<V1Pod[]>13export const getAppPods = async ({cluster_name, name}: WithClusterAndName) => {14 return await get1Pods({cluster_name, labels: {name}})15}17export const getMostRecentPod = async ({cluster_name, name}: WithClusterAndName) => {18 let pods = await getAppPods({cluster_name, name})19 if (!pods.length) {20 // Fallback to fuzzy pod name match for CLI usage (e.g. `flogs nimbus` matching `nimbus-mainnet-...`)21 pods = await get1Pods({cluster_name, fuzzyPodName: name})22 }23 const pod = _.first(pods)24 assertDefined(pod)25 const podName = pod.metadata?.name26 assertDefined(podName)27 return podName28}30export const delPodFuzzy = async ({name, cluster_name}: WithClusterAndName) => {31 const podName = await getMostRecentPod({cluster_name, name})32 return await eptKubeCli([cluster_name, 'delete', 'pod', podName])33}35type DelMatchingPodsProps = WithClusterName & { labels: Record<string, string> }36export const delMatchingPods = async ({cluster_name, labels}: DelMatchingPodsProps) => {37 assertDefined(labels)38 const resources = await get1Pods({cluster_name, labels})40 await resourcesActionLite({resources, action: 'delete'})41}