Skip to content

Commit 6316733

Browse files
committed
feat(infra): a container's environment reaches its own program
The variables a container sets (K8s env, compose environment, Dockerfile ENV) were container-scoped EnvVars of their own, joined to nothing: the readers of a name point at the global EnvVar, which every program reading that name shares, so a join through it was in the right program for 23 % of the pairs. Each scoped EnvVar now CONFIGURES {strategy:env_read, dockerfile} the readers of its name inside the files the container's image is built from: - the image: a K8s container's literal image, built from exactly one Dockerfile (Image -DEPENDS_ON {via:build}->); a compose service's own build, else its image's; a Dockerfile's own ENV. A K8s image reference now names its container (USAGE {ref:image, container}), so a two-container pod joins each container to its own image. - its files: the context sources COPY / ADD put into the final stage, the stages it is built FROM and the stages it copies from, resolved in the build context (the Dockerfile's directory or the repository root, the one the sources exist under), less the context's .dockerignore, never a test file. They are kept on the Dockerfile's Module (image_context, image_files, image_ignore). pass_docker now reads COPY / ADD from the instruction's own text, as Docker does: the grammar lost a JSON form opening with `/` and, after a word with a trailing blank, took the following instructions for more sources. Incremental: every run recomputes every join edge. A closure repair reads the inputs of the files it leaves alone from the store and replaces the stored edges wholesale; a .dockerignore (now a semantic-manifest control) changing, or a deletion of a file an image copies, rebuilds in full. CBM_SEMANTIC_INDEX_VERSION 8 -> 9. Measured on 17 infrastructure repositories: 80 pairs, 80 correct by hand; the graph matches an independent reading of the Dockerfiles 80/80. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 8aa5259 commit 6316733

12 files changed

Lines changed: 1284 additions & 87 deletions

‎Makefile.cbm‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,7 @@ PIPELINE_SRCS = \
541541
src/pipeline/pass_shell.c \
542542
src/pipeline/pass_terraform.c \
543543
src/pipeline/pass_docker.c \
544+
src/pipeline/pass_envjoin.c \
544545
src/pipeline/pass_ci.c \
545546
src/pipeline/pass_similarity.c \
546547
src/pipeline/pass_semantic_edges.c \

‎internal/cbm/cbm.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -713,8 +713,8 @@ typedef struct {
713713
* CBM_K8S_FACT_SOURCE: object def_index (a Flux OCIRepository) fetches
714714
* from the URL `name`; a HelmRelease whose chartRef names it deploys the
715715
* chart there.
716-
* CBM_K8S_FACT_IMAGE: object def_index runs a container of image `name`
717-
* (a literal reference); via "container".
716+
* CBM_K8S_FACT_IMAGE: object def_index runs its container `container`
717+
* (NULL: unnamed) from image `name` (a literal reference); via "container".
718718
* CBM_K8S_FACT_ENV: object def_index sets environment variable `name` in
719719
* its container `container` (NULL: unnamed) at `line`; via = where the
720720
* value comes from ("value", or the valueFrom kind) -- never the value. */

‎internal/cbm/extract_k8s.c‎

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -773,35 +773,13 @@ static void k8s_emit_ref(k8s_walk_t *w, const char *kind, const char *ns, const
773773
cbm_k8sfact_push(&w->ctx->result->k8s_facts, a, f);
774774
}
775775

776-
// A container's literal image: `path` ends in containers[].image,
777-
// initContainers[].image or ephemeralContainers[].image at any depth (pod
778-
// templates, CronJobs, the pod templates of custom resources).
779-
static bool k8s_image_leaf(const char *path, const char *value) {
780-
static const char *const LEAVES[] = {"containers[].image", "initContainers[].image",
781-
"ephemeralContainers[].image"};
782-
if (!value[0] || strchr(value, '$') || strchr(value, ' ') || strstr(value, "{{")) {
783-
return false;
784-
}
785-
size_t pl = strlen(path);
786-
for (size_t i = 0; i < sizeof(LEAVES) / sizeof(LEAVES[0]); i++) {
787-
size_t ll = strlen(LEAVES[i]);
788-
if (pl >= ll && strcmp(path + pl - ll, LEAVES[i]) == 0 &&
789-
(pl == ll || path[pl - ll - SKIP_ONE] == '.')) {
790-
return true;
791-
}
792-
}
793-
return false;
776+
// A literal image reference: no variable, no template, no blank.
777+
static bool k8s_image_literal(const char *value) {
778+
return value && value[0] && !strchr(value, '$') && !strchr(value, ' ') && !strstr(value, "{{");
794779
}
795780

796781
// A scalar at `path`: a leaf reference, or a CRD's group / kind.
797782
static void k8s_leaf(k8s_walk_t *w, const char *path, const char *value) {
798-
if (k8s_image_leaf(path, value)) {
799-
CBMK8sFact f = {.fact = CBM_K8S_FACT_IMAGE,
800-
.def_index = w->def_index,
801-
.name = cbm_arena_strdup(w->ctx->arena, value),
802-
.via = "container"};
803-
cbm_k8sfact_push(&w->ctx->result->k8s_facts, w->ctx->arena, f);
804-
}
805783
const char *pod = k8s_pod_prefix(w->kind);
806784
size_t pl = strlen(pod);
807785
if (pl && strncmp(path, pod, pl) == 0 && path[pl] == '.') {
@@ -1086,10 +1064,13 @@ static const char *k8s_env_source(k8s_walk_t *w, TSNode item) {
10861064
return "value";
10871065
}
10881066

1089-
// The environment variables a container (`path` ends in containers[],
1090-
// initContainers[] or ephemeralContainers[], any depth) sets: each env[]
1091-
// name, with where its value comes from -- never the value.
1092-
static void k8s_container_env(k8s_walk_t *w, const char *path, TSNode container) {
1067+
// A container (`path` ends in containers[], initContainers[] or
1068+
// ephemeralContainers[], any depth: pod templates, CronJobs, the pod
1069+
// templates of custom resources): its literal image, and the environment
1070+
// variables it sets -- each env[] name, with where its value comes from,
1071+
// never the value. Both carry the container's name, so a variable joins the
1072+
// program of its own container's image.
1073+
static void k8s_container(k8s_walk_t *w, const char *path, TSNode container) {
10931074
static const char *const ITEMS[] = {"containers[]", "initContainers[]",
10941075
"ephemeralContainers[]"};
10951076
size_t pl = strlen(path);
@@ -1104,6 +1085,15 @@ static void k8s_container_env(k8s_walk_t *w, const char *path, TSNode container)
11041085
}
11051086
const char *cname = k8s_text(w, container, "name");
11061087
cname = cname && cname[0] && !strstr(cname, "{{") ? cname : NULL;
1088+
const char *image = k8s_text(w, container, "image");
1089+
if (k8s_image_literal(image)) {
1090+
CBMK8sFact f = {.fact = CBM_K8S_FACT_IMAGE,
1091+
.def_index = w->def_index,
1092+
.name = image,
1093+
.container = cname,
1094+
.via = "container"};
1095+
cbm_k8sfact_push(&w->ctx->result->k8s_facts, w->ctx->arena, f);
1096+
}
11071097
k8s_iter_t env = k8s_iter(k8s_child(w, container, "env"));
11081098
TSNode item;
11091099
while (k8s_next(&env, &item)) {
@@ -1123,7 +1113,7 @@ static void k8s_container_env(k8s_walk_t *w, const char *path, TSNode container)
11231113

11241114
// A mapping at `path` that holds a reference in its kind/name/namespace.
11251115
static void k8s_mapping(k8s_walk_t *w, const char *path, TSNode mapping) {
1126-
k8s_container_env(w, path, mapping);
1116+
k8s_container(w, path, mapping);
11271117
for (size_t i = 0; i < sizeof(k8s_mapping_refs) / sizeof(k8s_mapping_refs[0]); i++) {
11281118
const k8s_mapping_ref_t *r = &k8s_mapping_refs[i];
11291119
if (k8s_kind_in(r->kinds, w->kind) && strcmp(path, r->path) == 0) {

‎scripts/check-lsp-originality.sh‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ REFS=(
8383
# Infrastructure hybrid LSP sources outside internal/cbm/lsp/ (scanned too).
8484
INFRA_SRC=(internal/cbm/extract_k8s.c src/pipeline/pass_k8s.c src/pipeline/pass_operator.c
8585
src/pipeline/pass_shell.c src/pipeline/pass_terraform.c
86-
src/pipeline/pass_ci.c src/pipeline/pass_docker.c)
86+
src/pipeline/pass_ci.c src/pipeline/pass_docker.c src/pipeline/pass_envjoin.c)
8787

8888
ONLY_LANG=""; REFRESH=0; LIST_ONLY=0
8989
while [ $# -gt 0 ]; do

0 commit comments

Comments
 (0)