Make generated Go models satisfy client.Object/client.ObjectList - #388
Open
erikmiller-gusto wants to merge 1 commit into
Open
erikmiller-gusto wants to merge 1 commit into
erikmiller-gusto wants to merge 1 commit into
Conversation
features.generateGoRuntimeObjects currently only makes generated root types implement runtime.Object, which isn't enough for sigs.k8s.io/controller-runtime's client.Client. client.Object also requires metav1.Object, whose method signatures are fixed by apimachinery (types.UID, metav1.Time, []metav1.OwnerReference, ...) - signatures the locally-generated ObjectMeta mirror type can't satisfy. This redirects io.k8s.apimachinery.pkg.apis.meta.v1 to the real k8s.io/apimachinery/pkg/apis/meta/v1 package when the flag is on, instead of generating a local mirror, and generates metav1.Object methods on root types and metav1.ListInterface methods on <Kind>List types delegating to the real Metadata field. It also fixes two pre-existing bugs surfaced along the way: - <Kind>List.Items was generated as *[]Kind (pointer-to-slice). k8s.io/apimachinery/pkg/api/meta's GetItemsPtr, used internally by client.List, the fake client, and the informer cache, requires a literal slice. - collectStructTypes didn't resolve `type A = B` aliases back to the underlying struct, so a List's Items element (always a qualified-name alias for the root type) was treated as a scalar and DeepCopy fell back to a shallow copy that shared item pointers with the original. Also registers metav1.AddToGroupVersion so client.List with query options and client.Watch work, and updates the generated module's go.mod/go.sum for the transitive dependencies the real apimachinery meta/v1 package needs. Breaking change to generateGoRuntimeObjects's output, documented in cmd/crossplane/config/help/config.md. The flag is off by default and wasn't usable as a client.Object before this, so there's no migration path beyond regenerating models. Signed-off-by: Erik Miller <erik.miller@gusto.com>
erikmiller-gusto
force-pushed
the
erik.miller--client-object-pr
branch
from
September 25, 2026 15:21
91eefee to
383fbf7
Compare
erikmiller-gusto
marked this pull request as ready for review
September 25, 2026 16:10
erikmiller-gusto
requested review from
a team,
jcogilvie and
tampakrap
as code owners
September 25, 2026 16:10
erikmiller-gusto
requested review from
haarchri
and removed request for
a team
September 25, 2026 16:10
Contributor
|
Warning Review limit reachedNext included review available in 47 minutes. View limit detailsLimit details: You’ve used the included review currently available. You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Review configuration: ⚙️ Run configurationConfiguration used: Repository: crossplane/cli/.coderabbit.yaml Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (9)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
Description of your changes
features.generateGoRuntimeObjects(off by default) makes generated root types implementruntime.Object, but that's not enough to use them withsigs.k8s.io/controller-runtime'sclient.Client.client.Objectalso requiresmetav1.Object, whose method signatures are fixed by apimachinery (types.UID,metav1.Time,[]metav1.OwnerReference, ...). The locally-generatedObjectMetamirror type can't satisfy those signatures no matter what methods get added to the root type.This PR:
io.k8s.apimachinery.pkg.apis.meta.v1to the realk8s.io/apimachinery/pkg/apis/meta/v1package when the flag is on, instead of generating a local mirror.Metadata's Go type becomes the realObjectMeta/ListMeta.metav1.Objectmethods on root types andmetav1.ListInterfacemethods on<Kind>Listtypes, delegating to the realMetadatafield.<Kind>List.Items, generated as*[]Kind(pointer-to-slice).k8s.io/apimachinery/pkg/api/meta'sGetItemsPtr, used byclient.List, the fake client, and the informer cache, requires a literal slice.metav1.AddToGroupVersionsoclient.Listwith query options andclient.Watchwork.collectStructTypes, which didn't resolvetype A = Baliases back to the underlying struct. A<Kind>List'sItemselement is always a qualified-name alias for the root type (type CoAcmePlatformV1Alpha1Foo = Foo), so it was treated as a scalar andDeepCopyfell back to a shallow copy that shared item pointers with the original.go.mod/go.sum. The realk8s.io/apimachinery/pkg/apis/meta/v1package pulls in transitive dependencies (golang.org/x/net,golang.org/x/text,gopkg.in/inf.v0,k8s.io/utils,sigs.k8s.io/randfill) nothing previously imported, captured from a realgo mod tidyrun.Breaking change to
generateGoRuntimeObjects's output, documented incmd/crossplane/config/help/config.md. The flag is off by default and wasn't usable as aclient.Objectbefore this, so there's no migration path beyond regenerating models.Tested with:
internal/schemas/generator/runtimeobject_test.gocovering the metav1.Object/ListInterface method split, the Items fix, and the alias-resolution fix.-tags compilegate) that builds a separate consumer module requiringsigs.k8s.io/controller-runtimevia areplacedirective at the materialized models directory, so controller-runtime never touches the generated module's owngo.mod. It exercises a real fake client (Create/Get/List), aDeepCopyindependence check against the real generatedXAccountScaffoldListtype, and aruntime.NewParameterCodecround-trip proving theAddToGroupVersionregistration.Opened as a draft for early feedback before marking ready for review.
I have:
./nix.sh flake checkto ensure this PR is ready for review.backport release-x.ylabels to auto-backport this PR.