🌳
pt0/deployF/k8sF/getClusterPodsF.mts
1import * as _ from 'lodash-es'
2import { CoreV1Api, type V1Pod } from '@kubernetes/client-node'
5import { getKubeApis } from './getApisF.mts'
9import { type WithClusterName } from './ctxF/klusterCtxF.mts'
13type Get1ClusterPodsProps = WithClusterName & {
14 labels?: Record<string, string>
15 fuzzyPodName?: string
16 limit?: number
19export const get1ClusterPods = async ({cluster_name, labels, fuzzyPodName, limit}: Get1ClusterPodsProps) => {
20 const offlineReason = getOfflineReason(cluster_name)
21 if (offlineReason) {
22 throPtErr('cluster offline', {cluster_name, offlineReason, what: 'listNamespacedPod'})
23 }
25 const {kubeConfig} = await getKubeApis({cluster_name})
26 const podsApi = kubeConfig.makeApiClient(CoreV1Api)
28 let labelQueryStr
29 if (labels) {
30 labelQueryStr = _.map(labels, (val, key) => `${key}=${val}`).join(',')
31 }
32 const maxLen = 500
33 if (fuzzyPodName) {
34 betVerboseLog(`setting limit=${maxLen} since fuzzyPodName`)
35 limit = maxLen
36 }
38 const timeoutMs = getKubeTimeoutSec(cluster_name) * 1000
39 let {items: pods} = await podsApi.listNamespacedPod({namespace: 'default', limit, labelSelector: labelQueryStr, signal: AbortSignal.timeout(timeoutMs)} as any)
41 pods = resourcesAsPods(pods)
42 pods = _.chain(pods).map((pod) => {
43 assertDefined(pod.metadata)
44 delete pod.metadata.managedFields
45 return pod
46 }).sortBy((pod) => {
47 return pod.metadata?.creationTimestamp
48 }).reverse().value()
49 const podsLen = pods.length
50 throwIf(() => podsLen == maxLen) // could be missing results
51 if (fuzzyPodName) {
52 pods = pods.filter((pod) => pod.metadata?.name?.includes(fuzzyPodName))
53 throwIf(() => pods.length > 1, {fuzzyPodName})
54 }
56 return pods