Skip to content

AsObject's fast path makes GetGeneration() return 0 for every request object #391

Open

Description

@wpbeckwith

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.

Repro (function-sdk-go v0.7.1, also main today):

s, _ := structpb.NewStruct(map[string]any{
	"apiVersion": "v1", "kind": "X",
	"metadata": map[string]any{"name": "a", "generation": 8},
})
c := composed.New()
_ = resource.AsObject(s, c)
fmt.Println(c.GetGeneration())                    // 0
fmt.Printf("%T\n", c.Object["metadata"].(map[string]any)["generation"]) // float64

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.

Activity

  1. blacksoxx commented on Nov 12, 2026

    @blacksoxx

    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.

  2. wpbeckwith commented on Nov 22, 2026

    @wpbeckwith
    Author

    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.

  3. blacksoxx commented on Nov 25, 2026

    @blacksoxx

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions