Merge pull request #3097 from DeusData/feat/kustomize-file-references

feat(pipeline): a kustomization references the files its generators read
This commit is contained in:
Martin Vogel
2026-11-27 01:53:58 +01:00
committed by GitHub
5 changed files with 345 additions and 7 deletions
+9 -2
View File
@@ -899,7 +899,13 @@ typedef struct {
* kind / group / name / ns; `path` is
* the patch file (as written, relative to the kustomization), NULL for an
* inline patch. kind and name both NULL: the patch file's documents name
* them. def_index -1: the kustomization itself is the source. */
* them. def_index -1: the kustomization itself is the source.
* CBM_K8S_FACT_FILE: a kustomization reads the file `path` (as written,
* relative to the kustomization) at `line`, named by `via`: an entry of a
* configMapGenerator's or secretGenerator's `files` (the path after
* `key=`), `envs` or `env`, or of `configurations`. The file is input to
* the build, not a member of it. def_index -1: the kustomization itself
* is the source. */
typedef enum {
CBM_K8S_FACT_REF = 0,
CBM_K8S_FACT_CRD,
@@ -908,7 +914,8 @@ typedef enum {
CBM_K8S_FACT_SOURCE,
CBM_K8S_FACT_IMAGE,
CBM_K8S_FACT_ENV,
CBM_K8S_FACT_PATCH
CBM_K8S_FACT_PATCH,
CBM_K8S_FACT_FILE
} CBMK8sFactKind;
typedef struct {
CBMK8sFactKind fact;
+85
View File
@@ -515,6 +515,79 @@ static void emit_kustomize_charts(CBMExtractCtx *ctx, TSNode seq) {
}
}
static const char KUST_CONFIGMAP_GEN[] = "configMapGenerator";
static const char KUST_SECRET_GEN[] = "secretGenerator";
static const char KUST_CONFIGURATIONS[] = "configurations";
// A FILE fact: the kustomization reads `path`, named by `via` (one of the
// static names above) at the row of `at`.
static void emit_kustomize_file(CBMExtractCtx *ctx, const char *path, const char *via, TSNode at) {
if (!path || !path[0]) {
return;
}
CBMK8sFact f = {.fact = CBM_K8S_FACT_FILE,
.def_index = -1,
.via = via,
.path = path,
.line = (int)ts_node_start_point(at).row + TS_LINE_OFFSET};
cbm_k8sfact_push(&ctx->result->k8s_facts, ctx->arena, f);
}
// One FILE fact per scalar item of sequence `seq`. `keyed`: an item may be
// `key=path` (a generator's `files`), and the path is what follows the `=`.
static void emit_kustomize_file_items(CBMExtractCtx *ctx, TSNode seq, const char *via, bool keyed) {
k8s_kids_t items = k8s_kids(seq);
TSNode item;
while (k8s_kid(&items, false, &item)) {
if (strcmp(ts_node_type(item), "block_sequence_item") != 0) {
continue;
}
k8s_kids_t vals = k8s_kids(item);
TSNode val;
while (k8s_kid(&vals, false, &val)) {
const char *text = get_scalar_text(ctx->arena, val, ctx->source);
const char *eq = text && keyed ? strchr(text, '=') : NULL;
emit_kustomize_file(ctx, eq ? eq + SKIP_ONE : text, via, val);
}
}
}
// The generators of a `configMapGenerator` / `secretGenerator` list: the
// files each reads -- `files`, `envs`, and the single `env` of older
// kustomize. Literals name no file.
static void emit_kustomize_generators(CBMExtractCtx *ctx, TSNode seq, const char *via) {
CBMArena *a = ctx->arena;
k8s_kids_t items = k8s_kids(seq);
TSNode item;
while (k8s_kid(&items, false, &item)) {
TSNode map = strcmp(ts_node_type(item), "block_sequence_item") == 0
? kust_item_mapping(item)
: (TSNode){0};
if (ts_node_is_null(map)) {
continue;
}
k8s_kids_t pairs = k8s_kids(map);
TSNode pair;
while (k8s_kid(&pairs, false, &pair)) {
if (strcmp(ts_node_type(pair), "block_mapping_pair") != 0) {
continue;
}
const char *key = get_scalar_text(a, ts_node_named_child(pair, 0), ctx->source);
TSNode v = unwrap_pair_value(pair);
if (!key || ts_node_is_null(v)) {
continue;
}
bool files = strcmp(key, "files") == 0;
if ((files || strcmp(key, "envs") == 0) &&
strcmp(ts_node_type(v), "block_sequence") == 0) {
emit_kustomize_file_items(ctx, v, via, files);
} else if (strcmp(key, "env") == 0) {
emit_kustomize_file(ctx, get_scalar_text(a, v, ctx->source), via, v);
}
}
}
}
// Process a single block_mapping_pair for kustomize list keys.
static void process_kustomize_pair(CBMExtractCtx *ctx, TSNode pair) {
if (strcmp(ts_node_type(pair), "block_mapping_pair") != 0) {
@@ -536,6 +609,18 @@ static void process_kustomize_pair(CBMExtractCtx *ctx, TSNode pair) {
emit_kustomize_charts(ctx, val_node);
return;
}
if (strcmp(key_text, KUST_CONFIGMAP_GEN) == 0) {
emit_kustomize_generators(ctx, val_node, KUST_CONFIGMAP_GEN);
return;
}
if (strcmp(key_text, KUST_SECRET_GEN) == 0) {
emit_kustomize_generators(ctx, val_node, KUST_SECRET_GEN);
return;
}
if (strcmp(key_text, KUST_CONFIGURATIONS) == 0) {
emit_kustomize_file_items(ctx, val_node, KUST_CONFIGURATIONS, false);
return;
}
if (!is_kustomize_list_key(key_text)) {
return;
}
+114 -5
View File
@@ -1322,6 +1322,15 @@ typedef struct {
int line;
} k8s_kchart_t;
/* A file a kustomization reads (extract_k8s.c FILE fact): input to its
* build, no member of it. */
typedef struct {
char *kust; /* the kustomization file */
char *path; /* the file, from the repository root */
const char *via; /* static: configMapGenerator, secretGenerator, configurations */
int line;
} k8s_kfile_t;
typedef struct {
k8s_ref_t *refs;
int ref_n;
@@ -1350,6 +1359,9 @@ typedef struct {
k8s_kchart_t *kcharts;
int kchart_n;
int kchart_cap;
k8s_kfile_t *kfiles;
int kfile_n;
int kfile_cap;
} k8s_facts_t;
static void k8s_facts_free(k8s_facts_t *f) {
@@ -1407,6 +1419,11 @@ static void k8s_facts_free(k8s_facts_t *f) {
cbm_free(CBM_MEM_CLASS_OTHER, c->values);
}
cbm_free(CBM_MEM_CLASS_OTHER, f->kcharts);
for (int i = 0; i < f->kfile_n; i++) {
cbm_free(CBM_MEM_CLASS_OTHER, f->kfiles[i].kust);
cbm_free(CBM_MEM_CLASS_OTHER, f->kfiles[i].path);
}
cbm_free(CBM_MEM_CLASS_OTHER, f->kfiles);
cbm_free(CBM_MEM_CLASS_OTHER, f->envs);
cbm_free(CBM_MEM_CLASS_OTHER, f->hrs);
cbm_free(CBM_MEM_CLASS_OTHER, f->srcs);
@@ -5368,21 +5385,59 @@ static void k8s_kchart_keep(k8s_facts_t *f, const char *kust, const CBMK8sFact *
.line = x->line};
}
/* Keep a kustomization's facts: its patches (PATCH) and the charts it
* inflates (DEPLOY via "kustomize"), each file as a repository path -- a
* patch file outside the repository (or a URL) names no file of it, a values
* file there is left out. */
/* The static name of a FILE fact's `via`; NULL: not one of ours. */
static const char *k8s_kfile_via(const char *via) {
static const char *const names[] = {"configMapGenerator", "secretGenerator", "configurations"};
for (size_t i = 0; via && i < sizeof(names) / sizeof(names[0]); i++) {
if (strcmp(via, names[i]) == 0) {
return names[i];
}
}
return NULL;
}
/* A file kustomization `kust` reads, at repository path `path`. */
static void k8s_kfile_keep(k8s_facts_t *f, const char *kust, const CBMK8sFact *x,
const char *path) {
const char *via = k8s_kfile_via(x->via);
if (!via || !k8s_grow((void **)&f->kfiles, &f->kfile_cap, f->kfile_n, sizeof(*f->kfiles))) {
return;
}
char *kust_copy = cbm_mem_strdup(CBM_MEM_CLASS_OTHER, kust);
char *path_copy = cbm_mem_strdup(CBM_MEM_CLASS_OTHER, path);
if (!kust_copy || !path_copy) {
cbm_free(CBM_MEM_CLASS_OTHER, kust_copy);
cbm_free(CBM_MEM_CLASS_OTHER, path_copy);
return;
}
f->kfiles[f->kfile_n++] =
(k8s_kfile_t){.kust = kust_copy, .path = path_copy, .via = via, .line = x->line};
}
/* Keep a kustomization's facts: its patches (PATCH), the charts it inflates
* (DEPLOY via "kustomize") and the files it reads (FILE), each file as a
* repository path -- a patch file outside the repository (or a URL) names no
* file of it, a values file or a read file there is left out. */
static void k8s_kustomize_keep(k8s_facts_t *f, const char *kust, const CBMFileResult *res) {
char dir[CBM_SZ_1K];
k8s_dirname(kust, dir, sizeof(dir));
for (int i = 0; res && i < res->k8s_facts.count; i++) {
const CBMK8sFact *x = &res->k8s_facts.items[i];
bool chart = x->fact == CBM_K8S_FACT_DEPLOY && x->via && strcmp(x->via, "kustomize") == 0;
if (x->fact != CBM_K8S_FACT_PATCH && !chart) {
bool file = x->fact == CBM_K8S_FACT_FILE;
if (x->fact != CBM_K8S_FACT_PATCH && !chart && !file) {
continue;
}
char path[CBM_SZ_1K];
bool in_repo = x->path && k8s_repo_path(dir, x->path, path, sizeof(path));
if (file) {
/* An absolute path names a file of the machine, not of the
* repository: k8s_repo_path would read it from the directory. */
if (in_repo && x->path[0] != '/') {
k8s_kfile_keep(f, kust, x, path);
}
continue;
}
if (chart) {
k8s_kchart_keep(f, kust, x, in_repo ? path : NULL);
continue;
@@ -5645,6 +5700,59 @@ static void k8s_patch_builds(const k8s_kusts_t *ks, const k8s_ints_t *inc, int k
}
}
static int k8s_cmp_kfile(const void *a, const void *b) {
const k8s_kfile_t *x = a;
const k8s_kfile_t *y = b;
int c = strcmp(x->kust, y->kust);
if (c == 0) {
c = (x->line > y->line) - (x->line < y->line);
}
return c ? c : strcmp(x->path, y->path);
}
/* The files kustomizations read -> REFERENCES_FILE {via: configMapGenerator |
* secretGenerator | configurations, line}, the kustomization's File to the
* file's File: once per kustomization and file, at the first line naming it.
* A file the index does not hold (a generated kubeconfig, a secret kept out
* of the repository) is none. Never an IMPORTS: that would make the file a
* member of the build (k8s_kusts_wire). */
static void k8s_link_kfiles(cbm_pipeline_ctx_t *ctx, k8s_facts_t *f) {
if (f->kfile_n == 0) {
return;
}
k8s_qsort(f->kfiles, (size_t)f->kfile_n, sizeof(*f->kfiles), k8s_cmp_kfile);
CBMArena keys;
cbm_arena_init_lazy(&keys, CBM_SZ_4K);
CBMHashTable *seen = cbm_ht_create((uint32_t)f->kfile_n);
int edges = 0;
for (int i = 0; seen && i < f->kfile_n; i++) {
const k8s_kfile_t *x = &f->kfiles[i];
const cbm_gbuf_node_t *tgt = cbm_pipeline_file_node(ctx->gbuf, ctx->project_name, x->path);
int64_t src = k8s_file_of(ctx, x->kust);
/* a File's QN folds `/` and `.` alike: the path itself must be the file's */
if (src <= 0 || !tgt || !tgt->file_path || strcmp(tgt->file_path, x->path) != 0 ||
tgt->id == src) {
continue;
}
const char *key = cbm_arena_sprintf(&keys, "%s\x01%s", x->kust, x->path);
if (!key || cbm_ht_get(seen, key)) {
continue;
}
cbm_ht_set(seen, key, (void *)key);
char props[CBM_SZ_128];
snprintf(props, sizeof(props), "{\"via\":\"%s\",\"line\":%d}", x->via, x->line);
cbm_gbuf_insert_edge(ctx->gbuf, src, tgt->id, "REFERENCES_FILE", props);
edges++;
}
if (seen) {
cbm_ht_free(seen);
}
cbm_arena_destroy(&keys);
if (edges > 0) {
cbm_log_info("pass.k8s.kustomize_files", "edges", itoa_k8s(edges));
}
}
/* Kustomize patches -> OVERRIDE {via: patch, kustomization, patch, line},
* child -> base: each patch against the build of the kustomization stating
* it, a component's against the build of each kustomization including it
@@ -6111,6 +6219,7 @@ int cbm_pipeline_pass_k8s(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files,
k8s_helm_collect(ctx, &facts, &helm);
k8s_link_envs(ctx, &facts);
k8s_link_patches(ctx, &kusts, &facts);
k8s_link_kfiles(ctx, &facts);
k8s_link_renames(ctx, &kusts);
k8s_facts_free(&facts);
k8s_records_free(&recs);
+106
View File
@@ -3096,6 +3096,111 @@ TEST(probe_kustomize_helm_charts_deploy_their_chart) {
PASS();
}
/* The REFERENCES_FILE edges a kustomization draws for the files it reads, as
* "<kustomization> -> <target file> (via:line)", sorted, one per line; then a
* line "imports <files>": the targets of the IMPORTS edges leaving
* kustomization files. */
static char *gpf_kustomize_files(const GpfFile *files, int nfiles) {
GpfProj lp;
cbm_store_t *store = gpf_index_files(&lp, files, nfiles);
char *out = NULL;
if (store) {
sqlite3_stmt *st = NULL;
const char *sql =
"SELECT coalesce((SELECT group_concat(x, char(10)) FROM (SELECT "
"s.file_path || ' -> ' || t.file_path || ' (' || "
"json_extract(e.properties,'$.via') || ':' || json_extract(e.properties,'$.line') "
"|| ')' AS x "
"FROM edges e JOIN nodes s ON s.id=e.source_id JOIN nodes t ON t.id=e.target_id "
"WHERE e.project=?1 AND e.type='REFERENCES_FILE' AND json_extract(e.properties,'$.via') "
"IN ('configMapGenerator','secretGenerator','configurations') ORDER BY x)), '') "
"|| char(10) || 'imports ' || coalesce((SELECT group_concat(x, ',') FROM (SELECT "
"DISTINCT t.file_path AS x "
"FROM edges e JOIN nodes s ON s.id=e.source_id JOIN nodes t ON t.id=e.target_id "
"WHERE e.project=?1 AND e.type='IMPORTS' AND s.file_path LIKE '%kustomization.yaml' "
"ORDER BY x)), '')";
if (sqlite3_prepare_v2(cbm_store_get_db(store), sql, -1, &st, NULL) == SQLITE_OK) {
sqlite3_bind_text(st, 1, lp.project, -1, SQLITE_STATIC);
if (sqlite3_step(st) == SQLITE_ROW && sqlite3_column_text(st, 0)) {
out = strdup((const char *)sqlite3_column_text(st, 0));
}
}
sqlite3_finalize(st);
}
gpf_cleanup(&lp, store);
return out;
}
/* A kustomization reads files that are input to its build without being a
* member of it: a generator's `files` (a path, or `key=path`) and `envs`, and
* its `configurations`. Each indexed file is a file the kustomization
* references, once, at the first line naming it -- and never an import: an
* import makes a file's objects part of the build. A file the index does not
* hold (one that is missing, and config/settings.json, which is on disk but
* a name discovery skips), a path out of the repository, an absolute path
* (it names a file of the machine, not of the repository, also when the
* repository holds a file at that path below its root or below the
* kustomization) and a generator's literals are none. */
TEST(probe_kustomize_generator_files_are_file_references) {
static const GpfFile files[] = {
{"deploy/kustomization.yaml", "apiVersion: kustomize.config.k8s.io/v1beta1\n"
"kind: Kustomization\n"
"resources:\n"
" - deployment.yaml\n"
"configMapGenerator:\n"
" - name: app-config\n"
" files:\n"
" - app.properties\n"
" - settings=config/settings.json\n"
" - limits=config/limits.json\n"
" - missing.properties\n"
" - abs=/abs.properties\n"
" envs:\n"
" - app.env\n"
" - name: literal-only\n"
" literals:\n"
" - A=1\n"
"secretGenerator:\n"
" - name: tokens\n"
" files:\n"
" - ../../outside.yaml\n"
" - ../shared/token.yaml\n"
"configurations:\n"
" - kustomizeconfig.yaml\n"
" - app.properties\n"
" - /abs.properties\n"},
{"deploy/deployment.yaml", "apiVersion: apps/v1\n"
"kind: Deployment\n"
"metadata:\n"
" name: app\n"
"spec:\n"
" replicas: 1\n"},
{"deploy/app.properties", "mode=prod\n"},
{"deploy/config/settings.json", "{\"mode\": \"prod\"}\n"},
{"deploy/config/limits.json", "{\"max\": 3}\n"},
{"deploy/app.env", "MODE=prod\n"},
{"deploy/kustomizeconfig.yaml", "nameReference: []\n"},
{"deploy/abs.properties", "mode=abs\n"},
{"abs.properties", "mode=root\n"},
{"shared/token.yaml", "token: placeholder\n"}};
int n = (int)(sizeof(files) / sizeof(files[0]));
char *got = gpf_kustomize_files(files, n);
static const char want[] =
"deploy/kustomization.yaml -> deploy/app.env (configMapGenerator:14)\n"
"deploy/kustomization.yaml -> deploy/app.properties (configMapGenerator:8)\n"
"deploy/kustomization.yaml -> deploy/config/limits.json (configMapGenerator:10)\n"
"deploy/kustomization.yaml -> deploy/kustomizeconfig.yaml (configurations:24)\n"
"deploy/kustomization.yaml -> shared/token.yaml (secretGenerator:22)\n"
"imports deploy/deployment.yaml";
if (!got || strcmp(got, want) != 0) {
fprintf(stderr, "kustomize files got:\n%s\nwant:\n%s\n", got ? got : "(null)", want);
free(got);
FAIL("kustomize file refs");
}
free(got);
PASS();
}
/* Every REFERENCES_FILE edge a local `uses:` draws, as
* "<file> -> <target file> (uses:line)", sorted, one per line. */
static char *gpf_local_uses(const GpfFile *files, int nfiles) {
@@ -7592,6 +7697,7 @@ SUITE(grammar_probe_f) {
RUN_TEST(probe_kustomize_module_node);
RUN_TEST(probe_kustomize_imports_edges);
RUN_TEST(probe_kustomize_with_patches);
RUN_TEST(probe_kustomize_generator_files_are_file_references);
RUN_TEST(probe_k8s_manifest_after_leading_comment);
RUN_TEST(probe_kustomize_imports_after_leading_comment);
RUN_TEST(probe_k8s_selector_after_leading_separator);
+31
View File
@@ -5645,6 +5645,36 @@ TEST(incremental_local_action_pair_deleted_takes_the_full_route) {
PASS();
}
/* A kustomization's generator file that appears after the kustomization was
* indexed. */
TEST(incremental_kustomize_file_reference_follows_its_file) {
int counts[4] = {-1, -1, -1, -1};
int rc = incremental_file_reference_steps("deploy/kustomization.yaml",
"apiVersion: kustomize.config.k8s.io/v1beta1\n"
"kind: Kustomization\n"
"resources:\n"
" - deployment.yaml\n"
"configMapGenerator:\n"
" - name: app-config\n"
" files:\n"
" - late.properties\n",
"deploy/deployment.yaml",
"apiVersion: apps/v1\n"
"kind: Deployment\n"
"metadata:\n"
" name: app\n"
"spec:\n"
" replicas: 1\n",
"deploy/late.properties", "configMapGenerator",
counts);
ASSERT_EQ(rc, 0);
ASSERT_EQ(counts[0], 0);
ASSERT_EQ(counts[1], 1);
ASSERT_EQ(counts[2], 1);
ASSERT_EQ(counts[3], 0);
PASS();
}
/* Deleting the only script requesting a URL leaves no Route behind -- a full
* index never makes one without an edge; the Route a kept script requests
* stays. `legacy`: through the full-load route, else closure repair. Returns
@@ -38453,6 +38483,7 @@ SUITE(pipeline_semantic_manifest_repro) {
RUN_TEST(incremental_terraform_file_reference_follows_its_file);
RUN_TEST(incremental_local_uses_reference_follows_its_file);
RUN_TEST(incremental_local_action_pair_deleted_takes_the_full_route);
RUN_TEST(incremental_kustomize_file_reference_follows_its_file);
RUN_TEST(incremental_script_request_to_compose_service_keeps_host);
RUN_TEST(incremental_deleted_dockerfile_drops_its_image);
RUN_TEST(incremental_dockerfile_edit_keeps_build_edges);