🌳
pt0/deployF/k8sF/getSinglePodF.mts
1import * as _ from 'lodash-es'
2import type { V1Pod } from '@kubernetes/client-node'
7type GetSinglePodProps = WithClusterName & {
8 labels?: Record<string, string>
9 fuzzyPodName?: string
12export const resourcesAsPods = <T extends object>(resources: T[]) => {
13 return _.map(resources, (res) => {
14 (res as T & {apiVersion: string, kind: string}).apiVersion = 'v1'
15 ;(res as T & {apiVersion: string, kind: string}).kind = 'Pod'
16 return res
17 })
20const podToInfo = (pod: V1Pod) => {
21 assertDefined(pod.metadata?.name)
22 assertDefined(pod.spec?.containers)
23 const podName = pod.metadata.name
24 const containerNames = _.map(pod.spec.containers, 'name') as string[]
25 const containerName = containerNames[0]
26 const git_sha = pod.metadata?.labels?.git_sha
27 return {podName, containerName, containerNames, git_sha}
30export const getSinglePod = async (props: GetSinglePodProps) => {
31 // Don't pass limit to k8s API - it applies before sorting, so we'd get arbitrary pod instead of newest
32 const pod = (await get1ClusterPods({...props, labels: props.labels, fuzzyPodName: props.fuzzyPodName}))[0] as V1Pod | undefined
33 if (!pod) return
34 return podToInfo(pod)
37export const getAllPods = async (props: GetSinglePodProps) => {
38 const pods = await get1ClusterPods({...props, labels: props.labels})
39 return _.map(pods, podToInfo)