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

  1. Confirm the indexName string exactly matches an index registered with cache.AddIndexers for this store.
  2. Add the missing AddIndexers call when initializing the CacheStore before any MustByIndex use.
  3. Replace the panic with a checked ByIndex variant if the index name is dynamic or user-supplied.
  4. 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

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


AI-assisted analysis of cilium/cilium@ac7b90affa (2026-08-31). Data as JSON: /api/errors/a1e4274583bbb119. Report an issue: GitHub.