mirror of
https://github.com/kubernetes/sample-controller.git
synced 2026-08-03 00:00:02 +08:00
All code using the result of the generated client-go Informer() (a
cache.SharedIndexInformer) constantly has to do type casts from "any" to the
actual type of the objects managed by the informer.
This is:
- annoying at best
- often done inconsistently (should failed type assertions be logged and if so,
how?)
- a source of bugs (not handling FinalStateUnknown, not handling missing
object, type in event handler not matching the type in the informer)
In contrast, the Lister() result *is* typed. It converts without a type check
in e.g. ResourceIndexer[T].List:
err = cache.ListAllByNamespace(l.indexer, l.namespace, selector, func(m interface{}) {
ret = append(ret, m.(T))
})
This change here does the same wrapping for SharedIndexInformer, Indexer,
index functions and event handler support code. OnDelete is passed
a DeletedObject struct to cover the different scenarios that can occur
when reporting deletion (stale or even nil object!). Extracting key or
name from DeletedObject is guaranteed to never fail because the type-safe
API is restricted to types where the necessary meta data is guaranteed
to be available. This removes another class of tedious error checking.
While not strictly needed, aliases are generated for the generic types because
they are often shorter, easier to type (in particular with auto-complete) and
may help with avoiding an import of k8s.io/client-go/tools/cache.
This is a Go API break because all generated interfaces
change.
Kubernetes-commit: b83b3b39db5eee75af14cdb8d30cfc6899792ec2