Skip to content

Commit 856d7f9

Browse files
committed
hub/conat: discover router directly via k8s api
1 parent 8961fe5 commit 856d7f9

File tree

4 files changed

+90
-30
lines changed

4 files changed

+90
-30
lines changed

src/packages/hub/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
"@cocalc/static": "workspace:*",
1919
"@cocalc/util": "workspace:*",
2020
"@isaacs/ttlcache": "^1.4.1",
21-
"@kubernetes/client-node": "^1.3.0",
2221
"@types/formidable": "^3.4.5",
2322
"async": "^1.5.2",
2423
"awaiting": "^3.0.0",

src/packages/pnpm-lock.yaml

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 90 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,100 @@
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";
33

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();
628

7-
const k8sApi = kc.makeApiClient(k8s.CoreV1Api);
29+
// Base API path
30+
let path = `/api/v1/namespaces/${NAMESPACE}/pods`;
831

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+
}
1486

1587
export async function getAddressesFromK8sApi(): Promise<
1688
{ name: string; podIP: string }[]
1789
> {
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 });
2997
}
30-
return ret;
31-
} catch (err) {
32-
console.error("Error fetching addresses from Kubernetes API:", err);
33-
return [];
3498
}
99+
return ret;
35100
}

src/packages/server/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
"@google-cloud/storage-transfer": "^3.3.0",
5757
"@google/generative-ai": "^0.14.0",
5858
"@isaacs/ttlcache": "^1.4.1",
59-
"@kubernetes/client-node": "^1.3.0",
6059
"@langchain/anthropic": "^0.3.18",
6160
"@langchain/core": "^0.3.46",
6261
"@langchain/google-genai": "^0.2.4",

0 commit comments

Comments
 (0)