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.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
feat(k8s): a workload USAGE {ref: image} the Image its containers run
Martin Vogel9ae92661359e
diff --git a/internal/cbm/cbm.h b/internal/cbm/cbm.hindex d9084296c..e70d50500 100644--- a/internal/cbm/cbm.h+++ b/internal/cbm/cbm.h@@ -614,13 +614,16 @@ typedef struct {* (name NULL: none); via "ingress" / "httproute".* CBM_K8S_FACT_SOURCE: object def_index (a Flux OCIRepository) fetches* from the URL `name`; a HelmRelease whose chartRef names it deploys the- * chart there. */+ * chart there.+ * CBM_K8S_FACT_IMAGE: object def_index runs a container of image `name`+ * (a literal reference); via "container". */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_SOURCE,+ CBM_K8S_FACT_IMAGE} CBMK8sFactKind;typedef struct {CBMK8sFactKind fact;diff --git a/internal/cbm/extract_k8s.c b/internal/cbm/extract_k8s.cindex 608251263..3b7e25ecc 100644--- a/internal/cbm/extract_k8s.c+++ b/internal/cbm/extract_k8s.c@@ -773,8 +773,35 @@ static void k8s_emit_ref(k8s_walk_t *w, const char *kind, const char *ns, constcbm_k8sfact_push(&w->ctx->result->k8s_facts, a, f);}+// A container's literal image: `path` ends in containers[].image,+// initContainers[].image or ephemeralContainers[].image at any depth (pod+// templates, CronJobs, the pod templates of custom resources).+static bool k8s_image_leaf(const char *path, const char *value) {+ static const char *const LEAVES[] = {"containers[].image", "initContainers[].image",+ "ephemeralContainers[].image"};+ if (!value[0] || strchr(value, '$') || strchr(value, ' ') || strstr(value, "{{")) {+ return false;+ }+ size_t pl = strlen(path);+ for (size_t i = 0; i < sizeof(LEAVES) / sizeof(LEAVES[0]); i++) {+ size_t ll = strlen(LEAVES[i]);+ if (pl >= ll && strcmp(path + pl - ll, LEAVES[i]) == 0 &&+ (pl == ll || path[pl - ll - SKIP_ONE] == '.')) {+ return true;+ }+ }+ return false;+}+// A scalar at `path`: a leaf reference, or a CRD's group / kind.static void k8s_leaf(k8s_walk_t *w, const char *path, const char *value) {+ if (k8s_image_leaf(path, value)) {+ CBMK8sFact f = {.fact = CBM_K8S_FACT_IMAGE,+ .def_index = w->def_index,+ .name = cbm_arena_strdup(w->ctx->arena, value),+ .via = "container"};+ cbm_k8sfact_push(&w->ctx->result->k8s_facts, w->ctx->arena, f);+ }const char *pod = k8s_pod_prefix(w->kind);size_t pl = strlen(pod);if (pl && strncmp(path, pod, pl) == 0 && path[pl] == '.') {diff --git a/src/pipeline/pass_docker.c b/src/pipeline/pass_docker.cindex aa1d20ce7..c431450ef 100644--- a/src/pipeline/pass_docker.c+++ b/src/pipeline/pass_docker.c@@ -249,7 +249,22 @@ static const char *dk_expand(dk_t *d, const dk_var_t *vars, const char *text) {}/* The repository identity of image reference `ref` into out. */+/* Only characters an image reference can hold (name, :tag, @digest) -- a+ * template's placeholder (`<.Values.image>`, `<tpl>`) or an expression is+ * no image. */+static bool dk_ref_chars(const char *ref) {+ for (const char *p = ref; *p; p++) {+ if (!isalnum((unsigned char)*p) && !strchr("._-/:@+", *p)) {+ return false;+ }+ }+ return ref[0] != '\0';+}+static bool dk_normalize(const char *ref, char *out, size_t sz) {+ if (!dk_ref_chars(ref)) {+ return false;+ }char r[CBM_SZ_512];snprintf(r, sizeof(r), "%s", ref);char *at = strchr(r, '@');@@ -306,11 +321,10 @@ static bool dk_is_stage(const dk_t *d, const char *ref) {return false;}-/* 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 cbm_pipeline_image_node(cbm_pipeline_ctx_t *ctx, const char *ref) {char repo[CBM_SZ_512];- if (dk_ieq(ref, "scratch") || !dk_normalize(ref, repo, sizeof(repo))) {- return;+ if (!ref || dk_ieq(ref, "scratch") || !dk_normalize(ref, repo, sizeof(repo))) {+ return 0;}char qn[CBM_SZ_1K];snprintf(qn, sizeof(qn), "__image__%s", repo);@@ -318,7 +332,12 @@ static void dk_link(dk_t *d, const char *ref, const char *via, int line) {cbm_json_escape(esc_repo, (int)sizeof(esc_repo), repo);char node_props[CBM_SZ_2K];snprintf(node_props, sizeof(node_props), "{\"repository\":\"%s\"}", esc_repo);- int64_t img = cbm_gbuf_upsert_node(d->ctx->gbuf, "Image", repo, qn, "", 0, 0, node_props);+ return cbm_gbuf_upsert_node(ctx->gbuf, "Image", repo, qn, "", 0, 0, node_props);+}++/* 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);if (img <= 0) {return;}diff --git a/src/pipeline/pass_k8s.c b/src/pipeline/pass_k8s.cindex 5e316fc5f..41a4e62bc 100644--- a/src/pipeline/pass_k8s.c+++ b/src/pipeline/pass_k8s.c@@ -1159,6 +1159,9 @@ typedef struct {k8s_hr_t *srcs; /* an OCIRepository (node) and the URL it fetches (chart) */int src_n;int src_cap;+ k8s_hr_t *imgs; /* an object (node) and a container image it runs (chart) */+ int img_n;+ int img_cap;} k8s_facts_t;static void k8s_facts_free(k8s_facts_t *f) {@@ -1183,8 +1186,12 @@ static void k8s_facts_free(k8s_facts_t *f) {for (int i = 0; i < f->src_n; i++) {cbm_free(CBM_MEM_CLASS_OTHER, f->srcs[i].chart);}+ for (int i = 0; i < f->img_n; i++) {+ cbm_free(CBM_MEM_CLASS_OTHER, f->imgs[i].chart);+ }cbm_free(CBM_MEM_CLASS_OTHER, f->hrs);cbm_free(CBM_MEM_CLASS_OTHER, f->srcs);+ cbm_free(CBM_MEM_CLASS_OTHER, f->imgs);cbm_free(CBM_MEM_CLASS_OTHER, f->refs);cbm_free(CBM_MEM_CLASS_OTHER, f->crds);cbm_free(CBM_MEM_CLASS_OTHER, f->deploys);@@ -1262,6 +1269,11 @@ static void k8s_facts_keep(k8s_facts_t *f, const CBMFileResult *res, const int64f->srcs[f->src_n++] =(k8s_hr_t){node, cbm_mem_strdup(CBM_MEM_CLASS_OTHER, x->name), NULL};}+ } else if (x->fact == CBM_K8S_FACT_IMAGE) {+ if (x->name && k8s_grow((void **)&f->imgs, &f->img_cap, f->img_n, sizeof(*f->imgs))) {+ 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_CRD) {if (k8s_grow((void **)&f->crds, &f->crd_cap, f->crd_n, sizeof(*f->crds))) {f->crds[f->crd_n++] =@@ -3272,6 +3284,22 @@ static void k8s_link_chart_refs(k8s_dep_link_t *l, const k8s_hr_t *oci) {}}+/* A container image an object runs: USAGE {ref:image} to its Image node --+ * the node a Dockerfile building or building on that image joins. */+static void k8s_link_images(cbm_pipeline_ctx_t *ctx, const k8s_facts_t *f) {+ for (int i = 0; i < f->img_n; i++) {+ int64_t img = cbm_pipeline_image_node(ctx, f->imgs[i].chart);+ if (img <= 0) {+ continue;+ }+ char esc[CBM_SZ_1K];+ cbm_json_escape(esc, (int)sizeof(esc), f->imgs[i].chart);+ char props[CBM_SZ_2K];+ snprintf(props, sizeof(props), "{\"ref\":\"image\",\"image\":\"%s\"}", esc);+ cbm_gbuf_insert_edge(ctx->gbuf, f->imgs[i].node, img, "USAGE", props);+ }+}+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) {@@ -3460,6 +3488,7 @@ int cbm_pipeline_pass_k8s(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files,k8s_resolve_refs(ctx, &bundles, &facts);k8s_link_crds(ctx, &bundles, &facts);k8s_link_deploys(ctx, &kusts, &bundles, &facts);+ k8s_link_images(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.hindex ad35dbbfd..f77981f23 100644--- a/src/pipeline/pipeline_internal.h+++ b/src/pipeline/pipeline_internal.h@@ -1079,6 +1079,11 @@ void cbm_ci_commands(CBMArena *a, const char *rel, const char *src, size_t len,void cbm_pipeline_pass_terraform(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files,int file_count);+/* The Image node of image reference `ref` (its normalised repository:+ * docker.io for no registry host, library/ for an official image; tag and+ * 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);+/* 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.cindex ff5735219..77432e750 100644--- a/tests/test_grammar_probe_f.c+++ b/tests/test_grammar_probe_f.c@@ -1995,6 +1995,62 @@ TEST(probe_dockerfile_entrypoint_runs) {PASS();}+/* Every USAGE {ref:image} edge as "<object> -> <repository>", sorted. */+static char *gpf_image_users(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(group_concat(x, char(10)), '') FROM (SELECT s.name || ' -> ' || "+ "t.name 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='USAGE' AND t.label='Image' AND "+ "json_extract(e.properties,'$.ref')='image' 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) {+ out = strdup((const char *)sqlite3_column_text(st, 0));+ }+ }+ sqlite3_finalize(st);+ }+ gpf_cleanup(&lp, store);+ return out;+}++/* An object running a container USAGE {ref:image} the Image of its literal+ * image reference -- containers, initContainers, any pod template (CronJob,+ * a custom resource). A shell substitution or a template placeholder is no+ * image. */+TEST(probe_k8s_container_images_are_images) {+ 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@sha256:0123\n containers:\n - name: web\n"+ " image: nginx:1.27\n - name: side\n image: $(SIDE_IMAGE)\n"+ "---\napiVersion: batch/v1\nkind: CronJob\nmetadata:\n name: nightly\nspec:\n"+ " jobTemplate:\n spec:\n template:\n spec:\n containers:\n"+ " - name: job\n image: ghcr.io/acme/job:2\n"+ "---\napiVersion: example.com/v1\nkind: Widget\nmetadata:\n name: w\nspec:\n"+ " template:\n spec:\n containers:\n - name: w\n"+ " image: quay.io/acme/widget:3\n - name: placeholder\n"+ " image: <image-built-from-dockerfile>\n"}};+ char *got = gpf_image_users(files, 1);+ static const char want[] = "CronJob/nightly -> ghcr.io/acme/job\n"+ "Deployment/web -> docker.io/library/busybox\n"+ "Deployment/web -> docker.io/library/nginx\n"+ "Widget/w -> quay.io/acme/widget";+ if (!got || strcmp(got, want) != 0) {+ fprintf(stderr, "image users got:\n%s\nwant:\n%s\n", got ? got : "(null)", want);+ free(got);+ FAIL("USAGE {ref:image} edge set");+ }+ free(got);+ 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"@@ -4261,6 +4317,7 @@ SUITE(grammar_probe_f) {RUN_TEST(probe_dockerfile_bases_are_images);RUN_TEST(probe_dockerfile_variants_are_dockerfiles);RUN_TEST(probe_dockerfile_entrypoint_runs);+ RUN_TEST(probe_k8s_container_images_are_images);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);