fix: strip runtime gitops monitoring list items

This commit is contained in:
Codex
2026-07-04 17:03:34 +00:00
parent e1bebbf2d4
commit c5e8a72bc7
2 changed files with 97 additions and 5 deletions
@@ -42,6 +42,8 @@ function postprocessRuntimeGitops() {
function stripPrometheusOperatorResourcesFromFile(file) {
const original = readFileSync(file, "utf8");
const jsonResult = stripPrometheusOperatorResourcesFromJsonFile(file, original);
if (jsonResult !== null) return jsonResult;
const docs = splitYamlDocuments(original);
const nextDocs = docs.filter((doc) => !isPrometheusOperatorDocument(doc));
if (nextDocs.length === docs.length) return "unchanged";
@@ -53,6 +55,29 @@ function stripPrometheusOperatorResourcesFromFile(file) {
return "changed";
}
function stripPrometheusOperatorResourcesFromJsonFile(file, text) {
const parsed = parseJsonDocument(text);
if (parsed === null) return null;
const next = stripPrometheusOperatorResourcesFromJsonValue(parsed);
if (!next.changed) return "unchanged";
if (next.value === null) {
unlinkSync(file);
return "deleted";
}
writeFileSync(file, `${JSON.stringify(next.value, null, 2)}\n`, "utf8");
return "changed";
}
function stripPrometheusOperatorResourcesFromJsonValue(value) {
if (isPrometheusOperatorResource(value)) return { changed: true, value: null };
if (isKubernetesList(value)) {
const originalItems = Array.isArray(value.items) ? value.items : [];
const items = originalItems.filter((item) => !isPrometheusOperatorResource(item));
if (items.length !== originalItems.length) return { changed: true, value: { ...value, items } };
}
return { changed: false, value };
}
function pruneMissingKustomizationResources() {
const file = path.join(runtimeDir, "kustomization.yaml");
if (!existsSync(file)) return false;
@@ -94,6 +119,33 @@ function isPrometheusOperatorDocument(text) {
return Boolean(api && kind && prometheusOperatorKinds.has(kind[1]));
}
function isPrometheusOperatorResource(value) {
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
return typeof value.apiVersion === "string"
&& value.apiVersion.startsWith("monitoring.coreos.com/")
&& typeof value.kind === "string"
&& prometheusOperatorKinds.has(value.kind);
}
function isKubernetesList(value) {
return value
&& typeof value === "object"
&& !Array.isArray(value)
&& value.apiVersion === "v1"
&& value.kind === "List"
&& Array.isArray(value.items);
}
function parseJsonDocument(text) {
const trimmed = text.trim();
if (!trimmed.startsWith("{") && !trimmed.startsWith("[")) return null;
try {
return JSON.parse(trimmed);
} catch {
return null;
}
}
function leadingSpaces(value) {
const match = value.match(/^ */u);
return match ? match[0].length : 0;