Fix nil check for store metrics

This commit is contained in:
Michael Aspinwall 2026-05-18 18:19:58 +00:00
parent 9991d72c6b
commit e50a7f3dbc
2 changed files with 20 additions and 0 deletions

View file

@ -402,6 +402,9 @@ func WithTransformer(transformer TransformFunc) StoreOption {
func WithStoreMetrics(identifier InformerNameAndResource, metrics InformerMetricsProvider) StoreOption {
return func(c *cache) {
c.identifier = identifier
if metrics == nil {
metrics = globalInformerMetricsProvider
}
c.metrics = metrics
}
}

View file

@ -22,6 +22,7 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets"
)
@ -246,3 +247,19 @@ func TestCacheTransactionShouldIndexErrors(t *testing.T) {
})
}
}
func TestWithStoreMetricsDefaulting(t *testing.T) {
name, err := NewInformerName("test-informer")
if err != nil {
t.Fatalf("failed to create informer name: %v", err)
}
defer name.Release()
gvr := schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "deployments"}
id := name.WithResource(gvr)
s := NewStore(testStoreKeyFunc, WithStoreMetrics(id, nil)).(*cache)
if s.metrics == nil {
t.Errorf("expected c.metrics to be populated, got nil")
}
}