|
1 |
| -import * as k8s from "@kubernetes/client-node"; |
2 |
| -import { readFileSync } from "fs"; |
| 1 | +import * as fs from "fs"; |
| 2 | +import * as https from "https"; |
3 | 3 |
|
4 |
| -const kc = new k8s.KubeConfig(); |
5 |
| -kc.loadFromCluster(); |
| 4 | +// Define the options interface for type safety |
| 5 | +interface ListPodsOptions { |
| 6 | + labelSelector?: string; // e.g. "app=foo,env=prod" |
| 7 | +} |
| 8 | + |
| 9 | +const NAMESPACE: string = fs |
| 10 | + .readFileSync( |
| 11 | + "/var/run/secrets/kubernetes.io/serviceaccount/namespace", |
| 12 | + "utf8", |
| 13 | + ) |
| 14 | + .trim(); |
| 15 | +const CA: Buffer = fs.readFileSync( |
| 16 | + "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt", |
| 17 | +); |
| 18 | + |
| 19 | +async function listPods(options: ListPodsOptions = {}): Promise<any> { |
| 20 | + try { |
| 21 | + // Read service account details, token could be rotated |
| 22 | + const token = fs |
| 23 | + .readFileSync( |
| 24 | + "/var/run/secrets/kubernetes.io/serviceaccount/token", |
| 25 | + "utf8", |
| 26 | + ) |
| 27 | + .trim(); |
6 | 28 |
|
7 |
| -const k8sApi = kc.makeApiClient(k8s.CoreV1Api); |
| 29 | + // Base API path |
| 30 | + let path = `/api/v1/namespaces/${NAMESPACE}/pods`; |
8 | 31 |
|
9 |
| -// Read the namespace the pod is running in |
10 |
| -const namespace = readFileSync( |
11 |
| - "/var/run/secrets/kubernetes.io/serviceaccount/namespace", |
12 |
| - "utf8", |
13 |
| -).trim(); |
| 32 | + const queryParams: string[] = []; |
| 33 | + if (options.labelSelector) { |
| 34 | + queryParams.push( |
| 35 | + `labelSelector=${encodeURIComponent(options.labelSelector)}`, |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + if (queryParams.length > 0) { |
| 40 | + path += `?${queryParams.join("&")}`; |
| 41 | + } |
| 42 | + |
| 43 | + const query: https.RequestOptions = { |
| 44 | + hostname: "kubernetes.default.svc", |
| 45 | + path, |
| 46 | + method: "GET", |
| 47 | + headers: { |
| 48 | + Authorization: `Bearer ${token}`, |
| 49 | + Accept: "application/json", |
| 50 | + }, |
| 51 | + ca: [CA], |
| 52 | + }; |
| 53 | + |
| 54 | + return new Promise((resolve, reject) => { |
| 55 | + const req = https.request(query, (res) => { |
| 56 | + let data = ""; |
| 57 | + res.on("data", (chunk) => { |
| 58 | + data += chunk; |
| 59 | + }); |
| 60 | + res.on("end", () => { |
| 61 | + if (res.statusCode !== 200) { |
| 62 | + reject( |
| 63 | + new Error( |
| 64 | + `K8S API request failed. status=${res.statusCode}: ${data}`, |
| 65 | + ), |
| 66 | + ); |
| 67 | + } else { |
| 68 | + try { |
| 69 | + resolve(JSON.parse(data)); |
| 70 | + } catch (parseError) { |
| 71 | + reject(parseError); |
| 72 | + } |
| 73 | + } |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + req.on("error", (error) => reject(error)); |
| 78 | + req.end(); |
| 79 | + }); |
| 80 | + } catch (error) { |
| 81 | + throw new Error( |
| 82 | + `Failed to read service account files: ${(error as Error).message}`, |
| 83 | + ); |
| 84 | + } |
| 85 | +} |
14 | 86 |
|
15 | 87 | export async function getAddressesFromK8sApi(): Promise<
|
16 | 88 | { name: string; podIP: string }[]
|
17 | 89 | > {
|
18 |
| - try { |
19 |
| - const labelSelector = "run=hub-conat-router"; |
20 |
| - const res = await k8sApi.listNamespacedPod({ namespace, labelSelector }); |
21 |
| - |
22 |
| - const ret: { name: string; podIP: string }[] = []; |
23 |
| - for (const pod of res.items) { |
24 |
| - const name = pod.metadata?.name; |
25 |
| - const podIP = pod.status?.podIP; |
26 |
| - if (name && podIP) { |
27 |
| - ret.push({ name, podIP }); |
28 |
| - } |
| 90 | + const res = await listPods({ labelSelector: "run=hub-conat-router" }); |
| 91 | + const ret: { name: string; podIP: string }[] = []; |
| 92 | + for (const pod of res.items) { |
| 93 | + const name = pod.metadata?.name; |
| 94 | + const podIP = pod.status?.podIP; |
| 95 | + if (name && podIP) { |
| 96 | + ret.push({ name, podIP }); |
29 | 97 | }
|
30 |
| - return ret; |
31 |
| - } catch (err) { |
32 |
| - console.error("Error fetching addresses from Kubernetes API:", err); |
33 |
| - return []; |
34 | 98 | }
|
| 99 | + return ret; |
35 | 100 | }
|
0 commit comments