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.

feat(infra): the environment a container is given is its own EnvVar

Martin Vogel4ed6064c318d
diff --git a/internal/cbm/cbm.h b/internal/cbm/cbm.h
index e70d50500..b12b50874 100644
--- a/internal/cbm/cbm.h
+++ b/internal/cbm/cbm.h
@@ -616,14 +616,18 @@ typedef struct {
* from the URL `name`; a HelmRelease whose chartRef names it deploys the
* chart there.
* CBM_K8S_FACT_IMAGE: object def_index runs a container of image `name`
- * (a literal reference); via "container". */
+ * (a literal reference); via "container".
+ * CBM_K8S_FACT_ENV: object def_index sets environment variable `name` in
+ * its container `container` (NULL: unnamed) at `line`; via = where the
+ * value comes from ("value", or the valueFrom kind) -- never the value. */
typedef enum {
CBM_K8S_FACT_REF = 0,
CBM_K8S_FACT_CRD,
CBM_K8S_FACT_DEPLOY,
CBM_K8S_FACT_ROUTE,
CBM_K8S_FACT_SOURCE,
- CBM_K8S_FACT_IMAGE
+ CBM_K8S_FACT_IMAGE,
+ CBM_K8S_FACT_ENV
} CBMK8sFactKind;
typedef struct {
CBMK8sFactKind fact;
@@ -634,6 +638,8 @@ typedef struct {
const char *via;
const char *edge;
const char *route;
+ const char *container;
+ int line;
} CBMK8sFact;
typedef struct {
diff --git a/internal/cbm/extract_k8s.c b/internal/cbm/extract_k8s.c
index 3b7e25ecc..e6602c3f2 100644
--- a/internal/cbm/extract_k8s.c
+++ b/internal/cbm/extract_k8s.c
@@ -1045,8 +1045,85 @@ static void k8s_mapping_ref(k8s_walk_t *w, TSNode mapping, const char *kind, con
edge);
}
+// Where an env[] item's value comes from: "value", or its valueFrom kind
+// (configMapKeyRef, secretKeyRef, fieldRef, resourceFieldRef, fileKeyRef;
+// "valueFrom" for any other). The value itself is never read.
+static const char *k8s_env_source(k8s_walk_t *w, TSNode item) {
+ static const char *const KINDS[] = {"configMapKeyRef", "secretKeyRef", "fieldRef",
+ "resourceFieldRef", "fileKeyRef"};
+ k8s_kids_t pairs = k8s_kids(item);
+ TSNode pair;
+ while (k8s_kid(&pairs, true, &pair)) {
+ if (!k8s_is_pair(pair)) {
+ continue;
+ }
+ TSNode key = ts_node_named_child(pair, 0);
+ const char *k =
+ ts_node_is_null(key) ? NULL : get_scalar_text(w->ctx->arena, key, w->ctx->source);
+ if (!k || strcmp(k, "valueFrom") != 0) {
+ continue;
+ }
+ k8s_kids_done(&pairs);
+ TSNode from = k8s_unwrap(unwrap_pair_value(pair));
+ k8s_kids_t kinds = k8s_kids(from);
+ TSNode kp;
+ const char *kind = NULL;
+ while (k8s_kid(&kinds, true, &kp)) {
+ TSNode kk = k8s_is_pair(kp) ? ts_node_named_child(kp, 0) : (TSNode){0};
+ const char *s =
+ ts_node_is_null(kk) ? NULL : get_scalar_text(w->ctx->arena, kk, w->ctx->source);
+ if (s && !kind) {
+ kind = s;
+ }
+ }
+ for (size_t i = 0; kind && i < sizeof(KINDS) / sizeof(KINDS[0]); i++) {
+ if (strcmp(kind, KINDS[i]) == 0) {
+ return KINDS[i];
+ }
+ }
+ return "valueFrom";
+ }
+ return "value";
+}
+
+// The environment variables a container (`path` ends in containers[],
+// initContainers[] or ephemeralContainers[], any depth) sets: each env[]
+// name, with where its value comes from -- never the value.
+static void k8s_container_env(k8s_walk_t *w, const char *path, TSNode container) {
+ static const char *const ITEMS[] = {"containers[]", "initContainers[]",
+ "ephemeralContainers[]"};
+ size_t pl = strlen(path);
+ bool at = false;
+ for (size_t i = 0; i < sizeof(ITEMS) / sizeof(ITEMS[0]) && !at; i++) {
+ size_t il = strlen(ITEMS[i]);
+ at = pl >= il && strcmp(path + pl - il, ITEMS[i]) == 0 &&
+ (pl == il || path[pl - il - SKIP_ONE] == '.');
+ }
+ if (!at) {
+ return;
+ }
+ const char *cname = k8s_text(w, container, "name");
+ cname = cname && cname[0] && !strstr(cname, "{{") ? cname : NULL;
+ k8s_iter_t env = k8s_iter(k8s_child(w, container, "env"));
+ TSNode item;
+ while (k8s_next(&env, &item)) {
+ const char *key = k8s_is_mapping(item) ? k8s_text(w, item, "name") : NULL;
+ if (!key || !key[0] || strstr(key, "{{")) {
+ continue;
+ }
+ CBMK8sFact f = {.fact = CBM_K8S_FACT_ENV,
+ .def_index = w->def_index,
+ .name = key,
+ .container = cname,
+ .via = k8s_env_source(w, item),
+ .line = (int)ts_node_start_point(item).row + SKIP_ONE};
+ cbm_k8sfact_push(&w->ctx->result->k8s_facts, w->ctx->arena, f);
+ }
+}
+
// A mapping at `path` that holds a reference in its kind/name/namespace.
static void k8s_mapping(k8s_walk_t *w, const char *path, TSNode mapping) {
+ k8s_container_env(w, path, mapping);
for (size_t i = 0; i < sizeof(k8s_mapping_refs) / sizeof(k8s_mapping_refs[0]); i++) {
const k8s_mapping_ref_t *r = &k8s_mapping_refs[i];
if (k8s_kind_in(r->kinds, w->kind) && strcmp(path, r->path) == 0) {
diff --git a/src/pipeline/pass_ci.c b/src/pipeline/pass_ci.c
index c7083e0ac..c8b9a4467 100644
--- a/src/pipeline/pass_ci.c
+++ b/src/pipeline/pass_ci.c
@@ -74,6 +74,7 @@ typedef struct {
typedef struct {
const char *key;
TSNode value; /* may be null: `key:` without a value */
+ int row; /* the key's 0-based row */
} ci_pair_t;
static bool ci_type(TSNode n, const char *t) {
@@ -264,9 +265,10 @@ static ci_pair_t *ci_pairs(const ci_t *c, TSNode m, int *n) {
if (!ci_type(p, "block_mapping_pair") && !ci_type(p, "flow_pair")) {
continue;
}
- const char *key = ci_scalar(c, ts_node_child_by_field_name(p, TS_FIELD("key")), NULL);
+ int row = 0;
+ const char *key = ci_scalar(c, ts_node_child_by_field_name(p, TS_FIELD("key")), &row);
if (key) {
- out[(*n)++] = (ci_pair_t){key, ts_node_child_by_field_name(p, TS_FIELD("value"))};
+ out[(*n)++] = (ci_pair_t){key, ts_node_child_by_field_name(p, TS_FIELD("value")), row};
}
}
return out;
@@ -986,6 +988,38 @@ static int ci_compose_deps(const ci_t *c, TSNode svc, const char ***out) {
return n;
}
+/* The variable names a service's environment sets: a mapping's keys, a
+ * list's "NAME=value" / "NAME" items (the name only -- a value is never
+ * kept), with their 1-based lines. */
+static int ci_compose_env(const ci_t *c, TSNode svc, const char ***out, const int **lines) {
+ int n = 0;
+ TSNode env = ci_get(c, svc, "environment");
+ TSNode *items = ci_items(c, env, &n);
+ ci_pair_t *pairs = items ? NULL : ci_pairs(c, env, &n);
+ const char **names = cbm_arena_alloc(c->a, (size_t)(n + SKIP_ONE) * sizeof(*names));
+ int *rows = cbm_arena_alloc(c->a, (size_t)(n + SKIP_ONE) * sizeof(*rows));
+ int k = 0;
+ for (int i = 0; names && rows && i < n; i++) {
+ int row = 0;
+ const char *name = NULL;
+ if (items) {
+ const char *s = ci_scalar(c, items[i], &row);
+ const char *eq = s ? strchr(s, '=') : NULL;
+ name = eq ? cbm_arena_strndup(c->a, s, (size_t)(eq - s)) : s;
+ } else {
+ name = pairs[i].key;
+ row = pairs[i].row;
+ }
+ if (name && name[0]) {
+ names[k] = name;
+ rows[k++] = row + SKIP_ONE;
+ }
+ }
+ *out = names;
+ *lines = rows;
+ return k;
+}
+
static void ci_compose(const ci_t *c, TSNode root, const char *dir, const ci_env_t *env) {
int ns = 0;
ci_pair_t *svcs = ci_pairs(c, ci_get(c, root, "services"), &ns);
@@ -1010,6 +1044,7 @@ static void ci_compose(const ci_t *c, TSNode root, const char *dir, const ci_env
.line = (int)ts_node_start_point(svc).row,
.end_line = (int)ts_node_end_point(svc).row + SKIP_ONE};
s.ndeps = ci_compose_deps(c, svc, &s.deps);
+ s.nenv = ci_compose_env(c, svc, &s.env, &s.env_lines);
c->service_fn(&s, c->ud);
}
}
diff --git a/src/pipeline/pass_docker.c b/src/pipeline/pass_docker.c
index 26bdf15d7..85aa08d3c 100644
--- a/src/pipeline/pass_docker.c
+++ b/src/pipeline/pass_docker.c
@@ -28,6 +28,15 @@
* the file (or Go main package) wanted, else nothing; COPY --from=<stage>
* continues in that stage. WORKDIR makes paths absolute. `sh -c`, a variable
* program and arguments to an inherited ENTRYPOINT are no edge.
+ *
+ * PHASE 4 P4.5: the environment a container is given. Each variable a K8s
+ * container's env, a compose service's environment or the last stage's ENV
+ * (with the stages it is built FROM) sets is an EnvVar of that container
+ * (image, for ENV) -- `<owner QN>#env/<container | ->/<name>`, owned by the
+ * declaring file -- the owner CONFIGURES {strategy:env_set, source}. The
+ * global EnvVar a program's readers share is not joined: the same name is
+ * read by other programs (measured: 23 % of such joins in scope). Values are
+ * never read into the graph.
* Run at the end of the K8s pass.
*/
#include "foundation/constants.h"
@@ -77,9 +86,17 @@ typedef struct {
bool set;
} dk_entry_t;
+typedef struct dk_envkey {
+ const char *name;
+ int line;
+ struct dk_envkey *next; /* the stage's previous ENV key */
+} dk_envkey_t;
+
typedef struct {
dk_copy_t *copies; /* newest first */
dk_build_t *builds; /* newest first */
+ dk_envkey_t *envs; /* newest first */
+ int parent; /* the stage FROM builds on; -1: an image */
} dk_stage_t;
typedef struct {
@@ -321,6 +338,28 @@ static bool dk_is_stage(const dk_t *d, const char *ref) {
return false;
}
+/* The stage a --from names: an index, else the last stage of that name. */
+static int dk_stage_of(const dk_t *d, const char *ref) {
+ if (!ref || !ref[0]) {
+ return -1;
+ }
+ bool digits = true;
+ for (const char *p = ref; *p; p++) {
+ digits = digits && isdigit((unsigned char)*p);
+ }
+ if (digits) {
+ int k = atoi(ref);
+ return k < DK_MAX_STAGES ? k : -1;
+ }
+ int found = -1;
+ for (int i = 0; i <= d->stage && i < DK_MAX_STAGES; i++) {
+ if (d->stages[i] && dk_ieq(d->stages[i], ref)) {
+ found = i;
+ }
+ }
+ return found;
+}
+
int64_t cbm_pipeline_image_node(cbm_pipeline_ctx_t *ctx, const char *ref) {
char repo[CBM_SZ_512];
if (!ref || dk_ieq(ref, "scratch") || !dk_normalize(ref, repo, sizeof(repo))) {
@@ -335,6 +374,55 @@ int64_t cbm_pipeline_image_node(cbm_pipeline_ctx_t *ctx, const char *ref) {
return cbm_gbuf_upsert_node(ctx->gbuf, "Image", repo, qn, "", 0, 0, node_props);
}
+/* An environment variable name: [A-Za-z_][A-Za-z0-9_.-]*. */
+static bool dk_env_key(const char *k) {
+ if (!k || !(isalpha((unsigned char)k[0]) || k[0] == '_')) {
+ return false;
+ }
+ for (const char *p = k + SKIP_ONE; *p; p++) {
+ if (!isalnum((unsigned char)*p) && *p != '_' && *p != '.' && *p != '-') {
+ return false;
+ }
+ }
+ return strlen(k) < CBM_SZ_256;
+}
+
+int64_t cbm_pipeline_env_setter(cbm_pipeline_ctx_t *ctx, const cbm_gbuf_node_t *owner,
+ const char *container, const char *key, const char *source,
+ int line) {
+ if (!owner || !owner->qualified_name || !dk_env_key(key) || !source) {
+ return 0;
+ }
+ char qn[CBM_SZ_2K];
+ int n = snprintf(qn, sizeof(qn), "%s#env/%s/%s", owner->qualified_name,
+ container ? container : "-", key);
+ if (n <= 0 || (size_t)n >= sizeof(qn)) {
+ return 0;
+ }
+ char esc_c[CBM_SZ_512];
+ cbm_json_escape(esc_c, (int)sizeof(esc_c), container ? container : "");
+ char scope[CBM_SZ_1K];
+ if (container) {
+ snprintf(scope, sizeof(scope), "\"scope\":\"container\",\"container\":\"%s\"", esc_c);
+ } else {
+ snprintf(scope, sizeof(scope), "\"scope\":\"image\"");
+ }
+ char node_props[CBM_SZ_2K];
+ snprintf(node_props, sizeof(node_props), "{\"env_key\":\"%s\",%s,\"source\":\"%s\"}", key,
+ scope, source);
+ int64_t id =
+ cbm_gbuf_upsert_node(ctx->gbuf, "EnvVar", key, qn, owner->file_path ? owner->file_path : "",
+ line, line, node_props);
+ if (id <= 0 || id == owner->id) {
+ return 0;
+ }
+ char props[CBM_SZ_2K];
+ snprintf(props, sizeof(props), "{\"strategy\":\"env_set\",%s,\"source\":\"%s\",\"line\":%d}",
+ scope, source, line);
+ cbm_gbuf_insert_edge(ctx->gbuf, owner->id, id, "CONFIGURES", props);
+ return id;
+}
+
/* DEPENDS_ON from the Dockerfile to the Image of resolved reference `ref`. */
static void dk_link(dk_t *d, const char *ref, const char *via, int line) {
int64_t img = cbm_pipeline_image_node(d->ctx, ref);
@@ -394,7 +482,13 @@ static void dk_env(dk_t *d, TSNode ins) {
continue;
}
const char *raw = ts_node_is_null(vl) ? NULL : dk_value(d, vl);
- dk_set(d, &d->local, dk_text(d, nm), raw ? dk_expand(d, d->local, raw) : NULL);
+ const char *name = dk_text(d, nm);
+ dk_set(d, &d->local, name, raw ? dk_expand(d, d->local, raw) : NULL);
+ dk_envkey_t *k = d->stage < DK_MAX_STAGES ? cbm_arena_alloc(d->a, sizeof(*k)) : NULL;
+ if (k && name) {
+ *k = (dk_envkey_t){name, dk_line(pair), d->st[d->stage].envs};
+ d->st[d->stage].envs = k;
+ }
}
}
@@ -412,10 +506,12 @@ static void dk_from(dk_t *d, TSNode ins) {
d->workdir = "/";
d->entry.set = false;
d->cmd.set = false;
- if (ref && !dk_is_stage(d, ref)) {
+ bool stage = ref && dk_is_stage(d, ref);
+ if (ref && !stage) {
dk_link(d, ref, "from", dk_line(ins));
}
if (d->stage < DK_MAX_STAGES) {
+ d->st[d->stage].parent = stage ? dk_stage_of(d, ref) : -1;
d->stages[d->stage] = ts_node_is_null(alias) ? NULL : dk_text(d, alias);
}
}
@@ -652,28 +748,6 @@ static const char *dk_repo(dk_t *d, const char *src, const char *rel, int want)
return hits == SKIP_ONE ? hit : NULL;
}
-/* The stage a --from names: an index, else the last stage of that name. */
-static int dk_stage_of(const dk_t *d, const char *ref) {
- if (!ref || !ref[0]) {
- return -1;
- }
- bool digits = true;
- for (const char *p = ref; *p; p++) {
- digits = digits && isdigit((unsigned char)*p);
- }
- if (digits) {
- int k = atoi(ref);
- return k < DK_MAX_STAGES ? k : -1;
- }
- int found = -1;
- for (int i = 0; i <= d->stage && i < DK_MAX_STAGES; i++) {
- if (d->stages[i] && dk_ieq(d->stages[i], ref)) {
- found = i;
- }
- }
- return found;
-}
-
typedef struct {
const char *repo; /* a repository path, or */
const dk_build_t *build; /* the go build that made the path */
@@ -1047,6 +1121,32 @@ static void dk_spawn(dk_t *d) {
d->edges++;
}
+/* The environment the image of the last stage carries: the ENV keys of that
+ * stage and of the earlier stages it is built FROM (a key's nearest
+ * definition wins) -- image-scoped EnvVars the Dockerfile sets. */
+static void dk_envs(dk_t *d) {
+ const cbm_gbuf_node_t *mod = cbm_gbuf_find_by_id(d->ctx->gbuf, d->module);
+ if (!mod || d->stage < 0 || d->stage >= DK_MAX_STAGES) {
+ return;
+ }
+ CBMHashTable *seen = cbm_ht_create(0);
+ int s = d->stage;
+ for (int depth = 0; seen && s >= 0 && depth < DK_MAX_STAGES; depth++) {
+ for (const dk_envkey_t *k = d->st[s].envs; k; k = k->next) {
+ if (!cbm_ht_get(seen, k->name)) {
+ cbm_ht_set(seen, k->name, (void *)k);
+ if (cbm_pipeline_env_setter(d->ctx, mod, NULL, k->name, "dockerfile", k->line) >
+ 0) {
+ d->edges++;
+ }
+ }
+ }
+ int p = d->st[s].parent;
+ s = p >= 0 && p < s ? p : -1;
+ }
+ cbm_ht_free(seen);
+}
+
static void dk_file(dk_t *d, TSNode root) {
for (uint32_t i = 0; i < ts_node_named_child_count(root); i++) {
TSNode ins = ts_node_named_child(root, i);
@@ -1070,6 +1170,7 @@ static void dk_file(dk_t *d, TSNode root) {
}
}
dk_spawn(d);
+ dk_envs(d);
}
static char *dk_read(const char *path, size_t *len) {
@@ -1097,6 +1198,66 @@ static char *dk_read(const char *path, size_t *len) {
return buf;
}
+/* Does the line [b, e) hold a parser directive `# escape=...`? */
+static bool dk_escape_directive(const char *s, size_t b, size_t e) {
+ size_t i = b + SKIP_ONE;
+ while (i < e && (s[i] == ' ' || s[i] == '\t')) {
+ i++;
+ }
+ static const char kw[] = "escape";
+ for (size_t k = 0; kw[k]; k++, i++) {
+ if (i >= e || tolower((unsigned char)s[i]) != kw[k]) {
+ return false;
+ }
+ }
+ while (i < e && (s[i] == ' ' || s[i] == '\t')) {
+ i++;
+ }
+ return i < e && s[i] == '=';
+}
+
+/* Docker drops comment lines before it joins continued lines; the grammar
+ * ends an instruction at one (kubermatic: `ENV A=1 \` / `# url` / `B=2`).
+ * A comment line inside a continuation becomes a bare continuation (`#` ->
+ * `\`, the rest blank) -- rows and offsets stay. A file with an escape
+ * directive is left as it is. */
+static void dk_uncomment(char *s, size_t len) {
+ bool cont = false;
+ bool top = true; /* still in the leading directives */
+ for (size_t i = 0; i < len;) {
+ size_t e = i;
+ while (e < len && s[e] != '\n') {
+ e++;
+ }
+ size_t b = i;
+ while (b < e && (s[b] == ' ' || s[b] == '\t')) {
+ b++;
+ }
+ if (b < e && s[b] == '#') {
+ if (top && dk_escape_directive(s, b, e)) {
+ return;
+ }
+ if (cont) {
+ s[b] = '\\';
+ for (size_t k = b + SKIP_ONE; k < e && s[k] != '\r'; k++) {
+ s[k] = ' ';
+ }
+ }
+ } else if (b < e && s[b] != '\r') {
+ size_t t = e;
+ while (t > b &&
+ (s[t - SKIP_ONE] == ' ' || s[t - SKIP_ONE] == '\t' || s[t - SKIP_ONE] == '\r')) {
+ t--;
+ }
+ cont = s[t - SKIP_ONE] == '\\';
+ top = false;
+ } else {
+ top = false;
+ }
+ i = e + SKIP_ONE;
+ }
+}
+
typedef struct {
cbm_pipeline_ctx_t *ctx;
CBMArena *a;
@@ -1170,6 +1331,13 @@ static void dk_compose_service(const cbm_compose_service_t *s, void *ud) {
cbm_arena_sprintf(c->a, "Service/%s", s->name), qn,
c->file, s->line + SKIP_ONE, s->end_line, props);
dk_compose_edge(c, c->file_node, id, "DEFINES", "{}");
+ const cbm_gbuf_node_t *self = id > 0 ? cbm_gbuf_find_by_id(c->ctx->gbuf, id) : NULL;
+ for (int i = 0; self && i < s->nenv; i++) {
+ if (cbm_pipeline_env_setter(c->ctx, self, s->name, s->env[i], "compose",
+ s->env_lines[i]) > 0) {
+ c->edges++;
+ }
+ }
return;
}
const cbm_gbuf_node_t *self = cbm_gbuf_find_by_qn(c->ctx->gbuf, qn);
@@ -1321,6 +1489,7 @@ void cbm_pipeline_pass_docker(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *fi
if (!src) {
continue;
}
+ dk_uncomment(src, len);
if (!parser) {
parser = ts_parser_new();
}
diff --git a/src/pipeline/pass_k8s.c b/src/pipeline/pass_k8s.c
index 41a4e62bc..2ba210f49 100644
--- a/src/pipeline/pass_k8s.c
+++ b/src/pipeline/pass_k8s.c
@@ -1143,6 +1143,15 @@ typedef struct {
char *source; /* the sourceRef kind, NULL when unset */
} k8s_hr_t;
+/* An environment variable an object's container sets (never its value). */
+typedef struct {
+ int64_t node;
+ char *key;
+ char *container; /* NULL: unnamed */
+ const char *source; /* static: "value" or the valueFrom kind */
+ int line;
+} k8s_env_t;
+
typedef struct {
k8s_ref_t *refs;
int ref_n;
@@ -1162,6 +1171,9 @@ typedef struct {
k8s_hr_t *imgs; /* an object (node) and a container image it runs (chart) */
int img_n;
int img_cap;
+ k8s_env_t *envs;
+ int env_n;
+ int env_cap;
} k8s_facts_t;
static void k8s_facts_free(k8s_facts_t *f) {
@@ -1189,6 +1201,11 @@ static void k8s_facts_free(k8s_facts_t *f) {
for (int i = 0; i < f->img_n; i++) {
cbm_free(CBM_MEM_CLASS_OTHER, f->imgs[i].chart);
}
+ for (int i = 0; i < f->env_n; i++) {
+ cbm_free(CBM_MEM_CLASS_OTHER, f->envs[i].key);
+ cbm_free(CBM_MEM_CLASS_OTHER, f->envs[i].container);
+ }
+ cbm_free(CBM_MEM_CLASS_OTHER, f->envs);
cbm_free(CBM_MEM_CLASS_OTHER, f->hrs);
cbm_free(CBM_MEM_CLASS_OTHER, f->srcs);
cbm_free(CBM_MEM_CLASS_OTHER, f->imgs);
@@ -1274,6 +1291,13 @@ static void k8s_facts_keep(k8s_facts_t *f, const CBMFileResult *res, const int64
f->imgs[f->img_n++] =
(k8s_hr_t){node, cbm_mem_strdup(CBM_MEM_CLASS_OTHER, x->name), NULL};
}
+ } else if (x->fact == CBM_K8S_FACT_ENV) {
+ if (x->name && k8s_grow((void **)&f->envs, &f->env_cap, f->env_n, sizeof(*f->envs))) {
+ f->envs[f->env_n++] = (k8s_env_t){
+ node, cbm_mem_strdup(CBM_MEM_CLASS_OTHER, x->name),
+ x->container ? cbm_mem_strdup(CBM_MEM_CLASS_OTHER, x->container) : NULL, x->via,
+ x->line};
+ }
} else if (x->fact == CBM_K8S_FACT_CRD) {
if (k8s_grow((void **)&f->crds, &f->crd_cap, f->crd_n, sizeof(*f->crds))) {
f->crds[f->crd_n++] =
@@ -3300,6 +3324,19 @@ static void k8s_link_images(cbm_pipeline_ctx_t *ctx, const k8s_facts_t *f) {
}
}
+/* The environment variables an object's containers set: container-scoped
+ * EnvVar nodes the object CONFIGURES {strategy:env_set}. */
+static void k8s_link_envs(cbm_pipeline_ctx_t *ctx, const k8s_facts_t *f) {
+ for (int i = 0; i < f->env_n; i++) {
+ const k8s_env_t *e = &f->envs[i];
+ const cbm_gbuf_node_t *owner = cbm_gbuf_find_by_id(ctx->gbuf, e->node);
+ if (owner) {
+ cbm_pipeline_env_setter(ctx, owner, e->container ? e->container : "-", e->key,
+ e->source, e->line);
+ }
+ }
+}
+
static void k8s_link_deploys(cbm_pipeline_ctx_t *ctx, k8s_kusts_t *ks, k8s_bundles_t *b,
const k8s_facts_t *f) {
if (f->dep_n == 0 && f->hr_n == 0 && f->src_n == 0) {
@@ -3489,6 +3526,7 @@ int cbm_pipeline_pass_k8s(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files,
k8s_link_crds(ctx, &bundles, &facts);
k8s_link_deploys(ctx, &kusts, &bundles, &facts);
k8s_link_images(ctx, &facts);
+ k8s_link_envs(ctx, &facts);
k8s_facts_free(&facts);
k8s_records_free(&recs);
/* Link `file://` chart dependencies now that every Chart.yaml is read,
diff --git a/src/pipeline/pipeline_internal.h b/src/pipeline/pipeline_internal.h
index 0284d2185..140ce7587 100644
--- a/src/pipeline/pipeline_internal.h
+++ b/src/pipeline/pipeline_internal.h
@@ -1090,14 +1090,19 @@ typedef void (*cbm_build_site_fn)(const cbm_build_site_t *site, void *ud);
/* A compose service (pass_ci.c): its image (a reference, variables
* expanded; NULL: none or unknown), the Dockerfile its build names
- * (repository path; NULL: no build, or unknown), and the service names its
- * depends_on and links list. */
+ * (repository path; NULL: no build, or unknown), the service names its
+ * depends_on and links list, and the names of the environment variables its
+ * environment sets (map keys / list items' names -- never a value), with
+ * their 1-based lines. */
typedef struct {
const char *name;
const char *image;
const char *dockerfile;
const char **deps;
int ndeps;
+ const char **env;
+ const int *env_lines;
+ int nenv;
int line;
int end_line;
} cbm_compose_service_t;
@@ -1129,6 +1134,19 @@ void cbm_pipeline_pass_terraform(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t
* digest dropped), made when new; 0 for scratch or no image. */
int64_t cbm_pipeline_image_node(cbm_pipeline_ctx_t *ctx, const char *ref);
+/* Environment variable `key` that `owner` sets (pass_docker.c): a K8s or
+ * compose Resource for its container `container`, a Dockerfile Module for
+ * its image (container NULL). Makes the container-scoped EnvVar
+ * `<owner QN>#env/<container | ->/<key>` {env_key, scope, container,
+ * source}, owned by the owner's file at `line`, and owner -CONFIGURES
+ * {strategy:env_set, source}-> it. No edge to the global EnvVar of the key:
+ * the same name read elsewhere is another program's. `source` says where the
+ * value comes from; the value is never stored. 0 when `key` is no variable
+ * name ([A-Za-z_][A-Za-z0-9_.-]*). */
+int64_t cbm_pipeline_env_setter(cbm_pipeline_ctx_t *ctx, const cbm_gbuf_node_t *owner,
+ const char *container, const char *key, const char *source,
+ int line);
+
/* Container builds (pass_docker.c): a Dockerfile DEPENDS_ON the Images it
* builds on (FROM, COPY --from). Run at the end of the K8s pass. */
void cbm_pipeline_pass_docker(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files,
diff --git a/tests/test_grammar_probe_f.c b/tests/test_grammar_probe_f.c
index 2ff52501f..c32b752d8 100644
--- a/tests/test_grammar_probe_f.c
+++ b/tests/test_grammar_probe_f.c
@@ -2205,6 +2205,103 @@ TEST(probe_shell_docker_builds_build_images) {
PASS();
}
+/* Every CONFIGURES {strategy:env_set} edge as
+ * "<owner> -> <container | ->/<name> (<source>)"; then the joins a scoped
+ * EnvVar must not have: "#PORT=<EnvVar nodes named PORT>" and
+ * "#joined=<edges out of an EnvVar, or env_set edges into a global one>". */
+static char *gpf_env_setters(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 CASE s.label WHEN "
+ "'Module' THEN s.file_path ELSE s.name END || ' -> ' || "
+ "coalesce(json_extract(t.properties,'$.container'),'-') || '/' || t.name || ' (' || "
+ "json_extract(e.properties,'$.source') || ')' 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='CONFIGURES' AND json_extract(e.properties,'$.strategy')='env_set' ORDER BY "
+ "x)), '') || char(10) || '#PORT=' || (SELECT count(*) FROM nodes WHERE project=?1 "
+ "AND label='EnvVar' AND name='PORT') || char(10) || '#joined=' || (SELECT count(*) "
+ "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 (s.label='EnvVar' OR (substr(t.qualified_name,1,7)='__env__' "
+ "AND json_extract(e.properties,'$.strategy')='env_set')))";
+ 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) {
+ out = strdup((const char *)sqlite3_column_text(st, 0));
+ }
+ }
+ sqlite3_finalize(st);
+ }
+ gpf_cleanup(&lp, store);
+ return out;
+}
+
+/* The environment a container is given: each name a K8s container's env
+ * (any pod template; value or valueFrom), a compose service's environment
+ * (map or list) or the last Dockerfile stage's ENV (with the stages it is
+ * built FROM, a comment inside the continuation skipped) sets is an EnvVar
+ * of that container -- two containers' PORT are two nodes, neither joined to
+ * the PORT a program reads. No value reaches the graph. */
+TEST(probe_container_env_sets_scoped_envvars) {
+ static const GpfFile files[] = {
+ {"app.yaml",
+ "apiVersion: apps/v1\nkind: Deployment\nmetadata:\n name: web\nspec:\n template:\n"
+ " spec:\n initContainers:\n - name: init\n image: busybox\n"
+ " env:\n - name: MODE\n value: v-init-mode\n"
+ " containers:\n - name: web\n image: nginx\n env:\n"
+ " - name: PORT\n value: \"v-8080\"\n"
+ " - name: DB_PASS\n valueFrom:\n secretKeyRef:\n"
+ " name: db\n key: pass\n"
+ " - name: POD_IP\n valueFrom:\n fieldRef:\n"
+ " fieldPath: status.podIP\n"
+ "---\napiVersion: apps/v1\nkind: Deployment\nmetadata:\n name: api\nspec:\n"
+ " template:\n spec:\n containers:\n - name: api\n"
+ " image: acme/api\n env:\n - name: PORT\n"
+ " value: \"v-9090\"\n"},
+ {"compose.yaml", "services:\n web:\n image: acme/web\n environment:\n"
+ " LOG_LEVEL: v-debug\n API_KEY: v-s3cr3t\n"
+ " worker:\n image: acme/worker\n environment:\n"
+ " - QUEUE=v-jobs\n - VERBOSE\n"},
+ {"Dockerfile", "FROM golang:1.22 AS build\nENV CGO_ENABLED=0\n"
+ "FROM alpine:3.20 AS base\nENV TZ=v-utc\n"
+ "FROM base\nENV APP_HOME=/v-app \\\n # where it lives\n"
+ " GREETING=\"v-hello world\"\n"},
+ {"main.go", "package main\n\nimport \"os\"\n\nfunc main() { _ = os.Getenv(\"PORT\") }\n"}};
+ char *got = gpf_env_setters(files, 4);
+ static const char want[] = "Deployment/api -> api/PORT (value)\n"
+ "Deployment/web -> init/MODE (value)\n"
+ "Deployment/web -> web/DB_PASS (secretKeyRef)\n"
+ "Deployment/web -> web/POD_IP (fieldRef)\n"
+ "Deployment/web -> web/PORT (value)\n"
+ "Dockerfile -> -/APP_HOME (dockerfile)\n"
+ "Dockerfile -> -/GREETING (dockerfile)\n"
+ "Dockerfile -> -/TZ (dockerfile)\n"
+ "Service/web -> web/API_KEY (compose)\n"
+ "Service/web -> web/LOG_LEVEL (compose)\n"
+ "Service/worker -> worker/QUEUE (compose)\n"
+ "Service/worker -> worker/VERBOSE (compose)\n"
+ "#PORT=3\n#joined=0";
+ if (!got || strcmp(got, want) != 0) {
+ fprintf(stderr, "env setters got:\n%s\nwant:\n%s\n", got ? got : "(null)", want);
+ free(got);
+ FAIL("env_set edge set");
+ }
+ free(got);
+ static const char *const values[] = {"v-init-mode", "v-8080", "v-9090", "v-debug",
+ "v-s3cr3t", "v-jobs", "v-utc", "v-hello"};
+ for (size_t i = 0; i < sizeof(values) / sizeof(values[0]); i++) {
+ int hits = gpf_graph_mentions(files, 4, values[i]);
+ if (hits != 0) {
+ fprintf(stderr, "value %s: %d graph mentions\n", values[i], hits);
+ FAIL("an env value reached the graph");
+ }
+ }
+ PASS();
+}
+
#define GPF_CM(name) "apiVersion: v1\nkind: ConfigMap\nmetadata:\n name: " name "\n"
#define GPF_KUST "apiVersion: kustomize.config.k8s.io/v1beta1\nkind: Kustomization\n"
@@ -4475,6 +4572,7 @@ SUITE(grammar_probe_f) {
RUN_TEST(probe_build_configs_build_images_from_dockerfiles);
RUN_TEST(probe_compose_services_are_resources);
RUN_TEST(probe_shell_docker_builds_build_images);
+ RUN_TEST(probe_container_env_sets_scoped_envvars);
RUN_TEST(probe_gitops_objects_deploy_their_units);
RUN_TEST(probe_shell_kubectl_deploys_what_it_applies);
RUN_TEST(probe_shell_helm_deploys_its_chart);
diff --git a/tests/test_pipeline.c b/tests/test_pipeline.c
index 16fe909a6..85eaa8762 100644
--- a/tests/test_pipeline.c
+++ b/tests/test_pipeline.c
@@ -3665,6 +3665,87 @@ TEST(incremental_compose_edit_replaces_build_edges) {
PASS();
}
+/* Renaming a variable in a compose service's environment (and, with
+ * `dockerfile`, in a Dockerfile's ENV) replaces its container-scoped EnvVar;
+ * the unchanged compose file and Dockerfile keep theirs, once. `legacy`:
+ * through the full-load route, else the closure-repair route (a renamed ENV
+ * is a new Variable name, which the closure route leaves to a full
+ * rebuild). Returns "old=N new=M keep=K", or NULL. */
+static char *incremental_env_edit(bool legacy, bool dockerfile, cbm_incremental_route_t *route) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_envedit_XXXXXX");
+ if (!cbm_mkdtemp(tmp)) {
+ return NULL;
+ }
+ write_temp_file(tmp, "Dockerfile",
+ dockerfile ? "FROM alpine:3.20\nENV OLD_D=1\n" : "FROM alpine:3.20\nENV KEEP_D=1\n");
+ write_temp_file(tmp, "compose.yaml",
+ "services:\n web:\n image: acme/web\n environment:\n OLD_C: x\n");
+ write_temp_file(tmp, "compose.other.yaml",
+ "services:\n api:\n image: acme/api\n environment:\n - KEEP_C\n");
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/envedit.db", tmp);
+ static const char old_q[] =
+ "SELECT COUNT(*) FROM nodes WHERE label='EnvVar' AND name IN ('OLD_C','OLD_D')";
+ static const char new_q[] =
+ "SELECT COUNT(*) FROM edges e JOIN nodes t ON t.id=e.target_id WHERE "
+ "e.type='CONFIGURES' AND json_extract(e.properties,'$.strategy')='env_set' AND "
+ "t.name IN ('NEW_C','NEW_D')";
+ static const char keep_q[] =
+ "SELECT COUNT(*) FROM edges e JOIN nodes t ON t.id=e.target_id WHERE "
+ "e.type='CONFIGURES' AND json_extract(e.properties,'$.strategy')='env_set' AND "
+ "t.name IN ('KEEP_C','KEEP_D')";
+ if (k8s_inc_index(tmp, db_path) != 0 || k8s_inc_count(db_path, old_q) != (dockerfile ? 2 : 1) ||
+ k8s_inc_count(db_path, keep_q) != (dockerfile ? 1 : 2)) {
+ th_rmtree(tmp);
+ return NULL;
+ }
+ if (dockerfile) {
+ write_temp_file(tmp, "Dockerfile", "FROM alpine:3.20\nENV NEW_D=1\n");
+ }
+ write_temp_file(tmp, "compose.yaml",
+ "services:\n web:\n image: acme/web\n environment:\n NEW_C: x\n");
+#if defined(CBM_INCREMENTAL_TEST_API) && CBM_INCREMENTAL_TEST_API
+ if (legacy) {
+ cbm_pipeline_incremental_test_force_legacy_partial_once();
+ }
+#else
+ (void)legacy;
+#endif
+ char *out = NULL;
+ if (k8s_inc_index(tmp, db_path) == 0) {
+ char buf[64];
+ snprintf(buf, sizeof(buf), "old=%d new=%d keep=%d", k8s_inc_count(db_path, old_q),
+ k8s_inc_count(db_path, new_q), k8s_inc_count(db_path, keep_q));
+ out = strdup(buf);
+ }
+ *route = cbm_pipeline_incremental_test_last_route();
+ th_rmtree(tmp);
+ return out;
+}
+
+TEST(incremental_env_edit_replaces_scoped_envvars) {
+ cbm_incremental_route_t route = CBM_INCREMENTAL_ROUTE_NONE;
+ char *got = incremental_env_edit(false, false, &route);
+ ASSERT_EQ(route, CBM_INCREMENTAL_ROUTE_CLOSURE_REPAIR);
+ ASSERT_NOT_NULL(got);
+ ASSERT_STR_EQ(got, "old=0 new=1 keep=2");
+ free(got);
+#if defined(CBM_INCREMENTAL_TEST_API) && CBM_INCREMENTAL_TEST_API
+ got = incremental_env_edit(true, false, &route);
+ ASSERT_EQ(route, CBM_INCREMENTAL_ROUTE_LEGACY_PARTIAL);
+ ASSERT_NOT_NULL(got);
+ ASSERT_STR_EQ(got, "old=0 new=1 keep=2");
+ free(got);
+ got = incremental_env_edit(true, true, &route);
+ ASSERT_EQ(route, CBM_INCREMENTAL_ROUTE_LEGACY_PARTIAL);
+ ASSERT_NOT_NULL(got);
+ ASSERT_STR_EQ(got, "old=0 new=2 keep=1");
+ free(got);
+#endif
+ PASS();
+}
+
/* A compose project's .env feeds its compose file (variables in image,
* build): changing it is no closure repair -- the unchanged compose file's
* services would keep the old image. It rebuilds; no stale image remains. */
@@ -19431,6 +19512,7 @@ SUITE(pipeline_semantic_manifest_repro) {
RUN_TEST(incremental_deleted_dockerfile_drops_its_image);
RUN_TEST(incremental_dockerfile_edit_keeps_build_edges);
RUN_TEST(incremental_compose_edit_replaces_build_edges);
+ RUN_TEST(incremental_env_edit_replaces_scoped_envvars);
RUN_TEST(incremental_compose_dotenv_change_rebuilds);
RUN_TEST(incremental_dockerfile_edit_keeps_docker_build_edges);
RUN_TEST(incremental_second_build_declarer_keeps_the_edge);