Skip to content

Commit b1d93cc

Browse files
committed
feat(infra): Kubernetes objects declared in Terraform are Resources
A `kubernetes_*` resource in a .tf file -- eks-blueprints' network policies and storage classes, k8s.io's metrics-server stack -- declares a cluster object, but the graph only had the Terraform block, named by its Terraform address: no node carried the object's kind and name. A kubernetes_<type> resource (also _vN) whose metadata name is a literal is now a Resource of the same identity scheme as a manifest's object: kind from the resource type, or a kubernetes_manifest's own apiVersion and kind; name and namespace from the object's own metadata, never from spec; `ecosystem: terraform` on its properties. The .tf file DEFINES it and the block DEPLOYS it {via: terraform, line}. The objects are minted before the Kubernetes joins, so a manifest of the same bundle references them as it does a manifest's object (an Ingress maps to the Service). A computed name, and a type that acts on an object declared elsewhere (kubernetes_annotations, kubernetes_labels, ...), make none. Labels are not read, so a Service selector does not reach a Terraform workload. Measured on 17 infrastructure repositories: 21 of 21 literal-named objects against an independent reading, with group, kind, name and namespace; nothing else changed (21 nodes, 42 edges added, no property of an existing node); deterministic over three runs; the incremental re-index equals a full index when an object is renamed, gains a namespace, or its file is added or deleted. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent cdfdb8f commit b1d93cc

5 files changed

Lines changed: 425 additions & 14 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ The install script placed beside the binary is **reported, not deleted** — uni
252252
- `INFRA_MAPS` — a Kubernetes Service to each workload whose pod labels carry every `key: value` of the Service's selector, within one namespace
253253
- `CONNECTS_TO` — a Kubernetes workload or compose service to the Service its containers' environment addresses (`PRODUCT_CATALOG_SERVICE_ADDR: productcatalogservice:3550`, a URL's host, `<svc>.<ns>.svc...`, a bare name on an `*_ADDR` / `*_HOST` / `*_URL` / ... key; compose: the project's `.env` interpolated): a Kubernetes Service of the same bundle and namespace, or a compose service of the same file. The edge keeps `via: env`, `key` and `container` — never the value
254254
- `USAGE` with `ref: image` — a Kubernetes workload (any pod template: containers, initContainers, CronJobs, custom resources) or a docker compose service to the `Image` it runs
255-
- `DEPLOYS` — what puts manifests, kustomizations or charts on a cluster, to what it puts there: a Flux Kustomization or Argo Application to the kustomizations / manifests / chart of its path, a Flux HelmRelease to its chart (`spec.chart` or `chartRef` → OCIRepository), a shell command or Make recipe (`kubectl apply -f/-k`, `kustomize build | kubectl apply`, `helm install/upgrade/template`), a Go installer loading a chart with the Helm SDK, a Terraform `helm_release`, a kustomization inflating a chart (`helmCharts`; its values file an `IMPORTS`). A chart outside the repository is a shared external `Chart`; the edge says `via` (`flux`, `argo`, `helmrelease`, `kubectl`, `kustomize`, `helm`, `helm-sdk`, `terraform`, `helmcharts`, ...)
255+
- `DEPLOYS` — what puts manifests, kustomizations or charts on a cluster, to what it puts there: a Flux Kustomization or Argo Application to the kustomizations / manifests / chart of its path, a Flux HelmRelease to its chart (`spec.chart` or `chartRef` → OCIRepository), a shell command or Make recipe (`kubectl apply -f/-k`, `kustomize build | kubectl apply`, `helm install/upgrade/template`), a Go installer loading a chart with the Helm SDK, a Terraform `helm_release`, a Terraform `kubernetes_*` resource the object it declares (a `Resource` like a manifest's, `ecosystem: terraform`, when its name is a literal), a kustomization inflating a chart (`helmCharts`; its values file an `IMPORTS`). A chart outside the repository is a shared external `Chart`; the edge says `via` (`flux`, `argo`, `helmrelease`, `kubectl`, `kustomize`, `helm`, `helm-sdk`, `terraform`, `helmcharts`, ...)
256256
- `DEPENDS_ON` — a Helm chart to the charts it declares (the local chart for a resolvable `file://` repository, otherwise an external `Chart`; the edge keeps `repository`), and a `go.mod` / `requirements.txt` file to an external `Package`; a Dockerfile to the `Image`s it builds on (`via: from`, `copy-from`); an `Image` to the Dockerfile it is built from (`via: build`; `tool`: `skaffold`, `compose`, `docker-build` for `docker` / `podman` / `nerdctl` / `buildah` build commands in scripts, Make and CI); a compose service to the services its `depends_on` / `links` name
257257
- `CONFIGURES` — code reading an environment variable to the shared `EnvVar` of that name (`strategy: env_access`); a Kubernetes container, compose service or Dockerfile (`ENV` of the image) setting one to that container's own `EnvVar` (`strategy: env_set`, with `source`: `value`, `configMapKeyRef`, `secretKeyRef`, `fieldRef`, ..., `compose`, `dockerfile` — values are never stored); and that container's `EnvVar` to the code reading the name inside the files its image is built from (`strategy: env_read`, with `dockerfile`) — the image's files are the sources its Dockerfile's `COPY` / `ADD` put into it, less the context's `.dockerignore`
258258
- `OVERRIDE` with `via: patch` — a Kustomize patch to each object of its kustomization's build it changes (child → base): a patch file's documents to the object of the same kind and name; an inline patch (from the kustomization) or a JSON6902 operation file to what its `target` selects (group, version, kind, name, namespace, label selector); a component's in the build of each kustomization including it. The edge keeps `kustomization`, `patch` and `line`; a patch file is an `IMPORTS` (`local_name: patches`) of its kustomization, never a member of the build

‎src/pipeline/pass_k8s.c‎

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -554,10 +554,12 @@ static bool k8s_cluster_scoped(const char *kind) {
554554
* not NULL) is the namespace a kustomize build gives it. `version` (NULL or
555555
* "": none) is the version of its apiVersion, which the key leaves out;
556556
* `labels` (NULL: none) its metadata.labels as "k=v,k2=v2" (extract_k8s.c:
557-
* nothing JSON escapes in it). NULL for a QN without a well-formed key (the
558-
* caller keeps the plain source tag). */
557+
* nothing JSON escapes in it); `ecosystem` (NULL: a manifest's) what
558+
* declares it otherwise -- "terraform" (pass_terraform.c), plain text. NULL
559+
* for a QN without a well-formed key (the caller keeps the plain source
560+
* tag). */
559561
static char *k8s_resource_props(const char *qn, const char *effective_ns, const char *version,
560-
const char *labels) {
562+
const char *labels, const char *ecosystem) {
561563
enum { K8S_KEY_PARTS = 4, K8S_ESC = 6 * K8S_LABEL_LEN };
562564
const char *key = qn ? strstr(qn, "::") : NULL;
563565
if (!key) {
@@ -597,8 +599,11 @@ static char *k8s_resource_props(const char *qn, const char *effective_ns, const
597599
if (!props) {
598600
return NULL;
599601
}
600-
int w =
601-
snprintf(props, n, "{\"source\":\"k8s\",\"group\":\"%s\",\"kind\":\"%s\"", esc[0], esc[1]);
602+
int w = snprintf(props, n, "{\"source\":\"k8s\"");
603+
if (ecosystem && ecosystem[0]) {
604+
w += snprintf(props + w, n - (size_t)w, ",\"ecosystem\":\"%s\"", ecosystem);
605+
}
606+
w += snprintf(props + w, n - (size_t)w, ",\"group\":\"%s\",\"kind\":\"%s\"", esc[0], esc[1]);
602607
if (esc[5][0]) {
603608
w += snprintf(props + w, n - (size_t)w, ",\"version\":\"%s\"", esc[5]);
604609
}
@@ -613,6 +618,11 @@ static char *k8s_resource_props(const char *qn, const char *effective_ns, const
613618
return props;
614619
}
615620

621+
char *cbm_k8s_resource_props(const char *qn, const char *effective_ns, const char *version,
622+
const char *labels, const char *ecosystem) {
623+
return k8s_resource_props(qn, effective_ns, version, labels, ecosystem);
624+
}
625+
616626
/* The string property `key` of node `n` (as k8s_resource_props writes it:
617627
* no escapes inside) into `out`, "" when it has none. */
618628
static void k8s_node_prop(const cbm_gbuf_node_t *n, const char *key, char *out, size_t out_sz) {
@@ -1171,9 +1181,12 @@ static void k8s_apply_effective_namespaces(cbm_pipeline_ctx_t *ctx, k8s_bundles_
11711181
continue; /* no build sets a namespace: the declared one stands */
11721182
}
11731183
char version[K8S_LABEL_LEN];
1184+
char ecosystem[K8S_LABEL_LEN];
11741185
k8s_node_prop(n, "version", version, sizeof(version));
1186+
k8s_node_prop(n, "ecosystem", ecosystem, sizeof(ecosystem));
11751187
char *labels = k8s_node_prop_dup(n, "labels");
1176-
char *props = k8s_resource_props(n->qualified_name, agree ? eff : "?", version, labels);
1188+
char *props =
1189+
k8s_resource_props(n->qualified_name, agree ? eff : "?", version, labels, ecosystem);
11771190
cbm_free(CBM_MEM_CLASS_OTHER, labels);
11781191
if (props) {
11791192
(void)cbm_gbuf_node_set_properties_json((cbm_gbuf_node_t *)n, props);
@@ -2427,7 +2440,7 @@ static void handle_k8s_manifest(cbm_pipeline_ctx_t *ctx, const char *rel_path, c
24272440
labels = kv[SKIP_ONE];
24282441
}
24292442
}
2430-
char *props = k8s_resource_props(def->qualified_name, NULL, version, labels);
2443+
char *props = k8s_resource_props(def->qualified_name, NULL, version, labels, NULL);
24312444
int64_t node_id = cbm_gbuf_upsert_node(
24322445
ctx->gbuf, "Resource", def->name, def->qualified_name, rel_path, (int)def->start_line,
24332446
(int)def->end_line, props ? props : "{\"source\":\"k8s\"}");
@@ -4131,6 +4144,10 @@ int cbm_pipeline_pass_k8s(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files,
41314144
}
41324145
cbm_free(CBM_MEM_CLASS_OTHER, prep);
41334146

4147+
/* Kubernetes objects Terraform declares (pass_terraform.c), before the
4148+
* bundles and the joins: references meet them like manifest objects. */
4149+
cbm_pipeline_tf_k8s_objects(ctx, files, file_count);
4150+
41344151
/* Connect Services to the workloads their selectors target (INFRA_MAPS),
41354152
* inside the bundles that deploy them together. */
41364153
k8s_bundles_build(ctx, &kusts, &bundles);

0 commit comments

Comments
 (0)