fix: support follower state helper planes
This commit is contained in:
@@ -1,14 +1,19 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
import { execFileSync } from "node:child_process";
|
||||
import { existsSync } from "node:fs";
|
||||
import https from "node:https";
|
||||
|
||||
const namespace = process.env.NAMESPACE || "";
|
||||
const configMap = process.env.CONFIGMAP || "";
|
||||
const followerIds = parseFollowerIds(process.env.FOLLOWERS_JSON || "[]");
|
||||
const maxTimingStages = Number(process.env.MAX_TIMING_STAGES || "24");
|
||||
const tokenPath = "/var/run/secrets/kubernetes.io/serviceaccount/token";
|
||||
const caPath = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt";
|
||||
const inCluster = Boolean(process.env.KUBERNETES_SERVICE_HOST && existsSync(tokenPath) && existsSync(caPath));
|
||||
const host = process.env.KUBERNETES_SERVICE_HOST;
|
||||
const port = Number(process.env.KUBERNETES_SERVICE_PORT || "443");
|
||||
const token = readFileSync("/var/run/secrets/kubernetes.io/serviceaccount/token", "utf8").trim();
|
||||
const ca = readFileSync("/var/run/secrets/kubernetes.io/serviceaccount/ca.crt");
|
||||
const token = inCluster ? readFileSync(tokenPath, "utf8").trim() : "";
|
||||
const ca = inCluster ? readFileSync(caPath) : null;
|
||||
|
||||
function parseFollowerIds(text) {
|
||||
try {
|
||||
@@ -40,6 +45,7 @@ function request(method, path, body, contentType = "application/json") {
|
||||
}
|
||||
|
||||
async function readConfigMap() {
|
||||
if (!inCluster) return readConfigMapViaKubectl();
|
||||
try {
|
||||
const result = await request("GET", `/api/v1/namespaces/${encodeURIComponent(namespace)}/configmaps/${encodeURIComponent(configMap)}`);
|
||||
if (result.status === 404) return { ok: true, present: false, object: null, error: result.text };
|
||||
@@ -50,6 +56,21 @@ async function readConfigMap() {
|
||||
}
|
||||
}
|
||||
|
||||
function readConfigMapViaKubectl() {
|
||||
try {
|
||||
const stdout = execFileSync("kubectl", ["-n", namespace, "get", "configmap", configMap, "-o", "json"], {
|
||||
encoding: "utf8",
|
||||
maxBuffer: 16 * 1024 * 1024,
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
});
|
||||
return { ok: true, present: true, object: JSON.parse(stdout), error: "" };
|
||||
} catch (error) {
|
||||
const stderr = String(error?.stderr || error?.message || "");
|
||||
if (/not found/i.test(stderr)) return { ok: true, present: false, object: null, error: stderr };
|
||||
return { ok: false, present: false, object: null, error: stderr || "kubectl configmap read failed" };
|
||||
}
|
||||
}
|
||||
|
||||
function compactStateText(text) {
|
||||
if (typeof text !== "string" || text.length === 0) return null;
|
||||
let state;
|
||||
|
||||
Reference in New Issue
Block a user