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): Go operators link to the kinds they reconcile

Martin Vogeldd3a294737df
diff --git a/Makefile.cbm b/Makefile.cbm
index 76ba5b467..1dfc9b8ec 100644
--- a/Makefile.cbm
+++ b/Makefile.cbm
@@ -518,6 +518,7 @@ PIPELINE_SRCS = \
src/pipeline/pass_compile_commands.c \
src/pipeline/pass_infrascan.c \
src/pipeline/pass_k8s.c \
+ src/pipeline/pass_operator.c \
src/pipeline/pass_similarity.c \
src/pipeline/pass_semantic_edges.c \
src/pipeline/pass_complexity.c \
diff --git a/src/graph_buffer/graph_buffer.c b/src/graph_buffer/graph_buffer.c
index fd81f2c40..5b17ec273 100644
--- a/src/graph_buffer/graph_buffer.c
+++ b/src/graph_buffer/graph_buffer.c
@@ -1131,6 +1131,47 @@ static void gb_free_span_vec(const char *qn, void *value, void *ud) {
gb_span_vec_free(value);
}
+void cbm_gbuf_foreach_span(const cbm_gbuf_t *gb, const cbm_gbuf_node_t *n, cbm_gbuf_span_fn fn,
+ void *ud) {
+ if (!gb || !n || !fn) {
+ return;
+ }
+ const gb_span_vec_t *vec = gb->variant_spans && n->qualified_name
+ ? cbm_ht_get(gb->variant_spans, n->qualified_name)
+ : NULL;
+ if (vec && vec->count > 0) {
+ for (size_t i = 0; i < vec->count; i++) {
+ fn(vec->items[i].file, vec->items[i].start, vec->items[i].end, ud);
+ }
+ return;
+ }
+ if (gb_props_have_variants(n->properties_json)) {
+ yyjson_doc *doc = yyjson_read_opts((char *)n->properties_json, strlen(n->properties_json),
+ 0, &gb_yy_alc, NULL);
+ yyjson_val *list = gb_variants_list(doc);
+ size_t i;
+ size_t max;
+ yyjson_val *v;
+ bool any = false;
+ yyjson_arr_foreach(list, i, max, v) {
+ const char *f = yyjson_get_str(yyjson_obj_get(v, "file_path"));
+ int s = (int)yyjson_get_int(yyjson_obj_get(v, "start_line"));
+ int e = (int)yyjson_get_int(yyjson_obj_get(v, "end_line"));
+ if (f && s > 0) {
+ fn(f, s, e, ud);
+ any = true;
+ }
+ }
+ yyjson_doc_free(doc);
+ if (any) {
+ return;
+ }
+ }
+ if (n->file_path && n->start_line > 0) {
+ fn(n->file_path, n->start_line, n->end_line, ud);
+ }
+}
+
void cbm_gbuf_settle_variants(cbm_gbuf_t *gb) {
if (!gb || !gb->variant_spans) {
return;
diff --git a/src/graph_buffer/graph_buffer.h b/src/graph_buffer/graph_buffer.h
index a0783261d..b7a4e746a 100644
--- a/src/graph_buffer/graph_buffer.h
+++ b/src/graph_buffer/graph_buffer.h
@@ -194,4 +194,11 @@ int cbm_gbuf_node_set_properties_json(cbm_gbuf_node_t *node, const char *json);
* properties_json from the buffer after collisions. */
void cbm_gbuf_settle_variants(cbm_gbuf_t *gb);
+/* Visit every definition span node `n` stands for: the spans gathered from
+ * same-QN collisions (not yet written, or already in its `variants`), else
+ * its own. For passes that place a source line inside a definition. */
+typedef void (*cbm_gbuf_span_fn)(const char *file, int start, int end, void *ud);
+void cbm_gbuf_foreach_span(const cbm_gbuf_t *gb, const cbm_gbuf_node_t *n, cbm_gbuf_span_fn fn,
+ void *ud);
+
#endif /* CBM_GRAPH_BUFFER_H */
diff --git a/src/pipeline/pass_k8s.c b/src/pipeline/pass_k8s.c
index f09037b08..bbc465be9 100644
--- a/src/pipeline/pass_k8s.c
+++ b/src/pipeline/pass_k8s.c
@@ -1252,10 +1252,36 @@ static bool k8s_same_bundle(k8s_bundles_t *b, const char *a, const char *c) {
* kind. A CRD vendored in several places: the copy deployed with the
* resource (one bundle) is the one; with no copy beside it, the resource
* stands for all of them. */
+/* A CRD node says which group/kind it declares (search, and the Go operator
+ * pass's CRD -> type link). */
+static void k8s_mark_crd(cbm_pipeline_ctx_t *ctx, const k8s_crd_t *crd) {
+ cbm_gbuf_node_t *n = (cbm_gbuf_node_t *)cbm_gbuf_find_by_id(ctx->gbuf, crd->node);
+ const char *props = n ? n->properties_json : NULL;
+ size_t pl = props ? strlen(props) : 0;
+ if (!props || pl < PAIR_LEN || props[pl - SKIP_ONE] != '}' || strstr(props, "\"crd_kind\"")) {
+ return;
+ }
+ char group[K8S_LABEL_LEN * PAIR_LEN];
+ char kind[K8S_LABEL_LEN * PAIR_LEN];
+ cbm_json_escape(group, (int)sizeof(group), crd->group);
+ cbm_json_escape(kind, (int)sizeof(kind), crd->kind);
+ size_t n_out = pl + strlen(group) + strlen(kind) + CBM_SZ_64;
+ char *out = cbm_alloc(CBM_MEM_CLASS_OTHER, n_out);
+ if (out) {
+ snprintf(out, n_out, "%.*s,\"crd_group\":\"%s\",\"crd_kind\":\"%s\"}", (int)(pl - SKIP_ONE),
+ props, group, kind);
+ (void)cbm_gbuf_node_set_properties_json(n, out);
+ cbm_free(CBM_MEM_CLASS_OTHER, out);
+ }
+}
+
static void k8s_link_crds(cbm_pipeline_ctx_t *ctx, k8s_bundles_t *b, const k8s_facts_t *f) {
if (f->crd_n == 0) {
return;
}
+ for (int i = 0; i < f->crd_n; i++) {
+ k8s_mark_crd(ctx, &f->crds[i]);
+ }
/* group/kind -> the run of its CRDs in `order` (sorted by group/kind), so
* each resource visits only the CRDs of its own type. */
k8s_crd_key_t *order = cbm_calloc(CBM_MEM_CLASS_OTHER, (size_t)f->crd_n * sizeof(*order));
diff --git a/src/pipeline/pass_operator.c b/src/pipeline/pass_operator.c
new file mode 100644
index 000000000..0c0bdc468
--- /dev/null
+++ b/src/pipeline/pass_operator.c
@@ -0,0 +1,944 @@
+/*
+ * pass_operator.c — Go Kubernetes operators (PHASE1 P1.2).
+ *
+ * A controller-runtime operator ties Go code to Kubernetes kinds in three
+ * ways the type graph alone does not carry:
+ * - a controller is built `For(&v1.Widget{})`, `Owns(&v1.Gadget{})`,
+ * `Watches(&v1.X{}, ...)` or watches `source.Kind(cache, &v1.X{})`:
+ * HANDLES {via: controller, role} from the function that builds it to
+ * the Go type, resolved as Go does: `v1` is an import of the file, and
+ * the type is the struct of that name in the imported package's
+ * directory (unqualified: the file's own package);
+ * - a CRD's group/kind is backed by `type <Kind> struct` in the package
+ * marked `+groupName=<group>` (or `GroupName = "<group>"`): USAGE
+ * {ref: crd-type} from the CRD Resource to that Struct;
+ * - `//go:embed <patterns>` puts files into the binary: REFERENCES_FILE
+ * {via: go:embed} from the Go file to each embedded file.
+ * Runs after USAGE resolution; reads each Go file once.
+ */
+#include "foundation/constants.h"
+#include "foundation/compat.h"
+#include "foundation/compat_fs.h"
+#include "foundation/hash_table.h"
+#include "foundation/limits.h"
+#include "foundation/log.h"
+#include "foundation/mem_core.h"
+#include "foundation/str_util.h"
+#include "graph_buffer/graph_buffer.h"
+#include "pipeline/pipeline.h"
+#include "pipeline/pipeline_internal.h"
+
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define OP_SLEN(lit) (sizeof(lit) - SKIP_ONE)
+
+enum { OP_GROW_MIN = 16, OP_IDENT_MAX = 128 };
+
+/* Read a whole file into a core-owned NUL-terminated buffer, NULL on error
+ * or past the indexer's file-size policy. */
+static char *op_read_file(const char *path) {
+ FILE *f = cbm_fopen(path, "rb");
+ if (!f) {
+ return NULL;
+ }
+ (void)fseek(f, 0, SEEK_END);
+ long size = ftell(f);
+ (void)fseek(f, 0, SEEK_SET);
+ if (size <= 0 || size > cbm_max_file_bytes()) {
+ (void)fclose(f);
+ return NULL;
+ }
+ char *buf = cbm_alloc(CBM_MEM_CLASS_OTHER, (size_t)size + SKIP_ONE);
+ if (buf) {
+ size_t n = fread(buf, SKIP_ONE, (size_t)size, f);
+ buf[n] = '\0';
+ }
+ (void)fclose(f);
+ return buf;
+}
+
+static void op_dirname(const char *path, char *out, size_t out_sz) {
+ const char *slash = strrchr(path, '/');
+ snprintf(out, out_sz, "%.*s", slash ? (int)(slash - path) : 0, path);
+}
+
+/* ── Functions per file ──────────────────────────────────────────────── */
+
+/* One definition span of a function node: Go method QNs carry no receiver,
+ * so the methods of several types in one package (every reconciler's
+ * SetupWithManager) are one node standing for spans in several files. */
+typedef struct {
+ const cbm_gbuf_node_t *node;
+ int start;
+ int end;
+} op_span_t;
+
+typedef struct {
+ op_span_t *items;
+ int count;
+ int cap;
+} op_spans_t;
+
+static void op_spans_push(op_spans_t *v, op_span_t span) {
+ if (v->count == v->cap) {
+ int cap = v->cap ? v->cap * PAIR_LEN : OP_GROW_MIN;
+ op_span_t *grown = cbm_realloc(CBM_MEM_CLASS_OTHER, v->items, (size_t)cap * sizeof(*grown));
+ if (!grown) {
+ return;
+ }
+ v->items = grown;
+ v->cap = cap;
+ }
+ v->items[v->count++] = span;
+}
+
+static void op_free_spans(const char *key, void *value, void *ud) {
+ (void)ud;
+ op_spans_t *v = value;
+ cbm_free(CBM_MEM_CLASS_OTHER, v->items);
+ cbm_free(CBM_MEM_CLASS_OTHER, v);
+ cbm_free(CBM_MEM_CLASS_OTHER, (void *)key);
+}
+
+typedef struct {
+ CBMHashTable *by_file;
+ const cbm_gbuf_node_t *node;
+} op_index_ctx_t;
+
+static void op_index_span(const char *file, int start, int end, void *ud) {
+ op_index_ctx_t *c = ud;
+ op_spans_t *v = cbm_ht_get(c->by_file, file);
+ if (!v) {
+ v = cbm_calloc(CBM_MEM_CLASS_OTHER, sizeof(*v));
+ char *key = cbm_mem_strdup(CBM_MEM_CLASS_OTHER, file);
+ if (!v || !key) {
+ cbm_free(CBM_MEM_CLASS_OTHER, v);
+ cbm_free(CBM_MEM_CLASS_OTHER, key);
+ return;
+ }
+ cbm_ht_set(c->by_file, key, v);
+ }
+ op_spans_push(v, (op_span_t){c->node, start, end});
+}
+
+/* file_path -> the spans of Function and Method nodes in it (owned keys). */
+static CBMHashTable *op_functions_by_file(const cbm_gbuf_t *gb) {
+ CBMHashTable *by_file = cbm_ht_create(0);
+ static const char *const labels[] = {"Function", "Method"};
+ for (size_t l = 0; by_file && l < sizeof(labels) / sizeof(labels[0]); l++) {
+ const cbm_gbuf_node_t **nodes = NULL;
+ int count = 0;
+ if (cbm_gbuf_find_by_label(gb, labels[l], &nodes, &count) != 0) {
+ continue;
+ }
+ for (int i = 0; i < count; i++) {
+ op_index_ctx_t c = {by_file, nodes[i]};
+ cbm_gbuf_foreach_span(gb, nodes[i], op_index_span, &c);
+ }
+ }
+ return by_file;
+}
+
+/* Is span `a` a better enclosing function than `b`: shorter, then (equal
+ * spans of two nodes) the lower QN, so the choice never follows index order. */
+static bool op_span_better(const op_span_t *a, const op_span_t *b) {
+ int la = a->end - a->start;
+ int lb = b->end - b->start;
+ if (la != lb) {
+ return la < lb;
+ }
+ const char *qa = a->node->qualified_name ? a->node->qualified_name : "";
+ const char *qb = b->node->qualified_name ? b->node->qualified_name : "";
+ return strcmp(qa, qb) < 0;
+}
+
+/* The innermost function of `file` spanning `line`. */
+static const cbm_gbuf_node_t *op_enclosing(CBMHashTable *by_file, const char *file, int line) {
+ const op_spans_t *v = cbm_ht_get(by_file, file);
+ const op_span_t *best = NULL;
+ for (int i = 0; v && i < v->count; i++) {
+ const op_span_t *sp = &v->items[i];
+ if (sp->start <= line && line <= sp->end && (!best || op_span_better(sp, best))) {
+ best = sp;
+ }
+ }
+ return best ? best->node : NULL;
+}
+
+/* ── Go modules: import path -> directory ─────────────────────────────── */
+
+/* A go.mod of the repository: module path and the directory it roots. */
+typedef struct {
+ char *path;
+ char *dir;
+} op_module_t;
+
+typedef struct {
+ op_module_t *items;
+ int count;
+ int cap;
+} op_modules_t;
+
+static void op_modules_free(op_modules_t *m) {
+ for (int i = 0; i < m->count; i++) {
+ cbm_free(CBM_MEM_CLASS_OTHER, m->items[i].path);
+ cbm_free(CBM_MEM_CLASS_OTHER, m->items[i].dir);
+ }
+ cbm_free(CBM_MEM_CLASS_OTHER, m->items);
+ *m = (op_modules_t){0};
+}
+
+/* Note the `module` line of a go.mod at `file`. */
+static void op_note_module(op_modules_t *m, const char *file, const char *text) {
+ for (const char *p = strstr(text, "module"); p; p = strstr(p + SKIP_ONE, "module")) {
+ if ((p > text && p[-SKIP_ONE] != '\n') ||
+ (p[OP_SLEN("module")] != ' ' && p[OP_SLEN("module")] != '\t')) {
+ continue;
+ }
+ const char *q = p + OP_SLEN("module");
+ while (*q == ' ' || *q == '\t' || *q == '"') {
+ q++;
+ }
+ size_t n = strcspn(q, " \t\r\n\"/");
+ while (q[n] == '/') { /* module paths contain '/' */
+ n += SKIP_ONE + strcspn(q + n + SKIP_ONE, " \t\r\n\"/");
+ }
+ if (n == 0) {
+ return;
+ }
+ if (m->count == m->cap) {
+ int cap = m->cap ? m->cap * PAIR_LEN : OP_GROW_MIN;
+ op_module_t *grown =
+ cbm_realloc(CBM_MEM_CLASS_OTHER, m->items, (size_t)cap * sizeof(*grown));
+ if (!grown) {
+ return;
+ }
+ m->items = grown;
+ m->cap = cap;
+ }
+ char path[CBM_SZ_1K];
+ char dir[CBM_SZ_1K];
+ snprintf(path, sizeof(path), "%.*s", (int)n, q);
+ op_dirname(file, dir, sizeof(dir));
+ m->items[m->count].path = cbm_mem_strdup(CBM_MEM_CLASS_OTHER, path);
+ m->items[m->count].dir = cbm_mem_strdup(CBM_MEM_CLASS_OTHER, dir);
+ m->count++;
+ return;
+ }
+}
+
+/* The repository directory of import path `import`: below the go.mod whose
+ * module path is its longest prefix ("" when no module of the repository
+ * has it). */
+static bool op_import_dir(const op_modules_t *m, const char *import, char *out, size_t out_sz) {
+ const op_module_t *best = NULL;
+ for (int i = 0; i < m->count; i++) {
+ size_t pl = strlen(m->items[i].path);
+ if (strncmp(import, m->items[i].path, pl) == 0 &&
+ (import[pl] == '\0' || import[pl] == '/') && (!best || pl > strlen(best->path))) {
+ best = &m->items[i];
+ }
+ }
+ if (!best) {
+ return false;
+ }
+ const char *rest = import + strlen(best->path);
+ rest += *rest == '/';
+ snprintf(out, out_sz, "%s%s%s", best->dir, best->dir[0] && rest[0] ? "/" : "", rest);
+ return true;
+}
+
+/* ── Controllers: For / Owns / Watches / source.Kind ──────────────────── */
+
+static int op_line_of(const char *text, const char *at) {
+ int line = SKIP_ONE;
+ for (const char *p = text; p < at; p++) {
+ line += *p == '\n';
+ }
+ return line;
+}
+
+static bool op_is_quote(char c) {
+ return c == '"' || c == '\'' || c == '`';
+}
+
+/* Where a `//` comment starts in the line text [ls, le), outside string and
+ * rune literals; le when there is none. */
+static const char *op_comment_start(const char *ls, const char *le) {
+ char quote = 0;
+ for (const char *p = ls; p < le; p++) {
+ if (quote) {
+ if (*p == '\\' && quote != '`' && p + SKIP_ONE < le) {
+ p++;
+ } else if (*p == quote) {
+ quote = 0;
+ }
+ } else if (op_is_quote(*p)) {
+ quote = *p;
+ } else if (*p == '/' && p + SKIP_ONE < le && p[SKIP_ONE] == '/') {
+ return p;
+ }
+ }
+ return le;
+}
+
+static const char *op_line_start(const char *text, const char *p) {
+ while (p > text && p[-SKIP_ONE] != '\n') {
+ p--;
+ }
+ return p;
+}
+
+/* The code character before `at`, past blanks and `//` comments: gofmt
+ * puts a chain's '.' at the end of an earlier line, and comment lines may
+ * sit between. NULL at the start of the text. */
+static const char *op_prev_code(const char *text, const char *at) {
+ const char *le = at;
+ for (;;) {
+ const char *ls = op_line_start(text, le);
+ const char *end = op_comment_start(ls, le);
+ while (end > ls &&
+ (end[-SKIP_ONE] == ' ' || end[-SKIP_ONE] == '\t' || end[-SKIP_ONE] == '\r')) {
+ end--;
+ }
+ if (end > ls) {
+ return end - SKIP_ONE;
+ }
+ if (ls == text) {
+ return NULL;
+ }
+ le = ls - SKIP_ONE; /* the previous line, without its '\n' */
+ }
+}
+
+/* The end of the argument list opened at `open` ('('), balanced; parentheses
+ * in `//` comments and literals do not count. */
+static const char *op_args_end(const char *open) {
+ int depth = 0;
+ char quote = 0;
+ for (const char *p = open; *p; p++) {
+ if (quote) {
+ if (*p == '\\' && quote != '`' && p[SKIP_ONE]) {
+ p++;
+ } else if (*p == quote || (*p == '\n' && quote != '`')) {
+ quote = 0;
+ }
+ continue;
+ }
+ if (op_is_quote(*p)) {
+ quote = *p;
+ continue;
+ }
+ if (*p == '/' && p[SKIP_ONE] == '/') {
+ p = strchr(p, '\n');
+ if (!p) {
+ break;
+ }
+ continue;
+ }
+ depth += *p == '(';
+ depth -= *p == ')';
+ if (depth == 0) {
+ return p;
+ }
+ }
+ return open + strlen(open);
+}
+
+static bool op_ident_char(char c) {
+ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_';
+}
+
+/* The import path a file binds to `alias` (an explicit alias, else the
+ * path's last segment, Go's usual package name); "" when none. */
+static void op_import_path(const char *text, const char *alias, char *out, size_t out_sz) {
+ out[0] = '\0';
+ size_t al = strlen(alias);
+ for (const char *p = strstr(text, "import"); p; p = strstr(p + SKIP_ONE, "import")) {
+ if ((p > text && p[-SKIP_ONE] != '\n') || op_ident_char(p[OP_SLEN("import")])) {
+ continue;
+ }
+ const char *q = p + OP_SLEN("import");
+ const char *end = q;
+ while (*end == ' ' || *end == '\t') {
+ end++;
+ }
+ end = *end == '(' ? strchr(end, ')') : strchr(end, '\n');
+ end = end ? end : q + strlen(q);
+ /* each import line: [alias] "path" */
+ for (const char *l = q; l < end;) {
+ while (l < end && (*l == ' ' || *l == '\t' || *l == '(' || *l == '\n' || *l == '\r')) {
+ l++;
+ }
+ const char *name = l;
+ while (l < end && op_ident_char(*l)) {
+ l++;
+ }
+ size_t nl = (size_t)(l - name);
+ while (l < end && (*l == ' ' || *l == '\t')) {
+ l++;
+ }
+ if (l >= end || *l != '"') {
+ while (l < end && *l != '\n') {
+ l++;
+ }
+ continue;
+ }
+ const char *ps = l + SKIP_ONE;
+ const char *pe = strchr(ps, '"');
+ if (!pe || pe > end) {
+ break;
+ }
+ const char *seg = pe;
+ while (seg > ps && seg[-SKIP_ONE] != '/') {
+ seg--;
+ }
+ bool hit = nl ? (nl == al && strncmp(name, alias, al) == 0)
+ : ((size_t)(pe - seg) == al && strncmp(seg, alias, al) == 0);
+ if (hit) {
+ snprintf(out, out_sz, "%.*s", (int)(pe - ps), ps);
+ return;
+ }
+ l = pe + SKIP_ONE;
+ }
+ }
+}
+
+/* Is directory `dir` the package at import path `path`: the path ends with
+ * the directory, on a segment boundary (the module path is its prefix). */
+static bool op_dir_is_import(const char *dir, const char *path) {
+ size_t dl = strlen(dir);
+ size_t pl = strlen(path);
+ if (dl == 0 || dl > pl || strcmp(path + pl - dl, dir) != 0) {
+ return false;
+ }
+ return dl == pl || path[pl - dl - SKIP_ONE] == '/';
+}
+
+/* HANDLES from `fn` to the Go type `type` resolved as Go resolves it: the
+ * package the file imports as `qual`, or (unqualified) the file's own. */
+static int op_handle(cbm_gbuf_t *gb, const op_modules_t *mods, const cbm_gbuf_node_t *fn,
+ const char *file, const char *text, const char *qual, const char *type,
+ const char *role) {
+ char import[CBM_SZ_1K] = "";
+ char own_dir[CBM_SZ_1K];
+ char pkg_dir[CBM_SZ_1K] = "";
+ op_dirname(file, own_dir, sizeof(own_dir));
+ bool in_repo = false;
+ if (qual[0]) {
+ op_import_path(text, qual, import, sizeof(import));
+ if (!import[0]) {
+ return 0;
+ }
+ in_repo = op_import_dir(mods, import, pkg_dir, sizeof(pkg_dir));
+ }
+ const cbm_gbuf_node_t **types = NULL;
+ int tn = 0;
+ if (cbm_gbuf_find_by_name(gb, type, &types, &tn) != 0) {
+ return 0;
+ }
+ char props[CBM_SZ_128];
+ snprintf(props, sizeof(props), "{\"via\":\"controller\",\"role\":\"%s\"}", role);
+ int linked = 0;
+ for (int i = 0; i < tn; i++) {
+ const cbm_gbuf_node_t *t = types[i];
+ if (!t->label || !t->file_path ||
+ (strcmp(t->label, "Struct") != 0 && strcmp(t->label, "Class") != 0 &&
+ strcmp(t->label, "Type") != 0)) {
+ continue;
+ }
+ char dir[CBM_SZ_1K];
+ op_dirname(t->file_path, dir, sizeof(dir));
+ bool same = !qual[0] ? strcmp(dir, own_dir) == 0
+ : in_repo ? strcmp(dir, pkg_dir) == 0
+ : op_dir_is_import(dir, import);
+ if (same) {
+ cbm_gbuf_insert_edge(gb, fn->id, t->id, "HANDLES", props);
+ linked++;
+ }
+ }
+ return linked;
+}
+
+/* Every `&[pkg.]Type{` in the arguments of a controller builder call. */
+static int op_controller_calls(cbm_gbuf_t *gb, CBMHashTable *by_file, const op_modules_t *mods,
+ const char *file, const char *text) {
+ /* method name + "(", called on a receiver outside a comment: the previous
+ * code char is '.', and Kind's receiver is the `source` package. */
+ static const struct {
+ const char *name;
+ const char *recv;
+ const char *role;
+ } calls[] = {{"For(", NULL, "for"},
+ {"Owns(", NULL, "owns"},
+ {"Watches(", NULL, "watches"},
+ {"Kind(", "source", "watches"}};
+ int linked = 0;
+ for (size_t c = 0; c < sizeof(calls) / sizeof(calls[0]); c++) {
+ for (const char *p = strstr(text, calls[c].name); p;
+ p = strstr(p + SKIP_ONE, calls[c].name)) {
+ if (p > text && op_ident_char(p[-SKIP_ONE])) {
+ continue; /* part of a longer name */
+ }
+ if (op_comment_start(op_line_start(text, p), p) < p) {
+ continue;
+ }
+ const char *dot = op_prev_code(text, p);
+ if (!dot || *dot != '.') {
+ continue;
+ }
+ if (calls[c].recv) {
+ size_t rl = strlen(calls[c].recv);
+ const char *r = dot - rl;
+ if (r < text || strncmp(r, calls[c].recv, rl) != 0 ||
+ (r > text && op_ident_char(r[-SKIP_ONE]))) {
+ continue;
+ }
+ }
+ const char *open = p + strlen(calls[c].name) - SKIP_ONE;
+ const char *end = op_args_end(open);
+ const cbm_gbuf_node_t *fn = op_enclosing(by_file, file, op_line_of(text, p));
+ if (!fn) {
+ continue;
+ }
+ for (const char *a = open; a < end; a++) {
+ if (*a == '/' && a[SKIP_ONE] == '/') {
+ const char *nl = strchr(a, '\n');
+ a = nl && nl < end ? nl : end;
+ continue;
+ }
+ if (*a != '&') {
+ continue;
+ }
+ const char *q = a + SKIP_ONE;
+ const char *first = q;
+ const char *last = q;
+ while (q < end && (op_ident_char(*q) || *q == '.')) {
+ if (*q == '.') {
+ last = q + SKIP_ONE;
+ }
+ q++;
+ }
+ if (q < end && *q == '{' && q > last && (size_t)(q - last) < OP_IDENT_MAX &&
+ (size_t)(last - first) < OP_IDENT_MAX) {
+ char type[OP_IDENT_MAX];
+ char qual[OP_IDENT_MAX];
+ snprintf(type, sizeof(type), "%.*s", (int)(q - last), last);
+ snprintf(qual, sizeof(qual), "%.*s",
+ last > first ? (int)(last - first - SKIP_ONE) : 0, first);
+ linked += op_handle(gb, mods, fn, file, text, qual, type, calls[c].role);
+ }
+ }
+ }
+ }
+ return linked;
+}
+
+/* ── API groups: +groupName / GroupName ───────────────────────────────── */
+
+/* A Go identifier-or-DNS token at p (the group name). */
+static void op_token(const char *p, char *out, size_t out_sz) {
+ size_t n = 0;
+ while (p[n] && (op_ident_char(p[n]) || p[n] == '.' || p[n] == '-') && n + SKIP_ONE < out_sz) {
+ n++;
+ }
+ snprintf(out, out_sz, "%.*s", (int)n, p);
+}
+
+/* Owned strings. */
+typedef struct {
+ char **items;
+ int count;
+ int cap;
+} op_strs_t;
+
+static void op_strs_push(op_strs_t *v, const char *s) {
+ if (v->count == v->cap) {
+ int cap = v->cap ? v->cap * PAIR_LEN : OP_GROW_MIN;
+ char **grown = cbm_realloc(CBM_MEM_CLASS_OTHER, v->items, (size_t)cap * sizeof(*grown));
+ if (!grown) {
+ return;
+ }
+ v->items = grown;
+ v->cap = cap;
+ }
+ char *copy = cbm_mem_strdup(CBM_MEM_CLASS_OTHER, s);
+ if (copy) {
+ v->items[v->count++] = copy;
+ }
+}
+
+/* group -> the package directories that declare it (owned keys and lists). */
+static void op_note_group(CBMHashTable *groups, const char *group, const char *dir) {
+ if (!group[0]) {
+ return;
+ }
+ op_strs_t *dirs = cbm_ht_get(groups, group);
+ if (!dirs) {
+ dirs = cbm_calloc(CBM_MEM_CLASS_OTHER, sizeof(*dirs));
+ char *key = cbm_mem_strdup(CBM_MEM_CLASS_OTHER, group);
+ if (!dirs || !key) {
+ cbm_free(CBM_MEM_CLASS_OTHER, dirs);
+ cbm_free(CBM_MEM_CLASS_OTHER, key);
+ return;
+ }
+ cbm_ht_set(groups, key, dirs);
+ }
+ for (int i = 0; i < dirs->count; i++) {
+ if (strcmp(dirs->items[i], dir) == 0) {
+ return;
+ }
+ }
+ op_strs_push(dirs, dir);
+}
+
+static void op_scan_groups(CBMHashTable *groups, const char *file, const char *text) {
+ char dir[CBM_SZ_1K];
+ op_dirname(file, dir, sizeof(dir));
+ for (const char *p = strstr(text, "+groupName="); p; p = strstr(p + SKIP_ONE, "+groupName=")) {
+ char g[CBM_SZ_256];
+ op_token(p + OP_SLEN("+groupName="), g, sizeof(g));
+ op_note_group(groups, g, dir);
+ }
+ for (const char *p = strstr(text, "GroupName"); p; p = strstr(p + SKIP_ONE, "GroupName")) {
+ const char *q = p + OP_SLEN("GroupName");
+ while (*q == ' ' || *q == '\t') {
+ q++;
+ }
+ if (*q != '=') {
+ continue;
+ }
+ q++;
+ while (*q == ' ' || *q == '\t') {
+ q++;
+ }
+ if (*q == '"') {
+ char g[CBM_SZ_256];
+ op_token(q + SKIP_ONE, g, sizeof(g));
+ op_note_group(groups, g, dir);
+ }
+ }
+}
+
+static void op_free_groups(const char *key, void *value, void *ud) {
+ (void)ud;
+ op_strs_t *dirs = value;
+ for (int i = 0; i < dirs->count; i++) {
+ cbm_free(CBM_MEM_CLASS_OTHER, dirs->items[i]);
+ }
+ cbm_free(CBM_MEM_CLASS_OTHER, dirs->items);
+ cbm_free(CBM_MEM_CLASS_OTHER, dirs);
+ cbm_free(CBM_MEM_CLASS_OTHER, (void *)key);
+}
+
+/* A JSON string property's value (simple, unescaped scalars). */
+static bool op_prop(const char *props, const char *key, char *out, size_t out_sz) {
+ char pat[CBM_SZ_64];
+ snprintf(pat, sizeof(pat), "\"%s\":\"", key);
+ const char *p = props ? strstr(props, pat) : NULL;
+ if (!p) {
+ return false;
+ }
+ p += strlen(pat);
+ const char *e = strchr(p, '"');
+ if (!e) {
+ return false;
+ }
+ snprintf(out, out_sz, "%.*s", (int)(e - p), p);
+ return true;
+}
+
+/* CRD -> the Go type declared under its group, in a package marking it. */
+static int op_link_crd_types(cbm_gbuf_t *gb, CBMHashTable *groups) {
+ const cbm_gbuf_node_t **res = NULL;
+ int count = 0;
+ int linked = 0;
+ if (cbm_gbuf_find_by_label(gb, "Resource", &res, &count) != 0) {
+ return 0;
+ }
+ for (int i = 0; i < count; i++) {
+ char group[CBM_SZ_256];
+ char kind[CBM_SZ_256];
+ if (!op_prop(res[i]->properties_json, "crd_group", group, sizeof(group)) ||
+ !op_prop(res[i]->properties_json, "crd_kind", kind, sizeof(kind))) {
+ continue;
+ }
+ const op_strs_t *dirs = cbm_ht_get(groups, group);
+ const cbm_gbuf_node_t **types = NULL;
+ int tn = 0;
+ if (!dirs || cbm_gbuf_find_by_name(gb, kind, &types, &tn) != 0) {
+ continue;
+ }
+ for (int t = 0; t < tn; t++) {
+ if (!types[t]->label || strcmp(types[t]->label, "Struct") != 0 ||
+ !types[t]->file_path) {
+ continue;
+ }
+ char dir[CBM_SZ_1K];
+ op_dirname(types[t]->file_path, dir, sizeof(dir));
+ for (int d = 0; d < dirs->count; d++) {
+ if (strcmp(dirs->items[d], dir) == 0) {
+ cbm_gbuf_insert_edge(gb, res[i]->id, types[t]->id, "USAGE",
+ "{\"ref\":\"crd-type\"}");
+ linked++;
+ break;
+ }
+ }
+ }
+ }
+ return linked;
+}
+
+/* ── go:embed ─────────────────────────────────────────────────────────── */
+
+/* Go's path.Match for one segment: '*', '?', '[...]', '\\' escapes. */
+static bool op_match(const char *pat, const char *s) {
+ while (*pat) {
+ if (*pat == '*') {
+ pat++;
+ for (const char *t = s;; t++) {
+ if (op_match(pat, t)) {
+ return true;
+ }
+ if (!*t) {
+ return false;
+ }
+ }
+ }
+ if (!*s) {
+ return false;
+ }
+ if (*pat == '?') {
+ pat++;
+ s++;
+ continue;
+ }
+ if (*pat == '[') {
+ const char *q = pat + SKIP_ONE;
+ bool neg = *q == '^';
+ q += neg;
+ bool hit = false;
+ while (*q && *q != ']') {
+ char lo = *q;
+ char hi = lo;
+ if (q[SKIP_ONE] == '-' && q[PAIR_LEN] && q[PAIR_LEN] != ']') {
+ hi = q[PAIR_LEN];
+ q += PAIR_LEN;
+ }
+ hit = hit || (*s >= lo && *s <= hi);
+ q++;
+ }
+ if (*q != ']' || hit == neg) {
+ return false;
+ }
+ pat = q + SKIP_ONE;
+ s++;
+ continue;
+ }
+ if (*pat == '\\' && pat[SKIP_ONE]) {
+ pat++;
+ }
+ if (*pat != *s) {
+ return false;
+ }
+ pat++;
+ s++;
+ }
+ return *s == '\0';
+}
+
+/* Does `rel` (a path below the Go file's directory) match the embed
+ * pattern: segment by segment, a matched directory taking its files except
+ * hidden ones ('.', '_') unless `all`. */
+static bool op_embeds(const char *pattern, const char *rel, bool all) {
+ char pat[CBM_SZ_1K];
+ char path[CBM_SZ_1K];
+ snprintf(pat, sizeof(pat), "%s", pattern);
+ snprintf(path, sizeof(path), "%s", rel);
+ char *ps = pat;
+ char *rs = path;
+ while (ps && rs) {
+ char *pn = strchr(ps, '/');
+ char *rn = strchr(rs, '/');
+ if (pn) {
+ *pn = '\0';
+ }
+ if (rn) {
+ *rn = '\0';
+ }
+ if (!op_match(ps, rs)) {
+ return false;
+ }
+ ps = pn ? pn + SKIP_ONE : NULL;
+ rs = rn ? rn + SKIP_ONE : NULL;
+ if (!ps) {
+ /* pattern consumed: the file itself, or a file under a matched dir */
+ for (char *seg = rs; seg && !all;) {
+ if (seg[0] == '.' || seg[0] == '_') {
+ return false;
+ }
+ char *next = strchr(seg, '/');
+ seg = next ? next + SKIP_ONE : NULL;
+ }
+ return true;
+ }
+ }
+ return false;
+}
+
+static int op_cmp_path(const void *a, const void *b) {
+ const cbm_gbuf_node_t *x = *(const cbm_gbuf_node_t *const *)a;
+ const cbm_gbuf_node_t *y = *(const cbm_gbuf_node_t *const *)b;
+ return strcmp(x->file_path, y->file_path);
+}
+
+/* REFERENCES_FILE from the Go file to every file its //go:embed lines name;
+ * `files` is every File node sorted by path, so a directory's files are one
+ * range. */
+static int op_embed(cbm_gbuf_t *gb, const cbm_gbuf_node_t *go_file, const char *text,
+ const cbm_gbuf_node_t **files, int nfiles) {
+ char dir[CBM_SZ_1K];
+ op_dirname(go_file->file_path, dir, sizeof(dir));
+ size_t dl = strlen(dir);
+ int linked = 0;
+ for (const char *p = strstr(text, "//go:embed "); p; p = strstr(p + SKIP_ONE, "//go:embed ")) {
+ if (p != text && p[-SKIP_ONE] != '\n') {
+ continue; /* a directive starts its line */
+ }
+ const char *eol = strchr(p, '\n');
+ char line[CBM_SZ_1K];
+ const char *args = p + OP_SLEN("//go:embed ");
+ snprintf(line, sizeof(line), "%.*s", eol ? (int)(eol - args) : (int)strlen(args), args);
+ for (char *tok = line; *tok;) {
+ while (*tok == ' ' || *tok == '\t' || *tok == '\r') {
+ tok++;
+ }
+ char *e = tok;
+ while (*e && *e != ' ' && *e != '\t' && *e != '\r') {
+ e++;
+ }
+ char save = *e;
+ *e = '\0';
+ bool all = strncmp(tok, "all:", OP_SLEN("all:")) == 0;
+ const char *pattern = all ? tok + OP_SLEN("all:") : tok;
+ if (pattern[0] && pattern[0] != '"' && pattern[0] != '`') {
+ /* files below dir: the sorted range starting at "dir/" */
+ int lo = 0;
+ int hi = dl ? nfiles : 0;
+ while (lo < hi) {
+ int mid = lo + (hi - lo) / PAIR_LEN;
+ if (strncmp(files[mid]->file_path, dir, dl) < 0 ||
+ (strncmp(files[mid]->file_path, dir, dl) == 0 &&
+ files[mid]->file_path[dl] < '/')) {
+ lo = mid + SKIP_ONE;
+ } else {
+ hi = mid;
+ }
+ }
+ for (int i = lo; i < nfiles; i++) {
+ const char *fp = files[i]->file_path;
+ if (dl && (strncmp(fp, dir, dl) != 0 || fp[dl] != '/')) {
+ break;
+ }
+ const char *rel = dl ? fp + dl + SKIP_ONE : fp;
+ if (files[i] != go_file && op_embeds(pattern, rel, all)) {
+ cbm_gbuf_insert_edge(gb, go_file->id, files[i]->id, "REFERENCES_FILE",
+ "{\"via\":\"go:embed\"}");
+ linked++;
+ }
+ }
+ }
+ *e = save;
+ tok = e;
+ }
+ }
+ return linked;
+}
+
+/* ── The pass ─────────────────────────────────────────────────────────── */
+
+static bool op_is_go(const char *path) {
+ size_t n = strlen(path);
+ return n > OP_SLEN(".go") && strcmp(path + n - OP_SLEN(".go"), ".go") == 0;
+}
+
+int cbm_pipeline_pass_operator(cbm_pipeline_ctx_t *ctx) {
+ cbm_gbuf_t *gb = ctx->gbuf;
+ const cbm_gbuf_node_t **all = NULL;
+ int count = 0;
+ if (!gb || !ctx->repo_path || cbm_gbuf_find_by_label(gb, "File", &all, &count) != 0) {
+ return 0;
+ }
+ const cbm_gbuf_node_t **files =
+ cbm_calloc(CBM_MEM_CLASS_OTHER, (size_t)(count ? count : SKIP_ONE) * sizeof(*files));
+ if (!files) {
+ return CBM_NOT_FOUND;
+ }
+ int nfiles = 0;
+ for (int i = 0; i < count; i++) {
+ if (all[i]->file_path) {
+ files[nfiles++] = all[i];
+ }
+ }
+ qsort((void *)files, (size_t)nfiles, sizeof(*files), op_cmp_path);
+ CBMHashTable *by_file = op_functions_by_file(gb);
+ CBMHashTable *groups = cbm_ht_create(0);
+ op_modules_t mods = {0};
+ for (int i = 0; i < nfiles; i++) {
+ const char *slash = strrchr(files[i]->file_path, '/');
+ if (strcmp(slash ? slash + SKIP_ONE : files[i]->file_path, "go.mod") == 0) {
+ char path[CBM_SZ_4K];
+ snprintf(path, sizeof(path), "%s/%s", ctx->repo_path, files[i]->file_path);
+ char *text = op_read_file(path);
+ if (text) {
+ op_note_module(&mods, files[i]->file_path, text);
+ cbm_free(CBM_MEM_CLASS_OTHER, text);
+ }
+ }
+ }
+ int handles = 0;
+ int embeds = 0;
+ for (int i = 0; by_file && groups && i < nfiles; i++) {
+ if (!op_is_go(files[i]->file_path) || cbm_pipeline_check_cancel(ctx)) {
+ continue;
+ }
+ char path[CBM_SZ_4K];
+ snprintf(path, sizeof(path), "%s/%s", ctx->repo_path, files[i]->file_path);
+ char *text = op_read_file(path);
+ if (!text) {
+ continue;
+ }
+ if (strstr(text, "controller-runtime") || strstr(text, "source.Kind(")) {
+ handles += op_controller_calls(gb, by_file, &mods, files[i]->file_path, text);
+ }
+ op_scan_groups(groups, files[i]->file_path, text);
+ if (strstr(text, "//go:embed ")) {
+ embeds += op_embed(gb, files[i], text, files, nfiles);
+ }
+ cbm_free(CBM_MEM_CLASS_OTHER, text);
+ }
+ int crd_types = groups ? op_link_crd_types(gb, groups) : 0;
+ op_modules_free(&mods);
+ if (groups) {
+ cbm_ht_foreach(groups, op_free_groups, NULL);
+ cbm_ht_free(groups);
+ }
+ if (by_file) {
+ cbm_ht_foreach(by_file, op_free_spans, NULL);
+ cbm_ht_free(by_file);
+ }
+ cbm_free(CBM_MEM_CLASS_OTHER, (void *)files);
+ if (handles + embeds + crd_types > 0) {
+ char h[CBM_SZ_32];
+ char e[CBM_SZ_32];
+ char c[CBM_SZ_32];
+ snprintf(h, sizeof(h), "%d", handles);
+ snprintf(e, sizeof(e), "%d", embeds);
+ snprintf(c, sizeof(c), "%d", crd_types);
+ cbm_log_info("pass.operator", "handles", h, "embeds", e, "crd_types", c);
+ }
+ return 0;
+}
diff --git a/src/pipeline/pipeline.c b/src/pipeline/pipeline.c
index 9e8a66c8e..fa966538a 100644
--- a/src/pipeline/pipeline.c
+++ b/src/pipeline/pipeline.c
@@ -1837,6 +1837,9 @@ static void predump_sem(cbm_pipeline_ctx_t *ctx) {
static void predump_cfg(cbm_pipeline_ctx_t *ctx) {
cbm_pipeline_pass_configlink(ctx);
}
+static void predump_operator(cbm_pipeline_ctx_t *ctx) {
+ (void)cbm_pipeline_pass_operator(ctx);
+}
static void predump_doclinks(cbm_pipeline_ctx_t *ctx) {
cbm_pipeline_pass_doclinks(ctx);
}
@@ -2077,6 +2080,7 @@ static void run_predump_passes(cbm_pipeline_t *p, cbm_pipeline_ctx_t *ctx) {
} passes[] = {
{predump_deco, "decorator_tags", false},
{predump_cfg, "configlink", false},
+ {predump_operator, "operator", false},
{predump_doclinks, "doclinks", false},
{predump_route, "route_match", false},
{predump_ensemble, "ensemble_routing", false},
diff --git a/src/pipeline/pipeline_incremental.c b/src/pipeline/pipeline_incremental.c
index a11f82356..e9afe2811 100644
--- a/src/pipeline/pipeline_incremental.c
+++ b/src/pipeline/pipeline_incremental.c
@@ -2020,6 +2020,14 @@ static int run_postpasses(cbm_pipeline_ctx_t *ctx, cbm_file_info_t *changed_file
return rc < 0 ? rc : CBM_NOT_FOUND;
}
+ cbm_clock_gettime(CLOCK_MONOTONIC, &t);
+ rc = cbm_pipeline_pass_operator(ctx);
+ cbm_log_info("pass.timing", "pass", "incr_operator", "elapsed_ms",
+ itoa_buf((int)elapsed_ms(t)));
+ if (rc < 0 || cbm_pipeline_check_cancel(ctx)) {
+ return rc < 0 ? rc : CBM_NOT_FOUND;
+ }
+
cbm_clock_gettime(CLOCK_MONOTONIC, &t);
rc = cbm_pipeline_pass_doclinks(ctx);
cbm_log_info("pass.timing", "pass", "incr_doclinks", "elapsed_ms",
diff --git a/src/pipeline/pipeline_internal.h b/src/pipeline/pipeline_internal.h
index 4fe4b549d..19a9d3a08 100644
--- a/src/pipeline/pipeline_internal.h
+++ b/src/pipeline/pipeline_internal.h
@@ -1037,6 +1037,11 @@ int cbm_pipeline_pass_decorator_tags(cbm_gbuf_t *gbuf, const char *project);
/* Pre-dump pass: config ↔ code linking. */
int cbm_pipeline_pass_configlink(cbm_pipeline_ctx_t *ctx);
+/* Go Kubernetes operators (pass_operator.c): controller-runtime controllers
+ * HANDLES the kinds they reconcile, CRDs -> their Go types, //go:embed files.
+ * Runs after USAGE resolution. */
+int cbm_pipeline_pass_operator(cbm_pipeline_ctx_t *ctx);
+
/* Pre-dump pass: markdown → file REFERENCES_FILE linking. */
int cbm_pipeline_pass_doclinks(cbm_pipeline_ctx_t *ctx);
diff --git a/tests/test_grammar_probe_f.c b/tests/test_grammar_probe_f.c
index 391687e23..99b5b6ef5 100644
--- a/tests/test_grammar_probe_f.c
+++ b/tests/test_grammar_probe_f.c
@@ -1084,6 +1084,197 @@ TEST(probe_k8s_reference_prefers_its_own_file) {
PASS();
}
+/* ── Go operator layer (P1.2) ───────────────────────────────────────── */
+
+/* Edges of `type` whose properties carry `prop`, between nodes of
+ * `src_label` and `tgt_label`; -1 when indexing failed. */
+static int gpf_typed_edges(const GpfFile *files, int nfiles, const char *type,
+ const char *src_label, const char *tgt_label, const char *prop) {
+ GpfProj lp;
+ cbm_store_t *store = gpf_index_files(&lp, files, nfiles);
+ int n = -1;
+ if (store) {
+ sqlite3_stmt *st = NULL;
+ const char *sql = "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 e.type=?2 "
+ "AND s.label=?3 AND t.label=?4 AND instr(e.properties,?5)";
+ 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);
+ sqlite3_bind_text(st, 2, type, -1, SQLITE_STATIC);
+ sqlite3_bind_text(st, 3, src_label, -1, SQLITE_STATIC);
+ sqlite3_bind_text(st, 4, tgt_label, -1, SQLITE_STATIC);
+ sqlite3_bind_text(st, 5, prop, -1, SQLITE_STATIC);
+ n = sqlite3_step(st) == SQLITE_ROW ? sqlite3_column_int(st, 0) : -1;
+ }
+ sqlite3_finalize(st);
+ }
+ gpf_cleanup(&lp, store);
+ return n;
+}
+
+static const GpfFile k_operator_repo[] = {
+ {"go.mod", "module example.com/proj\n\ngo 1.22\n"},
+ {"api/v1/doc.go", "// +groupName=example.com\npackage v1\n"},
+ {"api/v1/widget_types.go", "package v1\n\ntype Widget struct {\n\tName string\n}\n\n"
+ "type Gadget struct {\n\tName string\n}\n"},
+ {"config/crd/widgets.yaml",
+ "apiVersion: apiextensions.k8s.io/v1\nkind: CustomResourceDefinition\n"
+ "metadata:\n name: widgets.example.com\nspec:\n group: example.com\n"
+ " names:\n kind: Widget\n plural: widgets\n scope: Namespaced\n"},
+ {"internal/controller/widget.go", "package controller\n\n"
+ "import (\n\texamplev1 \"example.com/proj/api/v1\"\n"
+ "\tctrl \"sigs.k8s.io/controller-runtime\"\n)\n\n"
+ "func SetupWithManager(mgr ctrl.Manager) error {\n"
+ "\treturn ctrl.NewControllerManagedBy(mgr).\n"
+ "\t\tFor(&examplev1.Widget{}).\n"
+ "\t\tOwns(&examplev1.Gadget{}).\n"
+ "\t\tComplete(nil)\n"
+ "}\n"},
+ {"config/embed.go",
+ "package config\n\nimport \"embed\"\n\n//go:embed crd/*\nvar CRDs embed.FS\n"}};
+
+/* A controller reconciles the kind it is built For, and the kinds it Owns. */
+TEST(probe_go_controller_handles_its_kinds) {
+ ASSERT_EQ(
+ gpf_typed_edges(k_operator_repo, 6, "HANDLES", "Function", "Struct", "\"role\":\"for\""),
+ 1);
+ ASSERT_EQ(
+ gpf_typed_edges(k_operator_repo, 6, "HANDLES", "Function", "Struct", "\"role\":\"owns\""),
+ 1);
+ PASS();
+}
+
+/* The CRD of group example.com / kind Widget is backed by `type Widget`
+ * in the package marked +groupName=example.com; a Go file embeds the CRD
+ * files its //go:embed names. */
+TEST(probe_go_crd_type_and_embed) {
+ ASSERT_EQ(
+ gpf_typed_edges(k_operator_repo, 6, "USAGE", "Resource", "Struct", "\"ref\":\"crd-type\""),
+ 1);
+ ASSERT_EQ(gpf_typed_edges(k_operator_repo, 6, "REFERENCES_FILE", "File", "File",
+ "\"via\":\"go:embed\""),
+ 1);
+ PASS();
+}
+
+/* The kind a controller reconciles is the type of the package it imports:
+ * kubermatic has `Cluster` structs and a method `Cluster` elsewhere, and a
+ * name-only lookup handed `For(&kubermaticv1.Cluster{})` to the method. */
+TEST(probe_go_controller_kind_is_the_imported_type) {
+ static const GpfFile files[] = {
+ {"go.mod", "module example.com/proj\n\ngo 1.22\n"},
+ {"api/v1/types.go", "package v1\n\ntype Cluster struct {\n\tName string\n}\n"},
+ {"internal/legacy/types.go", "package legacy\n\ntype Cluster struct {\n\tID int\n}\n"},
+ {"pkg/resources/creds.go",
+ "package resources\n\ntype Creds struct{}\n\nfunc (c Creds) Cluster() string {\n"
+ "\treturn \"\"\n}\n"},
+ {"internal/controller/cluster.go",
+ "package controller\n\n"
+ "import (\n\tkubermaticv1 \"example.com/proj/api/v1\"\n"
+ "\tctrl \"sigs.k8s.io/controller-runtime\"\n)\n\n"
+ "func Add(mgr ctrl.Manager) error {\n"
+ "\treturn ctrl.NewControllerManagedBy(mgr).For(&kubermaticv1.Cluster{}).Complete(nil)\n"
+ "}\n"}};
+ ASSERT_EQ(gpf_typed_edges(files, 5, "HANDLES", "Function", "Struct", "\"role\":\"for\""), 1);
+ ASSERT_EQ(gpf_typed_edges(files, 5, "HANDLES", "Function", "Method", "\"role\":\"for\""), 0);
+ PASS();
+}
+
+/* Edges of `type` into nodes defined in `tgt_file`; -1 when indexing failed. */
+static int gpf_edges_into_file(const GpfFile *files, int nfiles, const char *type,
+ const char *tgt_file) {
+ GpfProj lp;
+ cbm_store_t *store = gpf_index_files(&lp, files, nfiles);
+ int n = -1;
+ if (store) {
+ sqlite3_stmt *st = NULL;
+ const char *sql = "SELECT COUNT(*) FROM edges e JOIN nodes t ON t.id=e.target_id "
+ "WHERE e.project=?1 AND e.type=?2 AND t.file_path=?3";
+ 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);
+ sqlite3_bind_text(st, 2, type, -1, SQLITE_STATIC);
+ sqlite3_bind_text(st, 3, tgt_file, -1, SQLITE_STATIC);
+ n = sqlite3_step(st) == SQLITE_ROW ? sqlite3_column_int(st, 0) : -1;
+ }
+ sqlite3_finalize(st);
+ }
+ gpf_cleanup(&lp, store);
+ return n;
+}
+
+/* An import path names the directory its go.mod roots: kubermatic's
+ * `k8c.io/kubermatic/sdk/v2/apis/...` lives in `sdk/apis/...`, which the
+ * path does not end with, while an unrelated `apis/v1` does. */
+TEST(probe_go_controller_import_follows_go_mod) {
+ static const GpfFile files[] = {
+ {"go.mod", "module example.com/proj\n\ngo 1.22\n"},
+ {"sdk/go.mod", "module example.com/proj/sdk/v2\n\ngo 1.22\n"},
+ {"sdk/apis/v1/types.go", "package v1\n\ntype Cluster struct {\n\tName string\n}\n"},
+ {"apis/v1/types.go", "package v1\n\ntype Cluster struct {\n\tID int\n}\n"},
+ {"pkg/controller/cluster.go",
+ "package controller\n\n"
+ "import (\n\tkubermaticv1 \"example.com/proj/sdk/v2/apis/v1\"\n"
+ "\tctrl \"sigs.k8s.io/controller-runtime\"\n)\n\n"
+ "func Add(mgr ctrl.Manager) error {\n"
+ "\treturn ctrl.NewControllerManagedBy(mgr).For(&kubermaticv1.Cluster{}).Complete(nil)\n"
+ "}\n"}};
+ ASSERT_EQ(gpf_edges_into_file(files, 5, "HANDLES", "sdk/apis/v1/types.go"), 1);
+ ASSERT_EQ(gpf_edges_into_file(files, 5, "HANDLES", "apis/v1/types.go"), 0);
+ PASS();
+}
+
+/* A comment line may sit between a chain's '.' and the call (kubermatic's
+ * seed-sync: `}).` / `// watch all seeds` / `For(&kubermaticv1.Seed{}, ...`),
+ * and a call written inside a comment builds nothing. */
+TEST(probe_go_controller_chain_with_comment_lines) {
+ static const GpfFile files[] = {
+ {"go.mod", "module example.com/proj\n\ngo 1.22\n"},
+ {"api/v1/widget.go", "package v1\n\ntype Widget struct {\n\tName string\n}\n"},
+ {"api/v1/gadget.go", "package v1\n\ntype Gadget struct {\n\tName string\n}\n"},
+ {"internal/controller/widget.go",
+ "package controller\n\n"
+ "import (\n\tv1 \"example.com/proj/api/v1\"\n"
+ "\tctrl \"sigs.k8s.io/controller-runtime\"\n)\n\n"
+ "func Add(mgr ctrl.Manager) error {\n"
+ "\t// gadgets would be b.Owns(&v1.Gadget{}) (not yet)\n"
+ "\treturn ctrl.NewControllerManagedBy(mgr).\n"
+ "\t\tNamed(\"widget\").\n"
+ "\t\t// watch all widgets\n"
+ "\t\tFor(&v1.Widget{}).\n"
+ "\t\tComplete(nil)\n"
+ "}\n"}};
+ ASSERT_EQ(gpf_edges_into_file(files, 4, "HANDLES", "api/v1/widget.go"), 1);
+ ASSERT_EQ(gpf_edges_into_file(files, 4, "HANDLES", "api/v1/gadget.go"), 0);
+ PASS();
+}
+
+/* Go method QNs carry no receiver, so every reconciler's SetupWithManager in
+ * one package is one node: each definition still handles its own kind. */
+TEST(probe_go_controller_methods_sharing_a_node) {
+ static const GpfFile files[] = {
+ {"go.mod", "module example.com/proj\n\ngo 1.22\n"},
+ {"api/v1/types.go", "package v1\n\ntype Widget struct {\n\tName string\n}\n\n"
+ "type Gadget struct {\n\tName string\n}\n"},
+ {"internal/controller/widget.go",
+ "package controller\n\n"
+ "import (\n\tv1 \"example.com/proj/api/v1\"\n"
+ "\tctrl \"sigs.k8s.io/controller-runtime\"\n)\n\n"
+ "type WidgetReconciler struct{}\n\n"
+ "func (r *WidgetReconciler) SetupWithManager(mgr ctrl.Manager) error {\n"
+ "\treturn ctrl.NewControllerManagedBy(mgr).For(&v1.Widget{}).Complete(r)\n"
+ "}\n"},
+ {"internal/controller/gadget.go",
+ "package controller\n\n"
+ "import (\n\tv1 \"example.com/proj/api/v1\"\n"
+ "\tctrl \"sigs.k8s.io/controller-runtime\"\n)\n\n"
+ "type GadgetReconciler struct{}\n\n"
+ "func (r *GadgetReconciler) SetupWithManager(mgr ctrl.Manager) error {\n"
+ "\treturn ctrl.NewControllerManagedBy(mgr).For(&v1.Gadget{}).Complete(r)\n"
+ "}\n"}};
+ ASSERT_EQ(gpf_edges_into_file(files, 4, "HANDLES", "api/v1/types.go"), 2);
+ PASS();
+}
+
/* A CRD vendored twice (gardener: example/seed-crds and a chart): a custom
* resource is an instance of the copy deployed with it; only when no copy
* shares its bundle does it stand for all of them. */
@@ -2682,6 +2873,12 @@ SUITE(grammar_probe_f) {
RUN_TEST(probe_k8s_reference_stays_in_bundle);
RUN_TEST(probe_k8s_reference_prefers_its_own_file);
RUN_TEST(probe_k8s_cr_prefers_the_crd_in_its_bundle);
+ RUN_TEST(probe_go_controller_handles_its_kinds);
+ RUN_TEST(probe_go_crd_type_and_embed);
+ RUN_TEST(probe_go_controller_kind_is_the_imported_type);
+ RUN_TEST(probe_go_controller_import_follows_go_mod);
+ RUN_TEST(probe_go_controller_chain_with_comment_lines);
+ RUN_TEST(probe_go_controller_methods_sharing_a_node);
RUN_TEST(probe_k8s_selector_stays_in_its_directory);
RUN_TEST(probe_k8s_selector_within_kustomize_build);
RUN_TEST(probe_k8s_kustomize_namespace_is_effective);