feat(monetizeapi): controller-gen as canonical CRD schema source#525
Closed
bussyjd wants to merge 1 commit into
Closed
feat(monetizeapi): controller-gen as canonical CRD schema source#525bussyjd wants to merge 1 commit into
bussyjd wants to merge 1 commit into
Conversation
Closes the entire class of "CRD YAML and Go struct drifted" bugs.
PurchaseAutoRefill.MaxTotal was the most recent instance — it existed
in purchaserequest-crd.yaml for months while internal/monetizeapi/
types.go didn't have the corresponding field. Without this commit,
that pattern recurs by design: two sources of truth, one hand-
maintained, no enforcement of agreement.
Now Go is the single source of truth:
- kubebuilder markers on every CRD-backed struct in types.go
(validation, required, enum, pattern, printer columns, subresources)
- `just generate` regenerates *-crd.yaml from those markers
+ zz_generated.deepcopy.go from object:generate=true
- CI fails if `git status` is non-empty after `just generate` runs
This commit also fixes the documented MaxTotal / MaxSpendPerDay drift
by adding both fields to PurchaseAutoRefill — the generated CRD now
matches the prior hand-written one and the controller can read them.
Pinned controller-tools at v0.16.5 in tools/tools.go (compatible with
client-go v0.34.x; a newer release would force prometheus/common
through a panicking validation-scheme change). Generation is
deterministic; running locally produces no diff after a clean
checkout.
For future CRD edits:
1. Edit types.go (add/change a field, update markers)
2. `just generate`
3. Commit both the Go and YAML diffs
4. CI verifies the YAML was committed
PreSignedAuth.Payment is map[string]interface{} (opaque x402
payload), which controller-gen cannot deep-copy automatically; a
hand-written DeepCopy lives in deepcopy_manual.go and the type is
flagged object:generate=false.
The hack/boilerplate.go.txt file is force-added past *.txt gitignore;
it's an empty marker for now — add a copyright header later if the
repo settles on one.
Comment on lines
+48
to
+73
| name: CRD generation up-to-date | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1 | ||
|
|
||
| - name: Set up Go | ||
| uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0 | ||
| with: | ||
| go-version-file: 'go.mod' | ||
|
|
||
| - name: Set up just | ||
| uses: extractions/setup-just@dd310ad5a97d8e7b41793f8ef055398d51ad4de6 # v2.0.2 | ||
|
|
||
| - name: Regenerate CRDs + DeepCopy | ||
| run: just generate | ||
|
|
||
| - name: Fail if regeneration changed any tracked files | ||
| run: | | ||
| if [ -n "$(git status --porcelain)" ]; then | ||
| echo "::error::CRD manifests or DeepCopy methods are out of date." | ||
| echo "::error::Run 'just generate' locally and commit the result." | ||
| git status | ||
| git --no-pager diff | ||
| exit 1 | ||
| fi |
This was referenced May 23, 2026
Collaborator
Author
|
Superseded by bundle PR #536 — closing in favor of the consolidated merge target. Original branch and history preserved. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
CRD-vs-Go drift is a documented recurring bug class.
PurchaseAutoRefill.MaxTotalexisted inpurchaserequest-crd.yamlfor months whiletypes.godidn't read it — fixed by hand in PR #513 with no enforcement to prevent the next instance. This commit also adds back the missingMaxTotalandMaxSpendPerDayfields to the Go struct so the next reconcile actually reads what the CRD declares.Before
After
What changed
tools/tools.go— anchors controller-gen as a build dependency (v0.16.5, compatible with k8s.io/client-go v0.34.x)justfile—generaterecipe runs controller-gen + renamesobol.org_<plural>.yamlto existing<singular>-crd.yamlnaminghack/boilerplate.go.txt— generated-file marker (force-added past the repo's*.txtgitignore)internal/monetizeapi/doc.go— package-level+groupName=obol.org/+versionName=v1alpha1/+kubebuilder:object:generate=trueinternal/monetizeapi/types.go— kubebuilder markers on every CRD-backed type, plus the previously-missingMaxTotal/MaxSpendPerDayfields onPurchaseAutoRefillinternal/monetizeapi/deepcopy_manual.go— hand-written DeepCopy forPreSignedAuth(itsPayment map[string]interface{}is opaque to controller-gen)internal/monetizeapi/zz_generated.deepcopy.go— generated DeepCopy methods for the rest of the packageinternal/embed/infrastructure/base/templates/*-crd.yaml— regenerated (see diff section below).github/workflows/lint-test.yaml— newgenerate-checkjob: runsjust generateand fails ifgit status --porcelainis non-emptyCRD diff after generation
Bit-exact round-trip wasn't possible because controller-gen normalises layout. The unavoidable differences between hand-written and generated CRDs are:
# ServiceOffer CRD\n# Defines a compute service...). All structural descriptions are preserved asdescription:fields on the matching property; the narrative comments are reproduced as Go doc comments on the corresponding types intypes.go.controller-gen.kubebuilder.io/version: v0.16.5annotation added on every generated CRD — required for CI to detect drift via diff and lets future operators see which controller-gen version produced the YAML.additionalPrinterColumnsbeforename,singularaftershortNames) and uses 2-space indentation throughout. The hand-written files used 4-space and a CRD-conventional ordering. The OpenAPI schemas are semantically identical;kubectl applyis order-insensitive.apiVersion+kind+metadataproperties appear in every CR'sopenAPIV3Schema. controller-gen always emits these to match what apiserver expects; they were elided in the hand-written files. No behaviour change.maxTotal(integer) andmaxSpendPerDay(string) on the Go side. Both fields already existed in the prior hand-written CRD but were silently missing from types.go — this is the bug class the PR exists to close.---separator on the (previously-missing)purchaserequest-crd.yamladded by controller-gen; harmless.No fields were dropped. No validations were loosened. The
^0x[0-9a-fA-F]{40}$pattern onpayTo, theeip3009;permit2enum ontransferMethod, the^/[a-zA-Z0-9/_.-]*$path pattern, the1-65535port range, the1-2500count range, theinference;fine-tuning;http;agenttype enum, all printer columns, and allsubresources.statusdeclarations are preserved.Test plan
just generate(executed as the inlined shell script sincejustisn't installed locally) produces zero diff on a clean re-rungo build ./...cleango test ./internal/embed/...green — the embed CRD parse + schema tests still pass against the regenerated YAMLgo test ./internal/monetizeapi/... ./internal/serviceoffercontroller/... ./internal/x402/... ./internal/x402/buyer/...greengo vet ./...clean (only pre-existinginternal/enclave/enclave_darwin.gounsafe-pointer warnings remain)internal/stackTestWarnIfNoChatModel_EmitsWarnWhenNoModelsfailure verified as pre-existing onorigin/main(unrelated)Future
Unblocks any future CRD changes (e.g.
spec.paused+metav1.Condition) — those become "edit Go markers, runjust generate, commit" instead of hand-editing CRDs twice and drifting.