client-go icon indicating copy to clipboard operation
client-go copied to clipboard

Informers: cache Indexer interface to support generics

Open ivankatliarchuk opened this issue 6 months ago • 0 comments

Currently cache Indexer https://github.com/kubernetes/client-go/blob/212c32462b4ebbd9e33874d3ad062ffcc8a3352c/tools/cache/index.go#L35-L55 return interface. Would be quite handy to add support for Generics.

Current approach

services, err := svcInformer.Informer().GetIndexer().ByIndex(informers.SpecSelectorIndex, labels.Set(selector).String())

for _, svc := range services {
		service, ok := svc.(*corev1.Service)
		if !ok {
			continue
		}
     ....
}

Proposed approach

services, err := svcInformer.Informer().GetIndexer().ByIndex[*corev1.Service](informers.SpecSelectorIndex, labels.Set(selector).String())

for _, service := range services {

     ....service.Name....
}

Please share feedback whether this feature is considered valuable. If so, I'm happy to implement it, may just need a direction or steps

ivankatliarchuk avatar Jun 18 '25 08:06 ivankatliarchuk