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
+45 -5
View File
@@ -28,8 +28,7 @@ function findPrometheusOperatorResources() {
for (const file of listYamlFiles(runtimeDir)) {
const rel = path.relative(repoDir, file);
for (const doc of splitYamlDocuments(readFileSync(file, "utf8"))) {
const ref = prometheusOperatorResourceRef(doc, rel);
if (ref !== null) refs.push(ref);
refs.push(...prometheusOperatorResourceRefs(doc, rel));
}
}
return refs;
@@ -39,12 +38,53 @@ function splitYamlDocuments(text) {
return text.split(/^---[ \t]*(?:#.*)?$/mu).map((doc) => doc.trim()).filter(Boolean);
}
function prometheusOperatorResourceRef(text, file) {
function prometheusOperatorResourceRefs(text, file) {
const jsonRefs = prometheusOperatorJsonResourceRefs(text, file);
if (jsonRefs !== null) return jsonRefs;
const api = text.match(/^\s*apiVersion:\s*["']?(monitoring\.coreos\.com\/[^"'\s#]+)["']?\s*(?:#.*)?$/mu);
const kind = text.match(/^\s*kind:\s*["']?([^"'\s#]+)["']?\s*(?:#.*)?$/mu);
if (!api || !kind || !prometheusOperatorKinds.has(kind[1])) return null;
if (!api || !kind || !prometheusOperatorKinds.has(kind[1])) return [];
const name = text.match(/^\s*name:\s*["']?([^"'\s#]+)["']?\s*(?:#.*)?$/mu);
return { file, kind: kind[1], name: name ? name[1] : null, container: null };
return [{ file, kind: kind[1], name: name ? name[1] : null, container: null }];
}
function prometheusOperatorJsonResourceRefs(text, file) {
const parsed = parseJsonDocument(text);
if (parsed === null) return null;
const items = isKubernetesList(parsed) ? parsed.items : [parsed];
return items.filter(isPrometheusOperatorResource).map((item) => ({
file,
kind: item.kind,
name: typeof item.metadata?.name === "string" ? item.metadata.name : null,
container: isKubernetesList(parsed) ? "List.items" : null,
}));
}
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 listYamlFiles(root) {