engenho_kube_codegen/lib.rs
1//! # engenho-kube-codegen
2//!
3//! Reads vendored Kubernetes `OpenAPI` v3 JSON + emits typed Rust source
4//! into `engenho-types/src/generated_v1_34/`. The
5//! load-bearing invariant: no hand-authored K8s resource
6//! types — every kind is generated bit-reproducibly from upstream.
7//!
8//! ## What's generated today (M0.0.1 first pass)
9//!
10//! A "thin kind" representation: each generated struct carries:
11//! * `metadata: engenho_types::meta::ObjectMeta`
12//! * `spec: serde_json::Value` (typed expansion is M0.0.4)
13//! * `status: Option<serde_json::Value>`
14//!
15//! Plus a `KubeResource` impl with the correct
16//! `GroupVersionKind` / `GroupVersionResource` / `Scope`.
17//!
18//! ## Future (M0.0.4)
19//!
20//! Recursive `$ref` expansion through the `OpenAPI` spec produces
21//! fully-typed `PodSpec`, `Container`, `Volume`, etc. Hand-rolled
22//! `ObjectMeta` also becomes generator output at that point.
23//!
24//! ## Determinism contract
25//!
26//! `kube-codegen --check` (callable from CI) must regenerate
27//! byte-identical source from the same input. Implementation:
28//! * Sort iteration deterministic (`BTreeMap`, never `HashMap`).
29//! * Emitted source has a fixed header comment.
30//! * Field ordering: required-then-optional, then alphabetical.
31
32#![warn(missing_docs)]
33
34pub mod catalog;
35pub mod emit;
36#[allow(missing_docs)]
37pub mod emit_blue;
38pub mod emit_typed;
39pub mod openapi;
40#[allow(missing_docs)]
41pub mod schemas;
42pub mod types;
43
44pub use catalog::{KIND_CATALOG, KindEntry, Subresource};
45pub use emit::{emit_catalog, emit_kind, emit_kind_typed, emit_module, emit_shared_module};
46pub use emit_blue::emit_blue_kinds;
47pub use emit_typed::{SchemaView, shared_substructs};
48pub use openapi::{KindShape, OpenApiDoc};
49
50/// Format emitted Rust source, deterministically and without a subprocess.
51///
52/// ## Why this exists
53///
54/// The generator emitted UNFORMATTED source while the committed files were
55/// formatted. Measured 2026-08-30: `--check` reported 46 drifting files
56/// against a pristine tree, and **40 of them differed by formatting alone**
57/// — reformatted by `16d0fd5`, a workspace-wide `cargo fmt` that swept 149
58/// files including this generated directory.
59///
60/// A determinism gate that is permanently red is not a gate. Once
61/// `--check` could never pass, nobody could see the SIX real differences
62/// hiding behind the forty cosmetic ones — including three hand-authored
63/// files (`rbac_v1/policy.rs`, `apps_v1/deployment_spec.rs`,
64/// `apps_v1/replicaset_spec.rs`) living inside a directory whose header
65/// says GENERATED — DO NOT EDIT. Regenerating deletes them, which is
66/// exactly how a routine regen broke `engenho-apiserver`'s authz module.
67///
68/// ## Why prettyplease and not rustfmt
69///
70/// `--check` compares bytes, so the formatter must be pinned by Cargo, not
71/// by whatever `rustfmt` is on the operator's PATH. A subprocess would also
72/// be a shell-out in a codebase that forbids them. `prettyplease` is a
73/// library, versioned in the lockfile, identical on every machine.
74///
75/// # Errors
76///
77/// Returns the parse error if `src` is not valid Rust. This fails LOUDLY
78/// on purpose: a generator emitting unparseable source is a defect, and
79/// silently writing it out would move the failure to whoever builds next.
80pub fn format_source(src: &str) -> anyhow::Result<String> {
81 let parsed = syn::parse_file(src)
82 .map_err(|e| anyhow::anyhow!("generated source is not valid Rust: {e}"))?;
83 Ok(prettyplease::unparse(&parsed))
84}