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): Flux, Argo, Cluster API, cert-manager and KKP references
Martin Vogel191b581de663
diff --git a/internal/cbm/cbm.h b/internal/cbm/cbm.hindex 0ae684030..6be7102d5 100644--- a/internal/cbm/cbm.h+++ b/internal/cbm/cbm.h@@ -603,10 +603,13 @@ typedef struct {/* A fact a Kubernetes manifest states about other objects (extract_k8s.c),* resolved by the K8s pass against the objects of the source's bundle.* CBM_K8S_FACT_REF: object def_index names a kind/ns/name object at a- * reference path (via), drawn as edge (USAGE or INFRA_MAPS); ns NULL =- * the source's own namespace.- * CBM_K8S_FACT_CRD: the CRD def_index declares kind `kind` in group `name`. */-typedef enum { CBM_K8S_FACT_REF = 0, CBM_K8S_FACT_CRD } CBMK8sFactKind;+ * reference path (via), drawn as edge (USAGE, INFRA_MAPS or DEPENDS_ON);+ * ns NULL = the source's own namespace.+ * CBM_K8S_FACT_CRD: the CRD def_index declares kind `kind` in group `name`.+ * CBM_K8S_FACT_DEPLOY: object def_index (a Flux Kustomization, an Argo+ * Application) deploys the repository directory `name` into namespace+ * `ns` (NULL: the objects' own); via "flux" / "argo". */+typedef enum { CBM_K8S_FACT_REF = 0, CBM_K8S_FACT_CRD, CBM_K8S_FACT_DEPLOY } CBMK8sFactKind;typedef struct {CBMK8sFactKind fact;int def_index;diff --git a/internal/cbm/extract_k8s.c b/internal/cbm/extract_k8s.cindex 29765d87c..6d5df5565 100644--- a/internal/cbm/extract_k8s.c+++ b/internal/cbm/extract_k8s.c@@ -407,6 +407,220 @@ static const k8s_mapping_ref_t k8s_mapping_refs[] = {"parentRef", "USAGE"},};+// The curated second table: the CRDs of Flux, Argo, Cluster API,+// cert-manager and Kubermatic, written from their API types. Source kinds+// are qualified by API group ("|g|" list), and a path may use `*`, which+// matches within one segment (`spec.cloud.*.credentialsReference`).+typedef struct {+ const char *groups;+ const char *kinds;+ const char *path;+ const char *kind;+ const char *via;+ const char *edge;+} k8s_crd_leaf_ref_t;++typedef struct {+ const char *groups;+ const char *kinds;+ const char *path;+ const char *kind; // the target kind; NULL = the mapping's own `kind` field+ const char *allowed; // with kind NULL: "|A|" of the kinds that are objects (NULL: any)+ const char *dflt; // with kind NULL: the kind when the field is absent+ const char *via;+ const char *edge;+} k8s_crd_mapping_ref_t;++#define K8S_FLUX_KS "|kustomize.toolkit.fluxcd.io|"+#define K8S_FLUX_HELM "|helm.toolkit.fluxcd.io|"+#define K8S_FLUX_SRC "|source.toolkit.fluxcd.io|"+#define K8S_FLUX_NOTE "|notification.toolkit.fluxcd.io|"+#define K8S_FLUX_IMAGE "|image.toolkit.fluxcd.io|"+#define K8S_ARGO "|argoproj.io|"+#define K8S_CAPI "|cluster.x-k8s.io|"+#define K8S_CAPI_BOOT "|bootstrap.cluster.x-k8s.io|"+#define K8S_CAPI_CP "|controlplane.cluster.x-k8s.io|"+#define K8S_CAPI_ADDONS "|addons.cluster.x-k8s.io|"+#define K8S_CM "|cert-manager.io|"+#define K8S_KKP "|kubermatic.k8c.io|"+#define K8S_KKP_APPS "|apps.kubermatic.k8c.io|"+#define K8S_FLUX_SOURCES "|GitRepository|OCIRepository|HelmRepository|Bucket|"+#define K8S_ISSUERS "|Issuer|ClusterIssuer|"++static const k8s_crd_leaf_ref_t k8s_crd_leaf_refs[] = {+ {K8S_FLUX_KS, "|Kustomization|", "spec.decryption.secretRef.name", "Secret", "secretRef",+ "USAGE"},+ {K8S_FLUX_KS "helm.toolkit.fluxcd.io|", "|Kustomization|HelmRelease|",+ "spec.kubeConfig.secretRef.name", "Secret", "secretRef", "USAGE"},+ {K8S_FLUX_KS "helm.toolkit.fluxcd.io|", "|Kustomization|HelmRelease|",+ "spec.serviceAccountName", "ServiceAccount", "serviceAccountName", "USAGE"},+ {K8S_FLUX_SRC, K8S_FLUX_SOURCES, "spec.secretRef.name", "Secret", "secretRef", "USAGE"},+ {K8S_FLUX_SRC, K8S_FLUX_SOURCES, "spec.certSecretRef.name", "Secret", "secretRef", "USAGE"},+ {K8S_FLUX_SRC, K8S_FLUX_SOURCES, "spec.proxySecretRef.name", "Secret", "secretRef", "USAGE"},+ {K8S_FLUX_SRC, "|GitRepository|OCIRepository|", "spec.verify.secretRef.name", "Secret",+ "secretRef", "USAGE"},+ {K8S_FLUX_SRC, "|OCIRepository|HelmRepository|", "spec.serviceAccountName", "ServiceAccount",+ "serviceAccountName", "USAGE"},+ {K8S_FLUX_NOTE, "|Alert|", "spec.providerRef.name", "Provider", "providerRef", "USAGE"},+ {K8S_FLUX_NOTE, "|Provider|Receiver|", "spec.secretRef.name", "Secret", "secretRef", "USAGE"},+ {K8S_FLUX_NOTE, "|Provider|", "spec.certSecretRef.name", "Secret", "secretRef", "USAGE"},+ {K8S_FLUX_IMAGE, "|ImagePolicy|", "spec.imageRepositoryRef.name", "ImageRepository",+ "imageRepositoryRef", "USAGE"},+ {K8S_FLUX_IMAGE, "|ImageRepository|", "spec.secretRef.name", "Secret", "secretRef", "USAGE"},+ {K8S_FLUX_IMAGE, "|ImageRepository|", "spec.certSecretRef.name", "Secret", "secretRef",+ "USAGE"},+ {K8S_FLUX_IMAGE, "|ImageRepository|", "spec.serviceAccountName", "ServiceAccount",+ "serviceAccountName", "USAGE"},+ {K8S_ARGO, "|Application|", "spec.project", "AppProject", "project", "USAGE"},+ {K8S_ARGO, "|ApplicationSet|", "spec.template.spec.project", "AppProject", "project", "USAGE"},+ {K8S_ARGO, "|Rollout|", "spec.strategy.canary.canaryService", "Service", "rolloutService",+ "USAGE"},+ {K8S_ARGO, "|Rollout|", "spec.strategy.canary.stableService", "Service", "rolloutService",+ "USAGE"},+ {K8S_ARGO, "|Rollout|", "spec.strategy.blueGreen.activeService", "Service", "rolloutService",+ "USAGE"},+ {K8S_ARGO, "|Rollout|", "spec.strategy.blueGreen.previewService", "Service", "rolloutService",+ "USAGE"},+ {K8S_ARGO, "|Rollout|", "spec.strategy.canary.analysis.templates[].templateName",+ "AnalysisTemplate", "analysis", "USAGE"},+ {K8S_ARGO, "|Rollout|", "spec.strategy.canary.steps[].analysis.templates[].templateName",+ "AnalysisTemplate", "analysis", "USAGE"},+ {K8S_ARGO, "|Workflow|", "spec.workflowTemplateRef.name", "WorkflowTemplate",+ "workflowTemplateRef", "USAGE"},+ {K8S_ARGO, "|CronWorkflow|", "spec.workflowSpec.workflowTemplateRef.name", "WorkflowTemplate",+ "workflowTemplateRef", "USAGE"},+ {K8S_ARGO, "|Workflow|WorkflowTemplate|", "spec.templates[].steps[][].templateRef.name",+ "WorkflowTemplate", "templateRef", "USAGE"},+ {K8S_ARGO, "|Workflow|WorkflowTemplate|", "spec.templates[].dag.tasks[].templateRef.name",+ "WorkflowTemplate", "templateRef", "USAGE"},+ {K8S_CAPI, "|Cluster|", "spec.topology.class", "ClusterClass", "topologyClass", "USAGE"},+ {K8S_CAPI, "|Machine|MachineDeployment|MachineSet|MachinePool|MachineHealthCheck|",+ "spec.clusterName", "Cluster", "clusterName", "USAGE"},+ {K8S_CAPI, "|Machine|", "spec.bootstrap.dataSecretName", "Secret", "dataSecretName", "USAGE"},+ {K8S_CAPI, "|MachineDeployment|MachineSet|MachinePool|",+ "spec.template.spec.bootstrap.dataSecretName", "Secret", "dataSecretName", "USAGE"},+ {K8S_CAPI_BOOT, "|KubeadmConfig|", "spec.files[].contentFrom.secret.name", "Secret", "files",+ "USAGE"},+ {K8S_CAPI_BOOT, "|KubeadmConfigTemplate|", "spec.template.spec.files[].contentFrom.secret.name",+ "Secret", "files", "USAGE"},+ {K8S_CAPI_CP, "|KubeadmControlPlane|", "spec.kubeadmConfigSpec.files[].contentFrom.secret.name",+ "Secret", "files", "USAGE"},+ {K8S_CAPI_CP, "|KubeadmControlPlaneTemplate|",+ "spec.template.spec.kubeadmConfigSpec.files[].contentFrom.secret.name", "Secret", "files",+ "USAGE"},+ {K8S_CM, "|Certificate|", "spec.secretName", "Secret", "secretName", "USAGE"},+ {K8S_CM, K8S_ISSUERS, "spec.ca.secretName", "Secret", "secretRef", "USAGE"},+ {K8S_CM, K8S_ISSUERS, "spec.acme.privateKeySecretRef.name", "Secret", "secretRef", "USAGE"},+ {K8S_CM, K8S_ISSUERS, "spec.acme.externalAccountBinding.keySecretRef.name", "Secret",+ "secretRef", "USAGE"},+ {K8S_CM, K8S_ISSUERS, "spec.acme.solvers[].dns01.*.*SecretRef.name", "Secret", "secretRef",+ "USAGE"},+ {K8S_CM, K8S_ISSUERS, "spec.vault.auth.tokenSecretRef.name", "Secret", "secretRef", "USAGE"},+ {K8S_CM, K8S_ISSUERS, "spec.vault.auth.*.secretRef.name", "Secret", "secretRef", "USAGE"},+ {K8S_CM, K8S_ISSUERS, "spec.vault.caBundleSecretRef.name", "Secret", "secretRef", "USAGE"},+ {K8S_CM, K8S_ISSUERS, "spec.venafi.*.*Ref.name", "Secret", "secretRef", "USAGE"},+ {"|networking.k8s.io|extensions|gateway.networking.k8s.io|", "|Ingress|Gateway|",+ "metadata.annotations.cert-manager.io/cluster-issuer", "ClusterIssuer", "issuer", "USAGE"},+ {"|networking.k8s.io|extensions|gateway.networking.k8s.io|", "|Ingress|Gateway|",+ "metadata.annotations.cert-manager.io/issuer", "Issuer", "issuer", "USAGE"},+ {K8S_KKP_APPS, "|ApplicationInstallation|", "spec.applicationRef.name", "ApplicationDefinition",+ "applicationRef", "USAGE"},+ {K8S_KKP_APPS, "|ApplicationDefinition|",+ "spec.versions[].template.source.*.credentials.*.name", "Secret", "credentials", "USAGE"},+};++static const k8s_crd_mapping_ref_t k8s_crd_mapping_refs[] = {+ {K8S_FLUX_KS, "|Kustomization|", "spec.sourceRef", NULL, NULL, NULL, "sourceRef", "USAGE"},+ {K8S_FLUX_KS, "|Kustomization|", "spec.dependsOn[]", "Kustomization", NULL, NULL, "dependsOn",+ "DEPENDS_ON"},+ {K8S_FLUX_KS, "|Kustomization|", "spec.postBuild.substituteFrom[]", NULL, "|ConfigMap|Secret|",+ NULL, "substituteFrom", "USAGE"},+ {K8S_FLUX_KS, "|Kustomization|", "spec.healthChecks[]", NULL, NULL, NULL, "healthCheck",+ "USAGE"},+ {K8S_FLUX_HELM, "|HelmRelease|", "spec.chart.spec.sourceRef", NULL, NULL, NULL, "sourceRef",+ "USAGE"},+ {K8S_FLUX_HELM, "|HelmRelease|", "spec.chartRef", NULL, NULL, NULL, "chartRef", "USAGE"},+ {K8S_FLUX_HELM, "|HelmRelease|", "spec.dependsOn[]", "HelmRelease", NULL, NULL, "dependsOn",+ "DEPENDS_ON"},+ {K8S_FLUX_HELM, "|HelmRelease|", "spec.valuesFrom[]", NULL, "|ConfigMap|Secret|", NULL,+ "valuesFrom", "USAGE"},+ {K8S_FLUX_SRC, "|HelmChart|", "spec.sourceRef", NULL, NULL, NULL, "sourceRef", "USAGE"},+ {K8S_FLUX_SRC, "|GitRepository|", "spec.include[].repository", "GitRepository", NULL, NULL,+ "include", "USAGE"},+ {K8S_FLUX_NOTE, "|Alert|", "spec.eventSources[]", NULL, NULL, NULL, "eventSource", "USAGE"},+ {K8S_FLUX_NOTE, "|Receiver|", "spec.resources[]", NULL, NULL, NULL, "resource", "USAGE"},+ {K8S_FLUX_IMAGE, "|ImageUpdateAutomation|", "spec.sourceRef", NULL, NULL, "GitRepository",+ "sourceRef", "USAGE"},+ {K8S_ARGO, "|Rollout|", "spec.workloadRef", NULL, NULL, NULL, "workloadRef", "USAGE"},+ {K8S_CAPI, "|Cluster|", "spec.infrastructureRef", NULL, NULL, NULL, "infrastructureRef",+ "USAGE"},+ {K8S_CAPI, "|Cluster|", "spec.controlPlaneRef", NULL, NULL, NULL, "controlPlaneRef", "USAGE"},+ {K8S_CAPI, "|Cluster|", "spec.topology.classRef", "ClusterClass", NULL, NULL, "topologyClass",+ "USAGE"},+ {K8S_CAPI, "|Machine|", "spec.bootstrap.configRef", NULL, NULL, NULL, "bootstrap", "USAGE"},+ {K8S_CAPI, "|Machine|", "spec.infrastructureRef", NULL, NULL, NULL, "infrastructureRef",+ "USAGE"},+ {K8S_CAPI, "|MachineDeployment|MachineSet|MachinePool|",+ "spec.template.spec.bootstrap.configRef", NULL, NULL, NULL, "bootstrap", "USAGE"},+ {K8S_CAPI, "|MachineDeployment|MachineSet|MachinePool|", "spec.template.spec.infrastructureRef",+ NULL, NULL, NULL, "infrastructureRef", "USAGE"},+ {K8S_CAPI_CP, "|KubeadmControlPlane|", "spec.machineTemplate.infrastructureRef", NULL, NULL,+ NULL, "infrastructureRef", "USAGE"},+ {K8S_CAPI_CP, "|KubeadmControlPlane|", "spec.machineTemplate.spec.infrastructureRef", NULL,+ NULL, NULL, "infrastructureRef", "USAGE"},+ {K8S_CAPI, "|ClusterClass|", "spec.infrastructure.ref", NULL, NULL, NULL, "template", "USAGE"},+ {K8S_CAPI, "|ClusterClass|", "spec.infrastructure.templateRef", NULL, NULL, NULL, "template",+ "USAGE"},+ {K8S_CAPI, "|ClusterClass|", "spec.controlPlane.ref", NULL, NULL, NULL, "template", "USAGE"},+ {K8S_CAPI, "|ClusterClass|", "spec.controlPlane.templateRef", NULL, NULL, NULL, "template",+ "USAGE"},+ {K8S_CAPI, "|ClusterClass|", "spec.controlPlane.machineInfrastructure.ref", NULL, NULL, NULL,+ "template", "USAGE"},+ {K8S_CAPI, "|ClusterClass|", "spec.controlPlane.machineInfrastructure.templateRef", NULL, NULL,+ NULL, "template", "USAGE"},+ {K8S_CAPI, "|ClusterClass|", "spec.workers.*[].template.*.ref", NULL, NULL, NULL, "template",+ "USAGE"},+ {K8S_CAPI, "|ClusterClass|", "spec.workers.*[].*.templateRef", NULL, NULL, NULL, "template",+ "USAGE"},+ {K8S_CAPI_ADDONS, "|ClusterResourceSet|", "spec.resources[]", NULL, "|ConfigMap|Secret|", NULL,+ "resources", "USAGE"},+ {K8S_CM, "|Certificate|CertificateRequest|", "spec.issuerRef", NULL, NULL, "Issuer",+ "issuerRef", "USAGE"},+ {K8S_KKP, "|KubermaticConfiguration|", "spec.ingress.certificateIssuer", NULL, NULL, NULL,+ "certificateIssuer", "USAGE"},+ {K8S_KKP, "|Seed|", "spec.kubeconfig", "Secret", NULL, NULL, "kubeconfig", "USAGE"},+ {K8S_KKP, "|Seed|", "spec.etcdBackupRestore.destinations.*.credentials", "Secret", NULL, NULL,+ "credentials", "USAGE"},+ {K8S_KKP, "|Cluster|", "spec.cloud.*.credentialsReference", "Secret", NULL, NULL, "credentials",+ "USAGE"},+ {K8S_KKP, "|Addon|EtcdBackupConfig|EtcdRestore|", "spec.cluster", "Cluster", NULL, NULL,+ "cluster", "USAGE"},+};++// Does `path` match `pat`, where `*` matches any run within one segment?+static bool k8s_path_match(const char *pat, const char *path) {+ const char *star = NULL;+ const char *mark = NULL;+ while (*path) {+ if (*pat == '*') {+ star = pat++;+ mark = path;+ } else if (*pat && *pat == *path) {+ pat++;+ path++;+ } else if (star && *mark != '.') {+ pat = star + SKIP_ONE;+ path = ++mark;+ } else {+ return false;+ }+ }+ while (*pat == '*') {+ pat++;+ }+ return *pat == '\0';+}+// Is `kind` one of the "|A|B|" list?static bool k8s_kind_in(const char *list, const char *kind) {if (!list || !kind[0]) {@@ -429,8 +643,9 @@ static const char *k8s_pod_prefix(const char *kind) {if (strcmp(kind, "CronJob") == 0) {return "spec.jobTemplate.spec.template.spec";}- if (k8s_kind_in("|Deployment|StatefulSet|DaemonSet|ReplicaSet|ReplicationController|Job|",- kind)) {+ if (k8s_kind_in(+ "|Deployment|StatefulSet|DaemonSet|ReplicaSet|ReplicationController|Job|Rollout|",+ kind)) {return "spec.template.spec";}return "";@@ -438,12 +653,42 @@ static const char *k8s_pod_prefix(const char *kind) {typedef struct {CBMExtractCtx *ctx;- const char *kind; // the document's kind+ const char *group; // the document's API group+ const char *kind; // the document's kindint def_index;char crd_group[K8S_BUF_SIZE];char crd_kind[K8S_BUF_SIZE];+ const char *deploy_ns; // a deploying object's target namespace} k8s_walk_t;+// Is the document of a source kind in group list `groups`?+static bool k8s_source_is(const k8s_walk_t *w, const char *groups, const char *kinds) {+ return k8s_kind_in(groups, w->group) && k8s_kind_in(kinds, w->kind);+}++// A Flux Kustomization / Argo Application deploys a repository directory.+static void k8s_deploy_leaf(k8s_walk_t *w, const char *path, const char *value) {+ bool flux = k8s_source_is(w, K8S_FLUX_KS, "|Kustomization|");+ bool argo = k8s_source_is(w, K8S_ARGO, "|Application|");+ if ((flux && strcmp(path, "spec.targetNamespace") == 0) ||+ (argo && strcmp(path, "spec.destination.namespace") == 0)) {+ w->deploy_ns = strstr(value, "{{") ? NULL : value;+ return;+ }+ if ((flux && strcmp(path, "spec.path") == 0) ||+ (argo &&+ (strcmp(path, "spec.source.path") == 0 || strcmp(path, "spec.sources[].path") == 0))) {+ if (strstr(value, "{{")) {+ return;+ }+ CBMK8sFact f = {.fact = CBM_K8S_FACT_DEPLOY,+ .def_index = w->def_index,+ .name = cbm_arena_strdup(w->ctx->arena, value),+ .via = flux ? "flux" : "argo"};+ cbm_k8sfact_push(&w->ctx->result->k8s_facts, w->ctx->arena, f);+ }+}+static void k8s_emit_ref(k8s_walk_t *w, const char *kind, const char *ns, const char *name,const char *via, const char *edge) {if (!kind || !kind[0] || !name || !name[0] || strstr(name, "{{") || strstr(kind, "{{")) {@@ -493,6 +738,13 @@ static void k8s_leaf(k8s_walk_t *w, const char *path, const char *value) {k8s_emit_ref(w, r->kind, NULL, value, r->via, r->edge);}}+ for (size_t i = 0; i < sizeof(k8s_crd_leaf_refs) / sizeof(k8s_crd_leaf_refs[0]); i++) {+ const k8s_crd_leaf_ref_t *r = &k8s_crd_leaf_refs[i];+ if (k8s_source_is(w, r->groups, r->kinds) && k8s_path_match(r->path, path)) {+ k8s_emit_ref(w, r->kind, NULL, value, r->via, r->edge);+ }+ }+ k8s_deploy_leaf(w, path, value);if (strcmp(w->kind, "CustomResourceDefinition") == 0) {if (strcmp(path, "spec.group") == 0) {snprintf(w->crd_group, sizeof(w->crd_group), "%s", value);@@ -546,23 +798,35 @@ static const char *k8s_field(k8s_walk_t *w, TSNode mapping, const char *field) {return NULL;}+// The reference a mapping holds in its kind/name/namespace: to `kind`, or+// (NULL) to its own `kind` field, one of `allowed` (NULL: any), `dflt`+// when absent.+static void k8s_mapping_ref(k8s_walk_t *w, TSNode mapping, const char *kind, const char *allowed,+ const char *dflt, const char *via, const char *edge) {+ if (!kind) {+ kind = k8s_field(w, mapping, "kind");+ kind = kind ? kind : dflt;+ if (!kind || (allowed && !k8s_kind_in(allowed, kind))) {+ return;+ }+ }+ k8s_emit_ref(w, kind, k8s_field(w, mapping, "namespace"), k8s_field(w, mapping, "name"), via,+ edge);+}+// 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) {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) {- continue;+ if (k8s_kind_in(r->kinds, w->kind) && strcmp(path, r->path) == 0) {+ k8s_mapping_ref(w, mapping, r->kind, r->allowed, r->dflt, r->via, r->edge);}- const char *kind = r->kind;- if (!kind) {- kind = k8s_field(w, mapping, "kind");- kind = kind ? kind : r->dflt;- if (!kind || (r->allowed && !k8s_kind_in(r->allowed, kind))) {- continue;- }+ }+ for (size_t i = 0; i < sizeof(k8s_crd_mapping_refs) / sizeof(k8s_crd_mapping_refs[0]); i++) {+ const k8s_crd_mapping_ref_t *r = &k8s_crd_mapping_refs[i];+ if (k8s_source_is(w, r->groups, r->kinds) && k8s_path_match(r->path, path)) {+ k8s_mapping_ref(w, mapping, r->kind, r->allowed, r->dflt, r->via, r->edge);}- k8s_emit_ref(w, kind, k8s_field(w, mapping, "namespace"), k8s_field(w, mapping, "name"),- r->via, r->edge);}}@@ -623,11 +887,20 @@ static void k8s_walk(k8s_walk_t *w, TSNode node, char *path, size_t len, size_t}}-// The references and CRD declaration of document def `def_index`.-static void k8s_emit_facts(CBMExtractCtx *ctx, TSNode mapping, const char *kind, int def_index) {- k8s_walk_t w = {.ctx = ctx, .kind = kind, .def_index = def_index};+// The references, deploys and CRD declaration of document def `def_index`.+static void k8s_emit_facts(CBMExtractCtx *ctx, TSNode mapping, const char *group, const char *kind,+ int def_index) {+ k8s_walk_t w = {.ctx = ctx, .group = group, .kind = kind, .def_index = def_index};char path[CBM_SZ_512] = "";+ int first = ctx->result->k8s_facts.count;k8s_walk(&w, mapping, path, 0, sizeof(path));+ /* the target namespace may follow the path in the document */+ for (int i = first; w.deploy_ns && i < ctx->result->k8s_facts.count; i++) {+ CBMK8sFact *f = &ctx->result->k8s_facts.items[i];+ if (f->fact == CBM_K8S_FACT_DEPLOY) {+ f->ns = cbm_arena_strdup(ctx->arena, w.deploy_ns);+ }+ }if (w.crd_group[0] && w.crd_kind[0]) {CBMK8sFact f = {.fact = CBM_K8S_FACT_CRD,.def_index = def_index,@@ -726,7 +999,7 @@ static void extract_k8s_manifest(CBMExtractCtx *ctx) {def.start_line = ts_node_start_point(mapping).row + TS_LINE_OFFSET;def.end_line = ts_node_end_point(mapping).row + TS_LINE_OFFSET;cbm_defs_push(&ctx->result->defs, a, def);- k8s_emit_facts(ctx, mapping, id.kind, ctx->result->defs.count - SKIP_ONE);+ k8s_emit_facts(ctx, mapping, group, id.kind, ctx->result->defs.count - SKIP_ONE);}}diff --git a/src/pipeline/pass_k8s.c b/src/pipeline/pass_k8s.cindex bbc465be9..674eabdd4 100644--- a/src/pipeline/pass_k8s.c+++ b/src/pipeline/pass_k8s.c@@ -676,6 +676,7 @@ typedef struct {CBMHashTable *names; /* bundle name -> itself (owned key) */CBMHashTable *members; /* manifest path -> k8s_members_t* */CBMHashTable *charts; /* Helm chart directory -> itself (owned key) */+ CBMHashTable *deploys; /* manifest path -> k8s_members_t* of its deploy scopes */} k8s_bundles_t;static bool k8s_grow(void **items, int *cap, int count, size_t elem) {@@ -818,14 +819,15 @@ static const char *k8s_bundle_name(k8s_bundles_t *b, const char *kind, const chareturn owned;}-static void k8s_member_add(k8s_bundles_t *b, const char *file, const char *bundle, const char *ns) {- k8s_members_t *m = cbm_ht_get(b->members, file);+static void k8s_member_add(CBMHashTable *table, const char *file, const char *bundle,+ const char *ns) {+ k8s_members_t *m = cbm_ht_get(table, file);if (!m) {m = cbm_calloc(CBM_MEM_CLASS_OTHER, sizeof(*m));if (!m) {return;}- cbm_ht_set(b->members, file, m);+ cbm_ht_set(table, file, m);}for (int i = 0; i < m->count; i++) {if (m->items[i].bundle == bundle) {@@ -867,7 +869,7 @@ static void k8s_bundles_from_kusts(k8s_bundles_t *b, k8s_kusts_t *ks) {seen[f.kust] = r + SKIP_ONE;const k8s_kust_t *k = &ks->items[f.kust];for (int i = 0; i < k->file_n; i++) {- k8s_member_add(b, k->files[i], bundle, f.ns);+ k8s_member_add(b->members, k->files[i], bundle, f.ns);}for (int i = k->kid_n - SKIP_ONE; i >= 0; i--) {if (k8s_grow((void **)&stack, &cap, depth, sizeof(*stack))) {@@ -938,6 +940,10 @@ static void k8s_bundles_free(k8s_bundles_t *b) {cbm_ht_foreach(b->members, k8s_free_members, NULL);cbm_ht_free(b->members);}+ if (b->deploys) {+ cbm_ht_foreach(b->deploys, k8s_free_members, NULL);+ cbm_ht_free(b->deploys);+ }if (b->names) {cbm_ht_foreach(b->names, k8s_free_owned_key, NULL);cbm_ht_free(b->names);@@ -1078,7 +1084,7 @@ typedef struct {char *ns; /* explicit target namespace, NULL = the source's */char *name; /* target metadata.name */const char *via; /* static: the extractor's table */- const char *edge; /* static: "USAGE" or "INFRA_MAPS" */+ const char *edge; /* static: "USAGE", "INFRA_MAPS" or "DEPENDS_ON" */} k8s_ref_t;/* A CRD and the group/kind it declares. */@@ -1088,6 +1094,14 @@ typedef struct {char *kind;} k8s_crd_t;+/* An object that deploys a repository directory (Flux Kustomization, Argo+ * Application). */+typedef struct {+ int64_t node;+ char *dir; /* normalised, "" = the repository root */+ char *ns; /* the namespace it deploys into, NULL = the objects' own */+} k8s_deploy_t;+typedef struct {k8s_ref_t *refs;int ref_n;@@ -1095,6 +1109,9 @@ typedef struct {k8s_crd_t *crds;int crd_n;int crd_cap;+ k8s_deploy_t *deploys;+ int dep_n;+ int dep_cap;} k8s_facts_t;static void k8s_facts_free(k8s_facts_t *f) {@@ -1107,11 +1124,60 @@ static void k8s_facts_free(k8s_facts_t *f) {cbm_free(CBM_MEM_CLASS_OTHER, f->crds[i].group);cbm_free(CBM_MEM_CLASS_OTHER, f->crds[i].kind);}+ for (int i = 0; i < f->dep_n; i++) {+ cbm_free(CBM_MEM_CLASS_OTHER, f->deploys[i].dir);+ cbm_free(CBM_MEM_CLASS_OTHER, f->deploys[i].ns);+ }cbm_free(CBM_MEM_CLASS_OTHER, f->refs);cbm_free(CBM_MEM_CLASS_OTHER, f->crds);+ cbm_free(CBM_MEM_CLASS_OTHER, f->deploys);*f = (k8s_facts_t){0};}+/* A deploy path as a repository directory: "./a//b/" -> "a/b", "." -> "";+ * false for URLs, templates, variables and paths leaving the repository. */+static bool k8s_norm_dir(const char *in, char *out, size_t out_sz) {+ if (!in || strstr(in, "://") || strstr(in, "{{") || strchr(in, '$') || out_sz == 0) {+ return false;+ }+ size_t n = 0;+ out[0] = '\0';+ for (const char *p = in; *p;) {+ while (*p == '/') {+ p++;+ }+ size_t sl = strcspn(p, "/");+ if (sl == 0) {+ break;+ }+ if (sl == SKIP_ONE && p[0] == '.') {+ p += sl;+ continue;+ }+ if (sl == PAIR_LEN && p[0] == '.' && p[SKIP_ONE] == '.') {+ if (n == 0) {+ return false;+ }+ char *slash = strrchr(out, '/');+ n = slash ? (size_t)(slash - out) : 0;+ out[n] = '\0';+ p += sl;+ continue;+ }+ if (n + SKIP_ONE + sl >= out_sz) {+ return false;+ }+ if (n) {+ out[n++] = '/';+ }+ memcpy(out + n, p, sl);+ n += sl;+ out[n] = '\0';+ p += sl;+ }+ return true;+}+/* Keep a file's facts, its def indices mapped to the Resource nodes. */static void k8s_facts_keep(k8s_facts_t *f, const CBMFileResult *res, const int64_t *node_of) {for (int i = 0; i < res->k8s_facts.count; i++) {@@ -1120,7 +1186,15 @@ static void k8s_facts_keep(k8s_facts_t *f, const CBMFileResult *res, const int64continue;}int64_t node = node_of[x->def_index];- if (x->fact == CBM_K8S_FACT_CRD) {+ if (x->fact == CBM_K8S_FACT_DEPLOY) {+ char dir[CBM_SZ_1K];+ if (k8s_norm_dir(x->name, dir, sizeof(dir)) &&+ k8s_grow((void **)&f->deploys, &f->dep_cap, f->dep_n, sizeof(*f->deploys))) {+ f->deploys[f->dep_n++] =+ (k8s_deploy_t){node, cbm_mem_strdup(CBM_MEM_CLASS_OTHER, dir),+ x->ns ? cbm_mem_strdup(CBM_MEM_CLASS_OTHER, x->ns) : 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++] =(k8s_crd_t){node, cbm_mem_strdup(CBM_MEM_CLASS_OTHER, x->name),@@ -1138,6 +1212,300 @@ static void k8s_facts_keep(k8s_facts_t *f, const CBMFileResult *res, const int64}}+/* ── Deploy scopes (PHASE1 P1.3) ───────────────────────────────────── */++/* A Flux Kustomization or Argo Application deploys a repository directory:+ * the build of a kustomization there, else every manifest below it, a+ * sub-directory's kustomization by its build (as Flux generates one). The+ * objects a root deploy reaches through such directories -- and the deploys+ * among them, transitively -- form one deploy scope, a cluster: references+ * meet across kustomize roots inside it (a Kustomization's dependsOn, a+ * Certificate's ClusterIssuer). A path that names no directory of the+ * repository (another source's layout) deploys nothing. */++typedef struct {+ const char **items; /* graph-owned paths */+ int count;+ int cap;+} k8s_paths_t;++static void k8s_paths_push(k8s_paths_t *v, const char *p) {+ if (k8s_grow((void **)&v->items, &v->cap, v->count, sizeof(*v->items))) {+ v->items[v->count++] = p;+ }+}++static void k8s_paths_sort_unique(k8s_paths_t *v) {+ if (v->count == 0) {+ return;+ }+ qsort((void *)v->items, (size_t)v->count, sizeof(*v->items), k8s_cmp_str);+ int w = SKIP_ONE;+ for (int i = SKIP_ONE; i < v->count; i++) {+ if (strcmp(v->items[w - SKIP_ONE], v->items[i]) != 0) {+ v->items[w++] = v->items[i];+ }+ }+ v->count = w;+}++/* The manifests the build of kustomization `root` reaches; `seen` marks the+ * kustomizations visited under `mark`. */+static void k8s_kust_closure(const k8s_kusts_t *ks, int root, int *seen, int mark,+ k8s_paths_t *out) {+ int *stack = NULL;+ int depth = 0;+ int cap = 0;+ if (!k8s_grow((void **)&stack, &cap, depth, sizeof(int))) {+ return;+ }+ stack[depth++] = root;+ while (depth > 0) {+ int k = stack[--depth];+ if (seen[k] == mark) {+ continue;+ }+ seen[k] = mark;+ for (int i = 0; i < ks->items[k].file_n; i++) {+ k8s_paths_push(out, ks->items[k].files[i]);+ }+ for (int i = 0; i < ks->items[k].kid_n; i++) {+ if (k8s_grow((void **)&stack, &cap, depth, sizeof(int))) {+ stack[depth++] = ks->items[k].kids[i];+ }+ }+ }+ cbm_free(CBM_MEM_CLASS_OTHER, stack);+}++/* The manifests a deploy of directory `dir` applies; `manifests` is every+ * manifest path, sorted (a directory's are one range). */+static void k8s_dir_members(const k8s_kusts_t *ks, CBMHashTable *by_path,+ const k8s_paths_t *manifests, const char *dir, int *seen, int mark,+ k8s_paths_t *out) {+ size_t dl = strlen(dir);+ int lo = 0;+ int hi = dl ? manifests->count : 0;+ while (lo < hi) {+ int mid = lo + (hi - lo) / PAIR_LEN;+ int c = strncmp(manifests->items[mid], dir, dl);+ if (c < 0 || (c == 0 && manifests->items[mid][dl] < '/')) {+ lo = mid + SKIP_ONE;+ } else {+ hi = mid;+ }+ }+ for (int i = lo; i < manifests->count; i++) {+ const char *file = manifests->items[i];+ if (dl && (strncmp(file, dir, dl) != 0 || file[dl] != '/')) {+ break;+ }+ /* the outermost kustomization between `dir` and the file applies it */+ char up[CBM_SZ_1K];+ k8s_dirname(file, up, sizeof(up));+ int top = 0;+ for (;;) {+ int k = k8s_kust_in_dir(by_path, up);+ top = k ? k : top;+ if (strlen(up) <= dl) {+ break;+ }+ char parent[CBM_SZ_1K];+ k8s_dirname(up, parent, sizeof(parent));+ snprintf(up, sizeof(up), "%s", parent);+ }+ if (!top) {+ k8s_paths_push(out, file);+ } else if (seen[top - SKIP_ONE] != mark) {+ k8s_kust_closure(ks, top - SKIP_ONE, seen, mark, out);+ }+ }+ k8s_paths_sort_unique(out);+}++/* A deploy's sort key: where it is declared, then which one. */+typedef struct {+ const char *file;+ const char *qn;+ const char *dir;+ int deploy;+} k8s_deploy_key_t;++static int k8s_cmp_deploy_key(const void *a, const void *b) {+ const k8s_deploy_key_t *x = a;+ const k8s_deploy_key_t *y = b;+ int c = strcmp(x->file, y->file);+ c = c ? c : strcmp(x->qn, y->qn);+ c = c ? c : strcmp(x->dir, y->dir);+ return c ? c : (x->deploy > y->deploy) - (x->deploy < y->deploy);+}++/* The deploys declared in `file`: a range of `keys` (sorted by file). */+static int k8s_deploys_in(const k8s_deploy_key_t *keys, int n, const char *file, int *first) {+ int lo = 0;+ int hi = n;+ while (lo < hi) {+ int mid = lo + (hi - lo) / PAIR_LEN;+ if (strcmp(keys[mid].file, file) < 0) {+ lo = mid + SKIP_ONE;+ } else {+ hi = mid;+ }+ }+ int end = lo;+ while (end < n && strcmp(keys[end].file, file) == 0) {+ end++;+ }+ *first = lo;+ return end - lo;+}++typedef struct {+ int *items;+ int count;+ int cap;+} k8s_ints_t;++/* Build the deploy scopes (after the kustomize bundles). */+static void k8s_deploys_build(cbm_pipeline_ctx_t *ctx, k8s_kusts_t *ks, k8s_bundles_t *b,+ const k8s_facts_t *f) {+ int n = f->dep_n;+ const cbm_gbuf_node_t **res = NULL;+ int rn = 0;+ if (n == 0 || !b->names || cbm_gbuf_find_by_label(ctx->gbuf, "Resource", &res, &rn) != 0) {+ return;+ }+ b->deploys = cbm_ht_create(0);+ k8s_paths_t manifests = {0};+ for (int i = 0; i < rn; i++) {+ if (res[i]->file_path) {+ k8s_paths_push(&manifests, res[i]->file_path);+ }+ }+ k8s_paths_sort_unique(&manifests);+ CBMHashTable *by_path = cbm_ht_create(0);+ for (int i = 0; by_path && i < ks->count; i++) {+ cbm_ht_set(by_path, ks->items[i].path, (void *)(intptr_t)(i + SKIP_ONE));+ }+ size_t un = (size_t)n;+ k8s_paths_t *members = cbm_calloc(CBM_MEM_CLASS_OTHER, un * sizeof(*members));+ k8s_ints_t *adj = cbm_calloc(CBM_MEM_CLASS_OTHER, un * sizeof(*adj));+ const cbm_gbuf_node_t **node = cbm_calloc(CBM_MEM_CLASS_OTHER, un * sizeof(*node));+ k8s_deploy_key_t *keys = cbm_calloc(CBM_MEM_CLASS_OTHER, un * sizeof(*keys));+ int *indeg = cbm_calloc(CBM_MEM_CLASS_OTHER, un * sizeof(int));+ int *reached = cbm_calloc(CBM_MEM_CLASS_OTHER, un * sizeof(int));+ int *visit = cbm_calloc(CBM_MEM_CLASS_OTHER, un * sizeof(int));+ int *queue = cbm_calloc(CBM_MEM_CLASS_OTHER, un * sizeof(int));+ int *seen =+ cbm_calloc(CBM_MEM_CLASS_OTHER, (size_t)(ks->count ? ks->count : SKIP_ONE) * sizeof(int));+ int roots = 0;+ if (b->deploys && by_path && members && adj && node && keys && indeg && reached && visit &&+ queue && seen) {+ for (int d = 0; d < n; d++) {+ node[d] = cbm_gbuf_find_by_id(ctx->gbuf, f->deploys[d].node);+ keys[d] = (k8s_deploy_key_t){+ node[d] && node[d]->file_path ? node[d]->file_path : "",+ node[d] && node[d]->qualified_name ? node[d]->qualified_name : "",+ f->deploys[d].dir, d};+ k8s_dir_members(ks, by_path, &manifests, f->deploys[d].dir, seen, d + SKIP_ONE,+ &members[d]);+ }+ qsort(keys, un, sizeof(*keys), k8s_cmp_deploy_key);+ /* deploy d reaches deploy e when e sits in a manifest d applies */+ for (int d = 0; d < n; d++) {+ for (int i = 0; i < members[d].count; i++) {+ int first = 0;+ int cnt = k8s_deploys_in(keys, n, members[d].items[i], &first);+ for (int j = first; j < first + cnt; j++) {+ int e = keys[j].deploy;+ if (e != d &&+ k8s_grow((void **)&adj[d].items, &adj[d].cap, adj[d].count, sizeof(int))) {+ adj[d].items[adj[d].count++] = e;+ indeg[e]++;+ }+ }+ }+ }+ /* roots: deploys no other reaches; then, in order, any a cycle hides */+ for (int pass = 0; pass < PAIR_LEN; pass++) {+ for (int oi = 0; oi < n; oi++) {+ int r = keys[oi].deploy;+ if (reached[r] || (pass == 0 && indeg[r] != 0)) {+ continue;+ }+ const char *bundle = k8s_bundle_name(+ b, "deploy", node[r] ? node[r]->qualified_name : f->deploys[r].dir);+ if (!bundle) {+ continue;+ }+ roots++;+ int mark = pass * n + oi + SKIP_ONE;+ int qh = 0;+ int qt = 0;+ queue[qt++] = r;+ visit[r] = mark;+ while (qh < qt) {+ int d = queue[qh++];+ reached[d] = SKIP_ONE;+ if (node[d] && node[d]->file_path) {+ k8s_member_add(b->deploys, node[d]->file_path, bundle, NULL);+ }+ for (int i = 0; i < members[d].count; i++) {+ k8s_member_add(b->deploys, members[d].items[i], bundle, f->deploys[d].ns);+ }+ for (int i = 0; i < adj[d].count; i++) {+ int e = adj[d].items[i];+ if (visit[e] != mark) {+ visit[e] = mark;+ queue[qt++] = e;+ }+ }+ }+ }+ }+ }+ for (int d = 0; members && d < n; d++) {+ cbm_free(CBM_MEM_CLASS_OTHER, (void *)members[d].items);+ }+ for (int d = 0; adj && d < n; d++) {+ cbm_free(CBM_MEM_CLASS_OTHER, adj[d].items);+ }+ cbm_free(CBM_MEM_CLASS_OTHER, members);+ cbm_free(CBM_MEM_CLASS_OTHER, adj);+ cbm_free(CBM_MEM_CLASS_OTHER, (void *)node);+ cbm_free(CBM_MEM_CLASS_OTHER, keys);+ cbm_free(CBM_MEM_CLASS_OTHER, indeg);+ cbm_free(CBM_MEM_CLASS_OTHER, reached);+ cbm_free(CBM_MEM_CLASS_OTHER, visit);+ cbm_free(CBM_MEM_CLASS_OTHER, queue);+ cbm_free(CBM_MEM_CLASS_OTHER, seen);+ cbm_free(CBM_MEM_CLASS_OTHER, (void *)manifests.items);+ if (by_path) {+ cbm_ht_free(by_path);+ }+ cbm_log_info("pass.k8s.deploys", "deploys", itoa_k8s(n), "roots", itoa_k8s(roots));+}++/* The namespace of a Resource's logical key ("" when unknown `?` or+ * cluster-scoped `_`), else its declared one. */+static void k8s_effective_ns(const cbm_gbuf_node_t *n, char *out, size_t out_sz) {+ out[0] = '\0';+ const char *k = n->properties_json ? strstr(n->properties_json, "\"obj_key\":\"") : NULL;+ if (!k) {+ k8s_declared_ns(n->qualified_name, out, out_sz);+ return;+ }+ k += K8S_SLEN("\"obj_key\":\"");+ for (int part = 0; k && part < PAIR_LEN; part++) { /* past group and kind */+ k = strchr(k, '/');+ k = k ? k + SKIP_ONE : NULL;+ }+ const char *end = k ? strchr(k, '/') : NULL;+ if (end && !(end - k == SKIP_ONE && (k[0] == '?' || k[0] == '_'))) {+ snprintf(out, out_sz, "%.*s", (int)(end - k), k);+ }+}+/* Does the target at `tgt` answer `ref` from `src`: one bundle, and the* namespace the reference asks for (explicit, else the source's own there)* agrees with the target's there? A cluster-scoped kind has no namespace. */@@ -1166,6 +1534,26 @@ static bool k8s_ref_meets(k8s_bundles_t *b, const k8s_ref_t *ref, const cbm_gbuf}}}+ /* or one deploy scope: the cluster a Flux / Argo root deploys */+ const k8s_members_t *ds = b->deploys ? cbm_ht_get(b->deploys, src->file_path) : NULL;+ const k8s_members_t *dt = ds ? cbm_ht_get(b->deploys, tgt->file_path) : NULL;+ if (!dt) {+ return false;+ }+ k8s_effective_ns(src, src_ns, sizeof(src_ns));+ k8s_effective_ns(tgt, tgt_ns, sizeof(tgt_ns));+ for (int i = 0; i < ds->count; i++) {+ for (int j = 0; j < dt->count; j++) {+ if (ds->items[i].bundle != dt->items[j].bundle) {+ continue;+ }+ const char *want = ref->ns ? ref->ns : ds->items[i].ns ? ds->items[i].ns : src_ns;+ const char *have = dt->items[j].ns ? dt->items[j].ns : tgt_ns;+ if (cluster || !want[0] || !have[0] || strcmp(want, have) == 0) {+ return true;+ }+ }+ }return false;}@@ -1186,7 +1574,10 @@ static void k8s_resolve_refs(cbm_pipeline_ctx_t *ctx, k8s_bundles_t *b, const k8}char props[CBM_SZ_256];snprintf(props, sizeof(props), "{\"%s\":\"%s\"}",- strcmp(ref->edge, "INFRA_MAPS") == 0 ? "kind" : "ref", ref->via);+ strcmp(ref->edge, "INFRA_MAPS") == 0 ? "kind"+ : strcmp(ref->edge, "DEPENDS_ON") == 0 ? "via"+ : "ref",+ ref->via);/* A name is one object per kind and namespace, so two declarations* of it in one bundle are alternatives (versioned manifests side by* side): the declaration in the reference's own file, when there is@@ -1954,6 +2345,7 @@ int cbm_pipeline_pass_k8s(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files,/* Connect Services to the workloads their selectors target (INFRA_MAPS),* inside the bundles that deploy them together. */k8s_bundles_build(ctx, &kusts, &bundles);+ k8s_deploys_build(ctx, &kusts, &bundles, &facts);k8s_apply_effective_namespaces(ctx, &bundles);k8s_link_selectors(ctx, &recs, &bundles);k8s_resolve_refs(ctx, &bundles, &facts);diff --git a/tests/test_grammar_probe_f.c b/tests/test_grammar_probe_f.cindex 99b5b6ef5..812a6f16c 100644--- a/tests/test_grammar_probe_f.c+++ b/tests/test_grammar_probe_f.c@@ -1275,6 +1275,213 @@ TEST(probe_go_controller_methods_sharing_a_node) {PASS();}+/* ── Curated CRD references + deploy scopes (P1.3) ──────────────────── */++/* A Flux bootstrap: the flux-system Kustomization deploys ./clusters/prod,+ * whose Kustomizations deploy ./infra and ./apps/prod. Each is its own+ * kustomize root, so the references between them -- a Kustomization's+ * sourceRef to the flux-system GitRepository, a HelmRelease's dependsOn in+ * another namespace, a Certificate's ClusterIssuer -- meet only in the+ * deploy scope the root reaches. */+TEST(probe_flux_refs_meet_in_the_deploy_scope) {+ static const GpfFile files[] = {+ {"clusters/prod/flux-system/kustomization.yaml",+ "apiVersion: kustomize.config.k8s.io/v1beta1\nkind: Kustomization\n"+ "resources:\n - gotk-sync.yaml\n"},+ {"clusters/prod/flux-system/gotk-sync.yaml",+ "apiVersion: source.toolkit.fluxcd.io/v1\nkind: GitRepository\n"+ "metadata:\n name: flux-system\n namespace: flux-system\n"+ "spec:\n url: ssh://[email protected]/fleet\n---\n"+ "apiVersion: kustomize.toolkit.fluxcd.io/v1\nkind: Kustomization\n"+ "metadata:\n name: flux-system\n namespace: flux-system\n"+ "spec:\n path: ./clusters/prod\n sourceRef:\n kind: GitRepository\n"+ " name: flux-system\n"},+ {"clusters/prod/apps.yaml",+ "apiVersion: kustomize.toolkit.fluxcd.io/v1\nkind: Kustomization\n"+ "metadata:\n name: infra\n namespace: flux-system\n"+ "spec:\n path: ./infra\n sourceRef:\n kind: GitRepository\n"+ " name: flux-system\n---\n"+ "apiVersion: kustomize.toolkit.fluxcd.io/v1\nkind: Kustomization\n"+ "metadata:\n name: apps\n namespace: flux-system\n"+ "spec:\n dependsOn:\n - name: infra\n path: ./apps/prod\n"+ " sourceRef:\n kind: GitRepository\n name: flux-system\n"},+ {"infra/kustomization.yaml",+ "apiVersion: kustomize.config.k8s.io/v1beta1\nkind: Kustomization\n"+ "resources:\n - cert-manager.yaml\n"},+ {"infra/cert-manager.yaml",+ "apiVersion: helm.toolkit.fluxcd.io/v2\nkind: HelmRelease\n"+ "metadata:\n name: cert-manager\n namespace: cert-manager\n"+ "spec:\n interval: 1h\n---\n"+ "apiVersion: cert-manager.io/v1\nkind: ClusterIssuer\n"+ "metadata:\n name: letsencrypt\nspec:\n selfSigned: {}\n"},+ {"apps/prod/kustomization.yaml",+ "apiVersion: kustomize.config.k8s.io/v1beta1\nkind: Kustomization\n"+ "namespace: podinfo\nresources:\n - ../base/podinfo\n"},+ {"apps/base/podinfo/kustomization.yaml",+ "apiVersion: kustomize.config.k8s.io/v1beta1\nkind: Kustomization\n"+ "resources:\n - release.yaml\n - repo.yaml\n"},+ {"apps/base/podinfo/release.yaml",+ "apiVersion: helm.toolkit.fluxcd.io/v2\nkind: HelmRelease\n"+ "metadata:\n name: podinfo\nspec:\n chart:\n spec:\n chart: podinfo\n"+ " sourceRef:\n kind: HelmRepository\n name: podinfo\n"+ " dependsOn:\n - name: cert-manager\n namespace: cert-manager\n"+ " valuesFrom:\n - kind: ConfigMap\n name: podinfo-values\n---\n"+ "apiVersion: cert-manager.io/v1\nkind: Certificate\n"+ "metadata:\n name: podinfo-tls\nspec:\n secretName: podinfo-tls\n"+ " issuerRef:\n kind: ClusterIssuer\n name: letsencrypt\n"},+ {"apps/base/podinfo/repo.yaml",+ "apiVersion: source.toolkit.fluxcd.io/v1\nkind: HelmRepository\n"+ "metadata:\n name: podinfo\nspec:\n url: https://example.com/charts\n---\n"+ "apiVersion: v1\nkind: ConfigMap\nmetadata:\n name: podinfo-values\n"+ "data:\n replicaCount: \"2\"\n"}};+ /* flux-system -> its GitRepository, infra and apps -> it, podinfo -> HelmRepository */+ ASSERT_EQ(gpf_typed_edges(files, 9, "USAGE", "Resource", "Resource", "\"ref\":\"sourceRef\""),+ 4);+ /* apps -> infra; podinfo -> cert-manager in another namespace and root */+ ASSERT_EQ(gpf_typed_edges(files, 9, "DEPENDS_ON", "Resource", "Resource",+ "\"via\":\"dependsOn\""),+ 2);+ ASSERT_EQ(gpf_typed_edges(files, 9, "USAGE", "Resource", "Resource", "\"ref\":\"issuerRef\""),+ 1);+ ASSERT_EQ(gpf_typed_edges(files, 9, "USAGE", "Resource", "Resource",+ "\"ref\":\"valuesFrom\""),+ 1);+ PASS();+}++/* Cluster API: a Cluster names its infrastructure, control plane and+ * class; machines name their cluster, bootstrap config and templates. The+ * source kind counts in its API group: a CloudNativePG `Cluster` with the+ * same field is not a Cluster API one. */+TEST(probe_capi_refs_follow_the_api_group) {+ static const char tmpl[] =+ "apiVersion: cluster.x-k8s.io/v1beta2\nkind: Cluster\nmetadata:\n name: c1\n"+ "spec:\n infrastructureRef:\n kind: DockerCluster\n name: c1\n"+ " controlPlaneRef:\n kind: KubeadmControlPlane\n name: c1-cp\n"+ " topology:\n classRef:\n name: quick\n---\n"+ "apiVersion: infrastructure.cluster.x-k8s.io/v1beta2\nkind: DockerCluster\n"+ "metadata:\n name: c1\n---\n"+ "apiVersion: controlplane.cluster.x-k8s.io/v1beta2\nkind: KubeadmControlPlane\n"+ "metadata:\n name: c1-cp\nspec:\n machineTemplate:\n spec:\n"+ " infrastructureRef:\n kind: DockerMachineTemplate\n name: cp-infra\n---\n"+ "apiVersion: infrastructure.cluster.x-k8s.io/v1beta2\nkind: DockerMachineTemplate\n"+ "metadata:\n name: cp-infra\n---\n"+ "apiVersion: cluster.x-k8s.io/v1beta2\nkind: MachineDeployment\nmetadata:\n name: md\n"+ "spec:\n clusterName: c1\n template:\n spec:\n bootstrap:\n"+ " configRef:\n kind: KubeadmConfigTemplate\n name: md-boot\n"+ " infrastructureRef:\n kind: DockerMachineTemplate\n name: md-infra\n---\n"+ "apiVersion: bootstrap.cluster.x-k8s.io/v1beta2\nkind: KubeadmConfigTemplate\n"+ "metadata:\n name: md-boot\n---\n"+ "apiVersion: infrastructure.cluster.x-k8s.io/v1beta2\nkind: DockerMachineTemplate\n"+ "metadata:\n name: md-infra\n---\n"+ "apiVersion: cluster.x-k8s.io/v1beta2\nkind: ClusterClass\nmetadata:\n name: quick\n"+ "spec:\n infrastructure:\n templateRef:\n kind: DockerClusterTemplate\n"+ " name: quick-cluster\n workers:\n machineDeployments:\n - class: w\n"+ " bootstrap:\n templateRef:\n kind: KubeadmConfigTemplate\n"+ " name: md-boot\n---\n"+ "apiVersion: infrastructure.cluster.x-k8s.io/v1beta2\nkind: DockerClusterTemplate\n"+ "metadata:\n name: quick-cluster\n---\n"+ "apiVersion: postgresql.cnpg.io/v1\nkind: Cluster\nmetadata:\n name: pg\n"+ "spec:\n infrastructureRef:\n kind: DockerCluster\n name: c1\n";+ static const GpfFile files[] = {{"templates/cluster.yaml", tmpl}};+ /* Cluster, KubeadmControlPlane and MachineDeployment -- not the cnpg Cluster */+ ASSERT_EQ(gpf_typed_edges(files, 1, "USAGE", "Resource", "Resource",+ "\"ref\":\"infrastructureRef\""),+ 3);+ ASSERT_EQ(gpf_typed_edges(files, 1, "USAGE", "Resource", "Resource",+ "\"ref\":\"controlPlaneRef\""),+ 1);+ ASSERT_EQ(gpf_typed_edges(files, 1, "USAGE", "Resource", "Resource",+ "\"ref\":\"topologyClass\""),+ 1);+ ASSERT_EQ(gpf_typed_edges(files, 1, "USAGE", "Resource", "Resource",+ "\"ref\":\"clusterName\""),+ 1);+ ASSERT_EQ(gpf_typed_edges(files, 1, "USAGE", "Resource", "Resource", "\"ref\":\"bootstrap\""),+ 1);+ ASSERT_EQ(gpf_typed_edges(files, 1, "USAGE", "Resource", "Resource", "\"ref\":\"template\""),+ 2);+ PASS();+}++/* cert-manager and Kubermatic: a Certificate's issuer (Issuer when the+ * kind is omitted) and Secret, an Ingress's cluster-issuer annotation, an+ * issuer's CA and DNS-solver Secrets (`*SecretRef` of any provider), a KKP+ * Seed's kubeconfig and a user cluster's provider credentials. */+TEST(probe_cert_manager_and_kkp_refs) {+ static const char doc[] =+ "apiVersion: cert-manager.io/v1\nkind: Issuer\nmetadata:\n name: ca\n"+ "spec:\n ca:\n secretName: ca-key\n---\n"+ "apiVersion: cert-manager.io/v1\nkind: ClusterIssuer\nmetadata:\n name: letsencrypt\n"+ "spec:\n acme:\n solvers:\n - dns01:\n cloudflare:\n"+ " apiTokenSecretRef:\n name: cf-token\n key: token\n"+ "---\n"+ "apiVersion: cert-manager.io/v1\nkind: Certificate\nmetadata:\n name: web\n"+ "spec:\n secretName: web-tls\n issuerRef:\n name: ca\n---\n"+ "apiVersion: networking.k8s.io/v1\nkind: Ingress\nmetadata:\n name: web\n"+ " annotations:\n cert-manager.io/cluster-issuer: letsencrypt\n---\n"+ "apiVersion: v1\nkind: Secret\nmetadata:\n name: ca-key\n---\n"+ "apiVersion: v1\nkind: Secret\nmetadata:\n name: cf-token\n---\n"+ "apiVersion: v1\nkind: Secret\nmetadata:\n name: web-tls\n---\n"+ "apiVersion: kubermatic.k8c.io/v1\nkind: Seed\nmetadata:\n name: europe\n"+ " namespace: kubermatic\nspec:\n kubeconfig:\n name: seed-kubeconfig\n"+ " namespace: kubermatic\n---\n"+ "apiVersion: kubermatic.k8c.io/v1\nkind: Cluster\nmetadata:\n name: user1\n"+ "spec:\n cloud:\n aws:\n credentialsReference:\n name: aws-creds\n"+ " namespace: kubermatic\n---\n"+ "apiVersion: v1\nkind: Secret\nmetadata:\n name: seed-kubeconfig\n"+ " namespace: kubermatic\n---\n"+ "apiVersion: v1\nkind: Secret\nmetadata:\n name: aws-creds\n namespace: kubermatic\n";+ static const GpfFile files[] = {{"pki/certs.yaml", doc}};+ ASSERT_EQ(gpf_typed_edges(files, 1, "USAGE", "Resource", "Resource", "\"ref\":\"issuerRef\""),+ 1);+ ASSERT_EQ(gpf_typed_edges(files, 1, "USAGE", "Resource", "Resource",+ "\"ref\":\"secretName\""),+ 1);+ ASSERT_EQ(gpf_typed_edges(files, 1, "USAGE", "Resource", "Resource", "\"ref\":\"issuer\""), 1);+ ASSERT_EQ(gpf_typed_edges(files, 1, "USAGE", "Resource", "Resource", "\"ref\":\"secretRef\""),+ 2);+ ASSERT_EQ(gpf_typed_edges(files, 1, "USAGE", "Resource", "Resource",+ "\"ref\":\"kubeconfig\""),+ 1);+ ASSERT_EQ(gpf_typed_edges(files, 1, "USAGE", "Resource", "Resource",+ "\"ref\":\"credentials\""),+ 1);+ PASS();+}++/* Argo: an Application's project, and a Rollout -- a workload with a pod+ * template -- names its Services, analysis template and service account. */+TEST(probe_argo_application_and_rollout_refs) {+ static const char doc[] =+ "apiVersion: argoproj.io/v1alpha1\nkind: AppProject\nmetadata:\n name: team-a\n"+ " namespace: argocd\n---\n"+ "apiVersion: argoproj.io/v1alpha1\nkind: Application\nmetadata:\n name: web\n"+ " namespace: argocd\nspec:\n project: team-a\n source:\n path: apps/web\n"+ " destination:\n namespace: web\n---\n"+ "apiVersion: argoproj.io/v1alpha1\nkind: Rollout\nmetadata:\n name: web\n"+ "spec:\n strategy:\n canary:\n canaryService: web-canary\n"+ " stableService: web-stable\n analysis:\n templates:\n"+ " - templateName: success-rate\n template:\n spec:\n"+ " serviceAccountName: web\n---\n"+ "apiVersion: v1\nkind: Service\nmetadata:\n name: web-canary\n---\n"+ "apiVersion: v1\nkind: Service\nmetadata:\n name: web-stable\n---\n"+ "apiVersion: argoproj.io/v1alpha1\nkind: AnalysisTemplate\nmetadata:\n"+ " name: success-rate\n---\n"+ "apiVersion: v1\nkind: ServiceAccount\nmetadata:\n name: web\n";+ static const GpfFile files[] = {{"apps/web/rollout.yaml", doc}};+ ASSERT_EQ(gpf_typed_edges(files, 1, "USAGE", "Resource", "Resource", "\"ref\":\"project\""), 1);+ ASSERT_EQ(gpf_typed_edges(files, 1, "USAGE", "Resource", "Resource",+ "\"ref\":\"rolloutService\""),+ 2);+ ASSERT_EQ(gpf_typed_edges(files, 1, "USAGE", "Resource", "Resource", "\"ref\":\"analysis\""),+ 1);+ ASSERT_EQ(gpf_typed_edges(files, 1, "USAGE", "Resource", "Resource",+ "\"ref\":\"serviceAccountName\""),+ 1);+ 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. */@@ -2879,6 +3086,10 @@ SUITE(grammar_probe_f) {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_flux_refs_meet_in_the_deploy_scope);+ RUN_TEST(probe_capi_refs_follow_the_api_group);+ RUN_TEST(probe_cert_manager_and_kkp_refs);+ RUN_TEST(probe_argo_application_and_rollout_refs);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);