Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(yaml): a Kubernetes manifest mints no Variables for its envelope

Martin Vogelb3e66e1bf1d0
diff --git a/internal/cbm/extract_defs.c b/internal/cbm/extract_defs.c
index 2a3b9d14d..ddf165d8c 100644
--- a/internal/cbm/extract_defs.c
+++ b/internal/cbm/extract_defs.c
@@ -9961,6 +9961,31 @@ static void extract_yaml_toplevel_keys(CBMExtractCtx *ctx, TSNode root) {
}
}
+// True when the YAML document's top level holds both `apiVersion` and `kind`:
+// a Kubernetes object, whose top-level keys are its envelope.
+static bool yaml_is_k8s_object(CBMExtractCtx *ctx, TSNode root) {
+ TSNode bm = find_yaml_toplevel_mapping(root);
+ if (ts_node_is_null(bm)) {
+ return false;
+ }
+ bool api_version = false;
+ bool kind = false;
+ uint32_t n = ts_node_named_child_count(bm);
+ for (uint32_t i = 0; i < n && !(api_version && kind); i++) {
+ TSNode pair = ts_node_named_child(bm, i);
+ if (strcmp(ts_node_type(pair), "block_mapping_pair") != 0) {
+ continue;
+ }
+ TSNode key = ts_node_child_by_field_name(pair, TS_FIELD("key"));
+ const char *k = ts_node_is_null(key) ? NULL : cbm_node_text(ctx->arena, key, ctx->source);
+ if (k) {
+ api_version = api_version || strcmp(k, "apiVersion") == 0;
+ kind = kind || strcmp(k, "kind") == 0;
+ }
+ }
+ return api_version && kind;
+}
+
static void extract_variables(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec) {
if (!spec->variable_node_types || !spec->variable_node_types[0]) {
return;
@@ -9972,6 +9997,13 @@ static void extract_variables(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec
return;
}
+ // A Kubernetes object's top-level keys (apiVersion, kind, metadata, spec,
+ // status) are its envelope, which its Resource node carries; as Variables
+ // they repeated in every manifest and fed name-similarity config links.
+ if (ctx->language == CBM_LANG_YAML && yaml_is_k8s_object(ctx, root)) {
+ return;
+ }
+
// Config languages with nested structure: use recursive walk
if (ctx->language == CBM_LANG_YAML || ctx->language == CBM_LANG_TOML ||
ctx->language == CBM_LANG_INI || ctx->language == CBM_LANG_JSON) {
diff --git a/tests/test_grammar_probe_f.c b/tests/test_grammar_probe_f.c
index ec569c312..8daf6d9c9 100644
--- a/tests/test_grammar_probe_f.c
+++ b/tests/test_grammar_probe_f.c
@@ -139,6 +139,7 @@ typedef struct {
int classes; /* Class label */
int resources; /* Resource label (k8s) */
int charts; /* Chart label (helm) */
+ int variables; /* Variable label */
int imports; /* IMPORTS edges */
int depends_on; /* DEPENDS_ON edges */
int infra_maps; /* INFRA_MAPS edges */
@@ -156,6 +157,7 @@ static GpfMetrics gpf_metrics_files(const GpfFile *files, int nfiles) {
m.classes = gpf_count_label(store, lp.project, "Class");
m.resources = gpf_count_label(store, lp.project, "Resource");
m.charts = gpf_count_label(store, lp.project, "Chart");
+ m.variables = gpf_count_label(store, lp.project, "Variable");
m.imports = cbm_store_count_edges_by_type(store, lp.project, "IMPORTS");
m.depends_on = cbm_store_count_edges_by_type(store, lp.project, "DEPENDS_ON");
m.infra_maps = cbm_store_count_edges_by_type(store, lp.project, "INFRA_MAPS");
@@ -743,6 +745,38 @@ TEST(probe_kustomize_extensionless_file) {
PASS();
}
+/* A K8s object's top-level keys (apiVersion, kind, metadata, spec, status) are
+ * its envelope, carried by its Resource node; as Variables they repeated per
+ * manifest (deckhouse: `kind` 1,358, `apiVersion` 1,347, `metadata` 1,336)
+ * and fed name-similarity config links. */
+TEST(probe_k8s_manifest_mints_no_envelope_variables) {
+ GpfMetrics m = gpf_metrics("deploy.yaml",
+ "# a manifest\n"
+ "apiVersion: apps/v1\n"
+ "kind: Deployment\n"
+ "metadata:\n"
+ " name: web\n"
+ "spec:\n"
+ " replicas: 2\n"
+ "status: {}\n");
+ ASSERT_TRUE(m.ok);
+ ASSERT_EQ(m.resources, 1);
+ ASSERT_EQ(m.variables, 0);
+ PASS();
+}
+
+/* A plain config YAML keeps its top-level keys as Variables. */
+TEST(probe_yaml_config_keeps_variables) {
+ GpfMetrics m = gpf_metrics("config.yaml",
+ "server:\n"
+ " port: 8080\n"
+ "kind: production\n"
+ "replicas: 3\n");
+ ASSERT_TRUE(m.ok);
+ ASSERT_TRUE(m.variables >= 3);
+ PASS();
+}
+
/* A `file://` dependency is the chart in that directory of this repository,
* not a published one: `app` depends on the local `common`, and no external
* Chart node stands in for it. `common` is read after `app`. */
@@ -2000,6 +2034,8 @@ SUITE(grammar_probe_f) {
RUN_TEST(probe_kustomize_extensionless_file);
RUN_TEST(probe_kustomize_remote_base_names_no_symbol);
RUN_TEST(probe_helm_file_dependency_links_local_chart);
+ RUN_TEST(probe_k8s_manifest_mints_no_envelope_variables);
+ RUN_TEST(probe_yaml_config_keeps_variables);
RUN_TEST(probe_helm_unresolved_file_dependency_stays_external);
RUN_TEST(probe_kustomize_nested_sibling_entries);
RUN_TEST(probe_kustomize_single_module);