cilium/cilium · critical
failed to retrieve objects by index %q with value %q: %w
Error message
failed to retrieve objects by index %q with value %q: %w
What it means
MustByIndex queries a generic CacheStore via a client-go indexer and, by contract (Must*), panics instead of returning an error when indexer.ByIndex fails. ByIndex itself only errors when the named index does not exist on the indexer, so this panic almost always means an index name typo or a missing AddIndexers registration.
Source
Thrown at pkg/clustermesh/operator/cache_store.go:50
itemAny, exists, _ := s.indexer.Get(obj)
if exists {
item = itemAny.(T)
}
return item, exists
}
func (s *CacheStore[T]) GetByKey(key string) (item T, exists bool) {
itemAny, exists, _ := s.indexer.GetByKey(key)
if exists {
item = itemAny.(T)
}
return item, exists
}
func (s *CacheStore[T]) MustByIndex(indexName, indexedValue string) []T {
items, err := s.indexer.ByIndex(indexName, indexedValue)
if err != nil {
panic(fmt.Errorf("failed to retrieve objects by index %q with value %q: %w", indexName, indexedValue, err))
}
return cslices.Map(items, func(item any) T {
return item.(T)
})
}
func (s *CacheStore[T]) Update(obj T) {
_ = s.indexer.Update(obj)
}
func (s *CacheStore[T]) Delete(obj T) {
_ = s.indexer.Delete(obj)
}
View on GitHub (pinned to ac7b90affa)
Solutions
- Confirm the indexName string exactly matches an index registered with cache.AddIndexers for this store.
- Add the missing AddIndexers call when initializing the CacheStore before any MustByIndex use.
- Replace the panic with a checked ByIndex variant if the index name is dynamic or user-supplied.
- Add a startup-time assertion/unit test that every queried index is registered.
Example fix
// before
items := store.MustByIndex("byService", svcName) // panics: index never registered
// after
err := store.AddIndexers(cache.Indexers{"byService": serviceIndexFunc})
if err != nil { return err }
items := store.MustByIndex("byService", svcName) Defensive patterns
Strategy: validation
Validate before calling
// ensure index registered before use
if _, found := indexer.GetIndexers()["byService"]; !found {
panic("index byService not registered: call AddIndexers first")
} Try / catch
func() (items []T) {
defer func() {
if r := recover(); r != nil {
log.Errorf("MustByIndex failed: %v", r)
items = nil
}
}()
return store.MustByIndex(indexName, value)
}() Prevention
- Define index names as shared constants used by both AddIndexers and MustByIndex.
- Add a startup assertion that every queried index exists in the indexer.
- Prefer an error-returning ByIndex wrapper for any index name that is not compile-time constant.
When it happens
Trigger: Calling MustByIndex with an indexName that was never registered via AddIndexers on the underlying cache.Indexer, or calling it before indexers were added; the wrapped error from ByIndex is embedded in the panic value.
Common situations: Refactoring/renaming an index constant without updating the query site; constructing a CacheStore and forgetting AddIndexers; test code asserting on indexes that production setup code registers.
Related errors
- unexpected object type: %T
- Can't find IP in NPHDS cache
- failed to rename downloaded chart: %w
- Error while creating k8s executor: %w
- error adding to scheme: %w
AI-assisted analysis of cilium/cilium@ac7b90affa (2026-08-31).
Data as JSON: /api/errors/a1e4274583bbb119.
Report an issue: GitHub.