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(k8s): a HelmRelease with chartRef DEPLOYS the chart its OCIRepository fetches

Martin Vogel77f328300c52
diff --git a/internal/cbm/cbm.h b/internal/cbm/cbm.h
index 4cef7f0f5..d9084296c 100644
--- a/internal/cbm/cbm.h
+++ b/internal/cbm/cbm.h
@@ -611,12 +611,16 @@ typedef struct {
* `ns` (NULL: the objects' own); via "flux" / "argo".
* CBM_K8S_FACT_ROUTE: object def_index (an Ingress, an HTTPRoute) exposes
* `route` ("<host or *><path>") and sends it to the kind/ns/name backend
- * (name NULL: none); via "ingress" / "httproute". */
+ * (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. */
typedef enum {
CBM_K8S_FACT_REF = 0,
CBM_K8S_FACT_CRD,
CBM_K8S_FACT_DEPLOY,
- CBM_K8S_FACT_ROUTE
+ CBM_K8S_FACT_ROUTE,
+ CBM_K8S_FACT_SOURCE
} CBMK8sFactKind;
typedef struct {
CBMK8sFactKind fact;
diff --git a/internal/cbm/extract_k8s.c b/internal/cbm/extract_k8s.c
index ddd114f0e..608251263 100644
--- a/internal/cbm/extract_k8s.c
+++ b/internal/cbm/extract_k8s.c
@@ -731,6 +731,17 @@ static void k8s_deploy_leaf(k8s_walk_t *w, const char *path, const char *value)
}
return;
}
+ /* the chart an OCIRepository fetches (a HelmRelease's chartRef) */
+ if (k8s_source_is(w, K8S_FLUX_SRC, "|OCIRepository|") && strcmp(path, "spec.url") == 0) {
+ if (!strstr(value, "{{") && !strstr(value, "${")) {
+ CBMK8sFact f = {.fact = CBM_K8S_FACT_SOURCE,
+ .def_index = w->def_index,
+ .name = cbm_arena_strdup(w->ctx->arena, value),
+ .via = "ocirepository"};
+ cbm_k8sfact_push(&w->ctx->result->k8s_facts, w->ctx->arena, f);
+ }
+ return;
+ }
if ((flux && strcmp(path, "spec.path") == 0) ||
(argo &&
(strcmp(path, "spec.source.path") == 0 || strcmp(path, "spec.sources[].path") == 0))) {
diff --git a/src/pipeline/pass_k8s.c b/src/pipeline/pass_k8s.c
index 1e6ba15f8..39138e114 100644
--- a/src/pipeline/pass_k8s.c
+++ b/src/pipeline/pass_k8s.c
@@ -1156,6 +1156,9 @@ typedef struct {
k8s_hr_t *hrs;
int hr_n;
int hr_cap;
+ k8s_hr_t *srcs; /* an OCIRepository (node) and the URL it fetches (chart) */
+ int src_n;
+ int src_cap;
} k8s_facts_t;
static void k8s_facts_free(k8s_facts_t *f) {
@@ -1177,7 +1180,11 @@ static void k8s_facts_free(k8s_facts_t *f) {
cbm_free(CBM_MEM_CLASS_OTHER, f->hrs[i].chart);
cbm_free(CBM_MEM_CLASS_OTHER, f->hrs[i].source);
}
+ for (int i = 0; i < f->src_n; i++) {
+ cbm_free(CBM_MEM_CLASS_OTHER, f->srcs[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->refs);
cbm_free(CBM_MEM_CLASS_OTHER, f->crds);
cbm_free(CBM_MEM_CLASS_OTHER, f->deploys);
@@ -1250,6 +1257,11 @@ static void k8s_facts_keep(k8s_facts_t *f, const CBMFileResult *res, const int64
node, cbm_mem_strdup(CBM_MEM_CLASS_OTHER, dir),
x->ns ? cbm_mem_strdup(CBM_MEM_CLASS_OTHER, x->ns) : NULL, x->via};
}
+ } else if (x->fact == CBM_K8S_FACT_SOURCE) {
+ if (x->name && k8s_grow((void **)&f->srcs, &f->src_cap, f->src_n, sizeof(*f->srcs))) {
+ f->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_CRD) {
if (k8s_grow((void **)&f->crds, &f->crd_cap, f->crd_n, sizeof(*f->crds))) {
f->crds[f->crd_n++] =
@@ -3219,9 +3231,49 @@ static int64_t k8s_external_chart(k8s_dep_link_t *l, const char *name, const cha
"{\"source\":\"helm\",\"external\":true}");
}
+/* A HelmRelease whose chartRef names OCIRepository `oci` (its USAGE edge
+ * {"ref":"chartRef"}, resolved with the references) deploys the chart that
+ * repository fetches: the shared external Chart of the URL's last segment. */
+static void k8s_link_chart_refs(k8s_dep_link_t *l, const k8s_hr_t *oci) {
+ const char *url = oci->chart;
+ size_t end = strlen(url);
+ while (end > 0 && url[end - SKIP_ONE] == '/') {
+ end--;
+ }
+ size_t start = end;
+ while (start > 0 && url[start - SKIP_ONE] != '/') {
+ start--;
+ }
+ if (start == 0 || start == end || end - start >= CBM_SZ_256) {
+ return;
+ }
+ char name[CBM_SZ_256];
+ snprintf(name, sizeof(name), "%.*s", (int)(end - start), url + start);
+ char esc_name[CBM_SZ_512];
+ char esc_url[CBM_SZ_1K];
+ cbm_json_escape(esc_name, (int)sizeof(esc_name), name);
+ cbm_json_escape(esc_url, (int)sizeof(esc_url), url);
+ char props[CBM_SZ_2K];
+ snprintf(props, sizeof(props),
+ "{\"via\":\"helmrelease\",\"chart\":\"%s\",\"external\":true,\"url\":\"%s\"}",
+ esc_name, esc_url);
+ const cbm_gbuf_edge_t **in = NULL;
+ int n = 0;
+ if (cbm_gbuf_find_edges_by_target_type(l->ctx->gbuf, oci->node, "USAGE", &in, &n) != 0) {
+ return;
+ }
+ for (int i = 0; i < n; i++) {
+ const char *p = in[i]->properties_json;
+ const cbm_gbuf_node_t *hr = cbm_gbuf_find_by_id(l->ctx->gbuf, in[i]->source_id);
+ if (p && strstr(p, "\"ref\":\"chartRef\"") && hr && hr->file_path) {
+ k8s_deploys_edge(l, hr->id, k8s_external_chart(l, name, hr->file_path), 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) {
+ if (f->dep_n == 0 && f->hr_n == 0 && f->src_n == 0) {
return;
}
k8s_dep_link_t l = {.ctx = ctx,
@@ -3292,6 +3344,9 @@ static void k8s_link_deploys(cbm_pipeline_ctx_t *ctx, k8s_kusts_t *ks, k8s_bundl
props);
}
}
+ for (int s = 0; l.files && l.charts && s < f->src_n; s++) {
+ k8s_link_chart_refs(&l, &f->srcs[s]);
+ }
cbm_free(CBM_MEM_CLASS_OTHER, (void *)members.items);
cbm_free(CBM_MEM_CLASS_OTHER, (void *)units.items);
cbm_free(CBM_MEM_CLASS_OTHER, (void *)manifests.items);
diff --git a/tests/test_grammar_probe_f.c b/tests/test_grammar_probe_f.c
index 818301477..fb1f1593c 100644
--- a/tests/test_grammar_probe_f.c
+++ b/tests/test_grammar_probe_f.c
@@ -1825,6 +1825,36 @@ static char *gpf_runs(const GpfFile *files, int nfiles) {
#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"
+/* A HelmRelease whose chartRef names an OCIRepository deploys the chart that
+ * repository fetches: the external chart of the oci:// URL's last segment.
+ * No edge for a URL with a variable, nor for a chartRef naming nothing. */
+TEST(probe_helmrelease_chartref_deploys_its_oci_chart) {
+ static const GpfFile files[] = {
+ {"apps/web/kustomization.yaml",
+ GPF_KUST "namespace: web\nresources:\n - ocirepository.yaml\n - helmrelease.yaml\n"},
+ {"apps/web/ocirepository.yaml",
+ "apiVersion: source.toolkit.fluxcd.io/v1\nkind: OCIRepository\nmetadata:\n"
+ " name: web-chart\nspec:\n interval: 15m\n ref:\n tag: 1.2.3\n"
+ " url: oci://ghcr.io/acme/charts/web-app\n"},
+ {"apps/web/helmrelease.yaml",
+ "apiVersion: helm.toolkit.fluxcd.io/v2\nkind: HelmRelease\nmetadata:\n name: web\n"
+ "spec:\n chartRef:\n kind: OCIRepository\n name: web-chart\n values:\n"
+ " fullnameOverride: '{{ .Release.Name }}'\n"},
+ {"apps/db/kustomization.yaml",
+ GPF_KUST "namespace: db\nresources:\n - ocirepository.yaml\n - helmrelease.yaml\n"},
+ {"apps/db/ocirepository.yaml",
+ "apiVersion: source.toolkit.fluxcd.io/v1\nkind: OCIRepository\nmetadata:\n"
+ " name: db-chart\nspec:\n url: oci://ghcr.io/acme/charts/${CHART}\n"},
+ {"apps/db/helmrelease.yaml",
+ "apiVersion: helm.toolkit.fluxcd.io/v2\nkind: HelmRelease\nmetadata:\n name: db\n"
+ "spec:\n chartRef:\n kind: OCIRepository\n name: db-chart\n"},
+ {"apps/x/helmrelease.yaml",
+ "apiVersion: helm.toolkit.fluxcd.io/v2\nkind: HelmRelease\nmetadata:\n name: x\n"
+ "spec:\n chartRef:\n kind: OCIRepository\n name: missing\n"}};
+ GPF_ASSERT_DEPLOYS(files, 7, "HelmRelease/web -> chart:web-app(ext) (helmrelease)");
+ PASS();
+}
+
/* kubectl apply -f deploys a manifest, a directory's manifests (-R: below
* it too; not a file holding no objects), a glob's; -k and a kustomize build
* piped into kubectl a Kustomization. The source is the function running the
@@ -4063,6 +4093,7 @@ SUITE(grammar_probe_f) {
RUN_TEST(probe_ci_and_scripts_run_scripts);
RUN_TEST(probe_make_targets_spawn_their_rules);
RUN_TEST(probe_make_rules_have_a_node_per_target);
+ RUN_TEST(probe_helmrelease_chartref_deploys_its_oci_chart);
RUN_TEST(probe_go_installer_deploys_charts);
RUN_TEST(probe_terraform_helm_release_deploys_its_chart);
RUN_TEST(probe_helm_templated_objects_are_resources);