Client and DynamicRESTMapper keep a kind's old scope after its CRD is recreated with a different `scope`

Abierto
#3,671 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

@AdamMagued ya está trabajando en esto.

Desde el 27/11/2026.

  • #3701 de @AdamMagued — abierto

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
45/100
Tipo de issue
Nueva funcionalidad
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
go, kubernetes

Línea de trabajo

Start in pkg/client/apiutil/restmapper.go to see how a group's mappings are cached and only reloaded on NoMatch, and check whether meta.ResettableRESTMapper can be satisfied there. Then read getResource in pkg/client/client_rest_resources.go to see how resourceByType pins a GVK's resourceMeta for the client's lifetime and how a mapper generation or per-GVK eviction could make it re-resolve. Done means a CRD recreated with a different spec.scope is picked up by a client sharing the invalidated mapper, with tests covering both Cluster→Namespaced and the reverse.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

(re-raising #3303)

Problem

#3303 described this precisely and closed as rotten with no fix. We hit it independently, as did Crossplane users
(crossplane/crossplane#6736), so we are re-raising it with a concrete, small proposal.

When a CRD is deleted and recreated under the same GroupVersionKind with a different spec.scope (Cluster ↔
Namespaced), a long-running controller-runtime client keeps addressing the old path until the process restarts:

  • client.Client caches resourceMeta, mapping included, per GVK in resourceByType and never invalidates it
    (pkg/client/client_rest_resources.go, getResource).
  • DynamicRESTMapper reloads a group only on NoMatch (pkg/client/apiutil/restmapper.go). A recreate keeps the kind
    matching, so the stale scope is never reloaded.

Symptoms: writes to a now-namespaced kind go to the cluster path and 404 ("the server could not find the requested
resource"). The reverse direction fails with "an empty namespace may not be set when a resource name is provided".
Informers built from the mapper inherit the wrong scope too.

This is an edge case, since scope is effectively immutable. But it is the ONLY way to change scope, and systems that
manage CRDs programmatically (Crossplane XRDs, operators that generate CRDs) do it at runtime.

Proposal (small, opt-in)
  1. Mapper invalidation. Add to the dynamic RESTMapper a way to drop one group's cached mappings, so that the next
    lookup reloads it. For example, an exported InvalidateGroup(group string), or Reset() for the whole mapper,
    satisfying meta.ResettableRESTMapper (some tooling already expects that interface).
  2. Client follows the mapper. Either key resourceByType entries to a mapper generation that bumps on every reload
    or invalidation, so a stale entry re-resolves, or have the client drop its entry for a GVK when the mapper is
    invalidated for that GVK's group.

With (1) and (2), a controller that watches its CRDs, as Crossplane's XRD reconciler does, can call
InvalidateGroup on a recreate, and every client sharing that mapper follows. Nothing changes for anyone who never
calls it. That answers the "not too invasive" bar from #3303.

The watch question from #3303: informers created after the invalidation use the new scope. Already-running informers
for the old scope must be torn down by their owner, as they already are when a CRD is deleted. The mapper change only
guarantees that the new ones are right.

Reproduction

Reproduced through Crossplane, which changes XRD scope this way: on Crossplane v2.5.0-rc.1 and v2.4.3 (Kubernetes v1.37.0, kind), after an XRD is deleted and recreated with scope: Namespaced, a new XR gets no finalizer and no conditions for as long as the process runs (checked at 30, 60 and 120 s), and is reconciled within seconds of a restart. A Crossplane issue with the full output will link here. A plain-CRD version:
create a CRD with scope Cluster; start a manager and Get or List the kind; delete the CRD; recreate it with scope
Namespaced; Create a namespaced object through the same client and observe the 404 at the cluster path; restart and
observe success.

We are willing to send the PR.

This issue was drafted with AI assistance (Claude) from our own observation and a reading of the controller-runtime
source; a human reviewed it before posting.

Lenguaje dominante
Go
Estrellas
3k
Forks
1.3k
Merge medio
1 d 7 h
PR fusionados (30 d)
48

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de kubernetes-sigs/controller-runtime

Todos los issues de kubernetes-sigs/controller-runtime

Issues similares

Más issues de Go

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.