Skip to content

client: CacheReader returns typed objects with empty GVK when UnsafeDisableDeepCopy is enabled #3606

Closed

Description

@stillya

Description

CacheReader restores an object's GroupVersionKind only on deep-copy. With UnsafeDisableDeepCopy enabled option, typed objects come back from Get and List with an empty TypeMeta.

Code that switches on the GVK (webhooks, generic reconcilers, kind-keyed event handlers) then
misbehaves silently, and only for users who turned the option on for performance(that's my case in EG).

How to reproduce

c, _ := cache.New(cfg, cache.Options{DefaultUnsafeDisableDeepCopy: ptr.To(true)})
out := &corev1.Pod{}
_ = c.Get(ctx, key, out)
fmt.Println(out.GetObjectKind().GroupVersionKind()) // it's empaty

list := &corev1.PodList{}
_ = c.List(ctx, list)
fmt.Println(list.Items[0].GroupVersionKind()) // empty as well

Why it happens

Two independent things have to line up:

  1. The GVK is stripped at decode. runtime.WithoutVersionDecoder clears the GVK after deserialization apimachinery/pkg/runtime/helper.go:255-262, "clearing the gvk is just a convention of a codec"). I could guess it's because TypeMeta creates a second representation of the object's identity that could conflict with the instantiated object.
  2. CacheReader puts it back only when it deep-copies - pkg/cache/internal/cache_reader.go:101-103 for Get, :167-176 for List.

Disable deep copy and nothing gonna restore it(but It affects only structured informers)

Suggested fix

Set GVK in Get and List whenever it is absent, rather than only on the deep-copy branch.
The skip was never needed for safety: Get shallow-copies into the caller's out before the guarded set, and for a typed object TypeMeta is a value field of that copy; after apimeta.SetList the items of a concretely typed Items slice are likewise copies.

But there's a couple of corner-cases.

  1. Unstructured must not be written to. SetGroupVersionKind on it is unconditional map write, so an unguarded set would mutate live cache state. Also like i said only structured informers affected because unstructured always arrives with apiVersion/kind populated.
  2. Some list shapes hold the cache's own objects. So we gonna skip them

Environment

  • controller-runtime: main
  • k8s.io/{api,apimachinery,client-go}: v0.37.0
  • Go: 1.26

Activity

  1. stillya commented on Sep 28, 2026

    @stillya
    Author

    ready to contribute if you consider it relevant.

  2. alvaroaleman commented on Sep 30, 2026

    @alvaroaleman
    Member

    If anything, the bug here is that its ever set on a typed object. Use apiutil.GVKForObject if you want to get the GVK for an object.

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