diff --git a/staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher_test.go b/staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher_test.go index 40b1ebad757..8b98016db7f 100644 --- a/staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher_test.go +++ b/staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher_test.go @@ -257,7 +257,7 @@ func TestListResourceVersionMatch(t *testing.T) { } func TestNamespaceScopedList(t *testing.T) { - ctx, cacher, terminate := testSetup(t, withSpecNodeNameIndexerFuncs) + ctx, cacher, terminate := testSetup(t, withNodeNameAndNamespaceIndex) t.Cleanup(terminate) storagetesting.RunTestNamespaceScopedList(ctx, t, cacher) } @@ -355,13 +355,13 @@ func TestWatchInitializationSignal(t *testing.T) { } func TestClusterScopedWatch(t *testing.T) { - ctx, cacher, terminate := testSetup(t, withClusterScopedKeyFunc, withSpecNodeNameIndexerFuncs) + ctx, cacher, terminate := testSetup(t, withClusterScopedKeyFunc, withNodeNameAndNamespaceIndex) t.Cleanup(terminate) storagetesting.RunTestClusterScopedWatch(ctx, t, cacher) } func TestNamespaceScopedWatch(t *testing.T) { - ctx, cacher, terminate := testSetup(t, withSpecNodeNameIndexerFuncs) + ctx, cacher, terminate := testSetup(t, withNodeNameAndNamespaceIndex) t.Cleanup(terminate) storagetesting.RunTestNamespaceScopedWatch(ctx, t, cacher) } @@ -432,7 +432,8 @@ func withClusterScopedKeyFunc(options *setupOptions) { } } -func withSpecNodeNameIndexerFuncs(options *setupOptions) { +// mirror indexer configuration from pkg/registry/core/pod/strategy.go +func withNodeNameAndNamespaceIndex(options *setupOptions) { options.indexerFuncs = map[string]storage.IndexerFunc{ "spec.nodeName": func(obj runtime.Object) string { pod, ok := obj.(*example.Pod) @@ -447,6 +448,10 @@ func withSpecNodeNameIndexerFuncs(options *setupOptions) { pod := obj.(*example.Pod) return []string{pod.Spec.NodeName}, nil }, + "f:metadata.namespace": func(obj interface{}) ([]string, error) { + pod := obj.(*example.Pod) + return []string{pod.ObjectMeta.Namespace}, nil + }, } } @@ -571,7 +576,7 @@ func BenchmarkStoreCreateList(b *testing.B) { b.Run(fmt.Sprintf("Indexed=%v", useIndex), func(b *testing.B) { opts := []setupOption{} if useIndex { - opts = append(opts, withSpecNodeNameIndexerFuncs) + opts = append(opts, withNodeNameAndNamespaceIndex) } ctx, cacher, _, terminate := testSetupWithEtcdServer(b, opts...) b.Cleanup(terminate) @@ -627,7 +632,7 @@ func BenchmarkStoreList(b *testing.B) { for _, store := range storeOptions { b.Run(fmt.Sprintf("Store=%s", store.name), func(b *testing.B) { featuregatetesting.SetFeatureGateDuringTest(b, utilfeature.DefaultFeatureGate, features.BtreeWatchCache, store.btreeEnabled) - ctx, cacher, _, terminate := testSetupWithEtcdServer(b, withSpecNodeNameIndexerFuncs) + ctx, cacher, _, terminate := testSetupWithEtcdServer(b, withNodeNameAndNamespaceIndex) b.Cleanup(terminate) var out example.Pod for _, pod := range data.Pods { diff --git a/staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher_whitebox_test.go b/staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher_whitebox_test.go index 961935d3b1e..60ea41f959a 100644 --- a/staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher_whitebox_test.go +++ b/staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher_whitebox_test.go @@ -2945,7 +2945,7 @@ func forceRequestWatchProgressSupport(t *testing.T) { } func TestListIndexer(t *testing.T) { - ctx, cacher, terminate := testSetup(t, withSpecNodeNameIndexerFuncs) + ctx, cacher, terminate := testSetup(t, withNodeNameAndNamespaceIndex) t.Cleanup(terminate) tests := []struct { name string diff --git a/staging/src/k8s.io/apiserver/pkg/storage/testing/store_benchmarks.go b/staging/src/k8s.io/apiserver/pkg/storage/testing/store_benchmarks.go index 039e9f44509..fe3e526b197 100644 --- a/staging/src/k8s.io/apiserver/pkg/storage/testing/store_benchmarks.go +++ b/staging/src/k8s.io/apiserver/pkg/storage/testing/store_benchmarks.go @@ -29,6 +29,7 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/util/rand" "k8s.io/apiserver/pkg/apis/example" + "k8s.io/apiserver/pkg/endpoints/request" "k8s.io/apiserver/pkg/storage" ) @@ -149,6 +150,11 @@ func runBenchmarkStoreList(ctx context.Context, b *testing.B, store storage.Inte objectCount.Add(uint64(objects)) pageCount.Add(uint64(pages)) case namespace: + ctx := ctx + if useIndex { + opts.Predicate.IndexFields = []string{"metadata.namespace"} + ctx = request.WithRequestInfo(ctx, &request.RequestInfo{Namespace: namespaceName}) + } objects, pages := paginateList(ctx, store, "/pods/"+namespaceName, opts) objectCount.Add(uint64(objects)) pageCount.Add(uint64(pages))