1import * as _ from 'lodash-es' 2import { KubernetesObjectApi, KubeConfig, type RequestContext } from '@kubernetes/client-node' 13export const getKubeApis = async (props: {cluster_name?: string, kubeTlsInsecure?: boolean} = {}) => { 14 const {cluster_name, kubeTlsInsecure} = getKlusterCtx(props) as KlusterCfg 17 const kubeConfig = new KubeConfig() 26 kubeConfig.loadFromString(klusterCfgStr) 28 if (kubeTlsInsecure) { 29 _.each(kubeConfig.clusters, (cluster) => { 30 (cluster as {skipTLSVerify?: boolean}).skipTLSVerify = true 33 const observableOf = <T,>(v: T) => ({toPromise: () => Promise.resolve(v)}) 35 const timeoutMiddleware = { 36 pre: (ctx: RequestContext) => { ctx.setSignal(AbortSignal.timeout(timeoutMs)); return observableOf(ctx) }, 37 post: (rsp: unknown) => observableOf(rsp), 40 // Patch makeApiClient so ALL clients (CoreV1Api, BatchV1Api, etc.) get the timeout, 41 // not just objectApi. Without this, makeApiClient-created clients hang ~75s on TCP 42 // ETIMEDOUT because KubeConfig.makeApiClient creates a fresh Configuration with empty middleware[]. 43 const origMakeApiClient = kubeConfig.makeApiClient.bind(kubeConfig) 44 kubeConfig.makeApiClient = function<T extends new (...a: any[]) => any>(apiClientType: T): InstanceType<T> { 45 const client = origMakeApiClient(apiClientType as any) as any 46 client.configuration?.middleware?.push(timeoutMiddleware) 50 const objectApi = KubernetesObjectApi.makeApiClient(kubeConfig) 52 return {objectApi, kubeConfig}