You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
AsObject's fast path makes GetGeneration() return 0 for every request object #391
GetGeneration() returns 0 for every object a function reads from its request: the observed XR, observed composed resources and required resources.
Cause:resource.AsObject takes the fast path for unstructured types:
u.SetUnstructuredContent(s.AsMap())
structpb.Struct.AsMap() returns every number as float64. apimachinery's GetGeneration() uses NestedInt64, which returns 0 for anything that isn't an int64. Other int64 metadata getters, like GetDeletionGracePeriodSeconds, have the same problem.
Conditions aren't affected, because GetCondition goes through a JSON conversion. So the generation reads 0 while a condition's observedGeneration reads correctly, and any observedGeneration == generation check in a function silently fails.
Live, on Crossplane v2.4.2: every composed resource in our function's requests had GetGeneration() == 0. Each one's Synced condition had observedGeneration equal to the real metadata.generation on the API server.
Possible fixes:
Convert whole-number float64s to int64 on the fast path. That matches how apimachinery's unstructured JSON decoding reads whole numbers.
Or always take the protojson + JSON path for unstructured, at some cost.
Our workaround reads metadata.generation with a type switch on float64/int64.
I would like to work on this. The fast path can convert whole-number float64s to int64 after AsMap, which is what apimachinery's JSON decoding produces, so GetGeneration and the other int64 accessors read the right value without giving up the fast path.
Thanks for picking this up, @blacksoxx. I agree with the fast-path conversion over the protojson round-trip. The round-trip is correct by construction but gives up the optimization the comment in AsObject is there to keep.
A few things I'd want the fix to cover:
Recurse through the whole map. Nested maps and slices need converting too, not just metadata. For example, an int inside a list of maps should come out as int64.
Convert only exactly integral values within int64 range. Fractional values and anything out of range stay float64. Values above 2^53 have already lost precision in structpb, so converting them can't recover anything.
Match apimachinery's decoder. It yields int64 for whole numbers and float64 otherwise, so a CRD float field holding 1 would come back as int64. That is the same ambiguity a normal client-go read has, but it's worth a line in the PR description.
Check the other AsMap() call sites. If any feed unstructured content elsewhere in the SDK, they have the same bug.
Tests I'd suggest:
The repro from this issue, as a regression test.
A table covering generation, deletionGracePeriodSeconds, and a nested int in a list of maps.
0.5 stays float64.
1<<62 doesn't overflow or panic.
AsStruct(AsObject(s)) is stable, so generation: 8 doesn't change type on the way back out.
Until this lands, reading metadata.generation with a float64/int64 type switch works as a workaround.
Thanks for the list. #392 already recursed through nested maps and lists, and AsMap has no other call site in the SDK. Your range point caught a real bug: float64(math.MaxInt64) rounds up to 2^63, so 2^63 passed the check and overflowed. adb7b51 fixes the bound and adds the cases you suggested (deletionGracePeriodSeconds, an int in a list of maps, 1<<62, 2^63 stays float64, the AsStruct round trip). The float-field ambiguity is now in the PR description.
Activity
blacksoxx commentedon Nov 12, 2026
I would like to work on this. The fast path can convert whole-number float64s to int64 after
AsMap, which is what apimachinery's JSON decoding produces, soGetGenerationand the other int64 accessors read the right value without giving up the fast path.wpbeckwith commentedon Nov 22, 2026
Thanks for picking this up, @blacksoxx. I agree with the fast-path conversion over the protojson round-trip. The round-trip is correct by construction but gives up the optimization the comment in
AsObjectis there to keep.A few things I'd want the fix to cover:
metadata. For example, an int inside a list of maps should come out asint64.float64. Values above 2^53 have already lost precision instructpb, so converting them can't recover anything.int64for whole numbers andfloat64otherwise, so a CRD float field holding1would come back asint64. That is the same ambiguity a normal client-go read has, but it's worth a line in the PR description.AsMap()call sites. If any feed unstructured content elsewhere in the SDK, they have the same bug.Tests I'd suggest:
generation,deletionGracePeriodSeconds, and a nested int in a list of maps.0.5staysfloat64.1<<62doesn't overflow or panic.AsStruct(AsObject(s))is stable, sogeneration: 8doesn't change type on the way back out.Until this lands, reading
metadata.generationwith afloat64/int64type switch works as a workaround.blacksoxx commentedon Nov 25, 2026
Thanks for the list. #392 already recursed through nested maps and lists, and
AsMaphas no other call site in the SDK. Your range point caught a real bug:float64(math.MaxInt64)rounds up to 2^63, so 2^63 passed the check and overflowed. adb7b51 fixes the bound and adds the cases you suggested (deletionGracePeriodSeconds, an int in a list of maps,1<<62, 2^63 staysfloat64, theAsStructround trip). The float-field ambiguity is now in the PR description.