| 1 |
/* |
| 2 |
Copyright 2015 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package cacher |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"fmt" |
| 22 |
"testing" |
| 23 |
"time" |
| 24 |
|
| 25 |
"github.com/go-logr/logr" |
| 26 |
|
| 27 |
apiequality "k8s.io/apimachinery/pkg/api/equality" |
| 28 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 29 |
"k8s.io/apimachinery/pkg/fields" |
| 30 |
"k8s.io/apimachinery/pkg/labels" |
| 31 |
"k8s.io/apimachinery/pkg/runtime" |
| 32 |
"k8s.io/apimachinery/pkg/runtime/schema" |
| 33 |
utilruntime "k8s.io/apimachinery/pkg/util/runtime" |
| 34 |
"k8s.io/apimachinery/pkg/util/wait" |
| 35 |
"k8s.io/apiserver/pkg/apis/example" |
| 36 |
examplev1 "k8s.io/apiserver/pkg/apis/example/v1" |
| 37 |
"k8s.io/apiserver/pkg/features" |
| 38 |
"k8s.io/apiserver/pkg/storage" |
| 39 |
etcd3testing "k8s.io/apiserver/pkg/storage/etcd3/testing" |
| 40 |
storagetesting "k8s.io/apiserver/pkg/storage/testing" |
| 41 |
utilfeature "k8s.io/apiserver/pkg/util/feature" |
| 42 |
"k8s.io/client-go/tools/cache" |
| 43 |
featuregatetesting "k8s.io/component-base/featuregate/testing" |
| 44 |
"k8s.io/klog/v2" |
| 45 |
"k8s.io/utils/clock" |
| 46 |
) |
| 47 |
|
| 48 |
func init() { |
| 49 |
metav1.AddToGroupVersion(scheme, metav1.SchemeGroupVersion) |
| 50 |
utilruntime.Must(example.AddToScheme(scheme)) |
| 51 |
utilruntime.Must(examplev1.AddToScheme(scheme)) |
| 52 |
} |
| 53 |
|
| 54 |
// GetPodAttrs returns labels and fields of a given object for filtering purposes. |
| 55 |
func GetPodAttrs(obj runtime.Object) (labels.Set, fields.Set, error) { |
| 56 |
pod, ok := obj.(*example.Pod) |
| 57 |
if !ok { |
| 58 |
return nil, nil, fmt.Errorf("not a pod") |
| 59 |
} |
| 60 |
return labels.Set(pod.ObjectMeta.Labels), PodToSelectableFields(pod), nil |
| 61 |
} |
| 62 |
|
| 63 |
// PodToSelectableFields returns a field set that represents the object |
| 64 |
// TODO: fields are not labels, and the validation rules for them do not apply. |
| 65 |
func PodToSelectableFields(pod *example.Pod) fields.Set { |
| 66 |
// The purpose of allocation with a given number of elements is to reduce |
| 67 |
// amount of allocations needed to create the fields.Set. If you add any |
| 68 |
// field here or the number of object-meta related fields changes, this should |
| 69 |
// be adjusted. |
| 70 |
podSpecificFieldsSet := make(fields.Set, 5) |
| 71 |
podSpecificFieldsSet["spec.nodeName"] = pod.Spec.NodeName |
| 72 |
podSpecificFieldsSet["spec.restartPolicy"] = string(pod.Spec.RestartPolicy) |
| 73 |
podSpecificFieldsSet["status.phase"] = string(pod.Status.Phase) |
| 74 |
return AddObjectMetaFieldsSet(podSpecificFieldsSet, &pod.ObjectMeta, true) |
| 75 |
} |
| 76 |
|
| 77 |
func AddObjectMetaFieldsSet(source fields.Set, objectMeta *metav1.ObjectMeta, hasNamespaceField bool) fields.Set { |
| 78 |
source["metadata.name"] = objectMeta.Name |
| 79 |
if hasNamespaceField { |
| 80 |
source["metadata.namespace"] = objectMeta.Namespace |
| 81 |
} |
| 82 |
return source |
| 83 |
} |
| 84 |
|
| 85 |
func checkStorageInvariants(ctx context.Context, t *testing.T, key string) { |
| 86 |
// No-op function since cacher simply passes object creation to the underlying storage. |
| 87 |
} |
| 88 |
|
| 89 |
func TestCreate(t *testing.T) { |
| 90 |
ctx, cacher, terminate := testSetup(t) |
| 91 |
t.Cleanup(terminate) |
| 92 |
storagetesting.RunTestCreate(ctx, t, cacher, checkStorageInvariants) |
| 93 |
} |
| 94 |
|
| 95 |
func TestCreateWithTTL(t *testing.T) { |
| 96 |
ctx, cacher, terminate := testSetup(t) |
| 97 |
t.Cleanup(terminate) |
| 98 |
storagetesting.RunTestCreateWithTTL(ctx, t, cacher) |
| 99 |
} |
| 100 |
|
| 101 |
func TestCreateWithKeyExist(t *testing.T) { |
| 102 |
ctx, cacher, terminate := testSetup(t) |
| 103 |
t.Cleanup(terminate) |
| 104 |
storagetesting.RunTestCreateWithKeyExist(ctx, t, cacher) |
| 105 |
} |
| 106 |
|
| 107 |
func TestGet(t *testing.T) { |
| 108 |
ctx, cacher, terminate := testSetup(t) |
| 109 |
t.Cleanup(terminate) |
| 110 |
storagetesting.RunTestGet(ctx, t, cacher) |
| 111 |
} |
| 112 |
|
| 113 |
func TestUnconditionalDelete(t *testing.T) { |
| 114 |
ctx, cacher, terminate := testSetup(t) |
| 115 |
t.Cleanup(terminate) |
| 116 |
storagetesting.RunTestUnconditionalDelete(ctx, t, cacher) |
| 117 |
} |
| 118 |
|
| 119 |
func TestConditionalDelete(t *testing.T) { |
| 120 |
ctx, cacher, terminate := testSetup(t) |
| 121 |
t.Cleanup(terminate) |
| 122 |
storagetesting.RunTestConditionalDelete(ctx, t, cacher) |
| 123 |
} |
| 124 |
|
| 125 |
func TestDeleteWithSuggestion(t *testing.T) { |
| 126 |
ctx, cacher, terminate := testSetup(t) |
| 127 |
t.Cleanup(terminate) |
| 128 |
storagetesting.RunTestDeleteWithSuggestion(ctx, t, cacher) |
| 129 |
} |
| 130 |
|
| 131 |
func TestDeleteWithSuggestionAndConflict(t *testing.T) { |
| 132 |
ctx, cacher, terminate := testSetup(t) |
| 133 |
t.Cleanup(terminate) |
| 134 |
storagetesting.RunTestDeleteWithSuggestionAndConflict(ctx, t, cacher) |
| 135 |
} |
| 136 |
|
| 137 |
func TestDeleteWithSuggestionOfDeletedObject(t *testing.T) { |
| 138 |
ctx, cacher, terminate := testSetup(t) |
| 139 |
t.Cleanup(terminate) |
| 140 |
storagetesting.RunTestDeleteWithSuggestionOfDeletedObject(ctx, t, cacher) |
| 141 |
} |
| 142 |
|
| 143 |
func TestValidateDeletionWithSuggestion(t *testing.T) { |
| 144 |
ctx, cacher, terminate := testSetup(t) |
| 145 |
t.Cleanup(terminate) |
| 146 |
storagetesting.RunTestValidateDeletionWithSuggestion(ctx, t, cacher) |
| 147 |
} |
| 148 |
|
| 149 |
func TestValidateDeletionWithOnlySuggestionValid(t *testing.T) { |
| 150 |
ctx, cacher, terminate := testSetup(t) |
| 151 |
t.Cleanup(terminate) |
| 152 |
storagetesting.RunTestValidateDeletionWithOnlySuggestionValid(ctx, t, cacher) |
| 153 |
} |
| 154 |
|
| 155 |
func TestDeleteWithConflict(t *testing.T) { |
| 156 |
ctx, cacher, terminate := testSetup(t) |
| 157 |
t.Cleanup(terminate) |
| 158 |
storagetesting.RunTestDeleteWithConflict(ctx, t, cacher) |
| 159 |
} |
| 160 |
|
| 161 |
func TestPreconditionalDeleteWithSuggestion(t *testing.T) { |
| 162 |
ctx, cacher, terminate := testSetup(t) |
| 163 |
t.Cleanup(terminate) |
| 164 |
storagetesting.RunTestPreconditionalDeleteWithSuggestion(ctx, t, cacher) |
| 165 |
} |
| 166 |
|
| 167 |
func TestPreconditionalDeleteWithSuggestionPass(t *testing.T) { |
| 168 |
ctx, cacher, terminate := testSetup(t) |
| 169 |
t.Cleanup(terminate) |
| 170 |
storagetesting.RunTestPreconditionalDeleteWithOnlySuggestionPass(ctx, t, cacher) |
| 171 |
} |
| 172 |
|
| 173 |
func TestListPaging(t *testing.T) { |
| 174 |
ctx, cacher, terminate := testSetup(t) |
| 175 |
t.Cleanup(terminate) |
| 176 |
storagetesting.RunTestListPaging(ctx, t, cacher) |
| 177 |
} |
| 178 |
|
| 179 |
func TestLists(t *testing.T) { |
| 180 |
for _, consistentRead := range []bool{true, false} { |
| 181 |
for _, listFromCacheSnapshot := range []bool{true, false} { |
| 182 |
t.Run(fmt.Sprintf("ConsistentListFromCache=%v,ListFromCacheSnapshot=%v", consistentRead, listFromCacheSnapshot), func(t *testing.T) { |
| 183 |
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ListFromCacheSnapshot, listFromCacheSnapshot) |
| 184 |
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ConsistentListFromCache, consistentRead) |
| 185 |
t.Run("List", func(t *testing.T) { |
| 186 |
t.Parallel() |
| 187 |
ctx, cacher, server, terminate := testSetupWithEtcdServer(t) |
| 188 |
t.Cleanup(terminate) |
| 189 |
storagetesting.RunTestList(ctx, t, cacher, increaseRV(server.V3Client.Client), true) |
| 190 |
}) |
| 191 |
|
| 192 |
t.Run("ConsistentList", func(t *testing.T) { |
| 193 |
t.Parallel() |
| 194 |
ctx, cacher, server, terminate := testSetupWithEtcdServer(t) |
| 195 |
t.Cleanup(terminate) |
| 196 |
storagetesting.RunTestConsistentList(ctx, t, cacher, increaseRV(server.V3Client.Client), true, consistentRead, listFromCacheSnapshot) |
| 197 |
}) |
| 198 |
|
| 199 |
t.Run("GetListNonRecursive", func(t *testing.T) { |
| 200 |
t.Parallel() |
| 201 |
ctx, cacher, server, terminate := testSetupWithEtcdServer(t) |
| 202 |
t.Cleanup(terminate) |
| 203 |
storagetesting.RunTestGetListNonRecursive(ctx, t, increaseRV(server.V3Client.Client), cacher) |
| 204 |
}) |
| 205 |
}) |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
func TestGetListRecursivePrefix(t *testing.T) { |
| 211 |
ctx, store, terminate := testSetup(t) |
| 212 |
t.Cleanup(terminate) |
| 213 |
storagetesting.RunTestGetListRecursivePrefix(ctx, t, store) |
| 214 |
} |
| 215 |
|
| 216 |
func checkStorageCalls(t *testing.T, pageSize, estimatedProcessedObjects uint64) { |
| 217 |
// No-op function for now, since cacher passes pagination calls to underlying storage. |
| 218 |
} |
| 219 |
|
| 220 |
func TestListContinuation(t *testing.T) { |
| 221 |
ctx, cacher, terminate := testSetup(t) |
| 222 |
t.Cleanup(terminate) |
| 223 |
storagetesting.RunTestListContinuation(ctx, t, cacher, checkStorageCalls) |
| 224 |
} |
| 225 |
|
| 226 |
func TestListPaginationRareObject(t *testing.T) { |
| 227 |
ctx, cacher, terminate := testSetup(t) |
| 228 |
t.Cleanup(terminate) |
| 229 |
storagetesting.RunTestListPaginationRareObject(ctx, t, cacher, checkStorageCalls) |
| 230 |
} |
| 231 |
|
| 232 |
func TestListContinuationWithFilter(t *testing.T) { |
| 233 |
ctx, cacher, terminate := testSetup(t) |
| 234 |
t.Cleanup(terminate) |
| 235 |
storagetesting.RunTestListContinuationWithFilter(ctx, t, cacher, checkStorageCalls) |
| 236 |
} |
| 237 |
|
| 238 |
func TestListInconsistentContinuation(t *testing.T) { |
| 239 |
// TODO(#109831): Enable use of this by setting compaction. |
| 240 |
} |
| 241 |
|
| 242 |
func TestListResourceVersionMatch(t *testing.T) { |
| 243 |
// TODO(#109831): Enable use of this test and run it. |
| 244 |
} |
| 245 |
|
| 246 |
func TestNamespaceScopedList(t *testing.T) { |
| 247 |
ctx, cacher, terminate := testSetup(t, withNodeNameAndNamespaceIndex) |
| 248 |
t.Cleanup(terminate) |
| 249 |
storagetesting.RunTestNamespaceScopedList(ctx, t, cacher) |
| 250 |
} |
| 251 |
|
| 252 |
func TestGuaranteedUpdate(t *testing.T) { |
| 253 |
// TODO(#109831): Enable use of this test and run it. |
| 254 |
} |
| 255 |
|
| 256 |
func TestGuaranteedUpdateWithTTL(t *testing.T) { |
| 257 |
ctx, cacher, terminate := testSetup(t) |
| 258 |
t.Cleanup(terminate) |
| 259 |
storagetesting.RunTestGuaranteedUpdateWithTTL(ctx, t, cacher) |
| 260 |
} |
| 261 |
|
| 262 |
func TestGuaranteedUpdateChecksStoredData(t *testing.T) { |
| 263 |
// TODO(#109831): Enable use of this test and run it. |
| 264 |
} |
| 265 |
|
| 266 |
func TestGuaranteedUpdateWithConflict(t *testing.T) { |
| 267 |
ctx, cacher, terminate := testSetup(t) |
| 268 |
t.Cleanup(terminate) |
| 269 |
storagetesting.RunTestGuaranteedUpdateWithConflict(ctx, t, cacher) |
| 270 |
} |
| 271 |
|
| 272 |
func TestGuaranteedUpdateWithSuggestionAndConflict(t *testing.T) { |
| 273 |
ctx, cacher, terminate := testSetup(t) |
| 274 |
t.Cleanup(terminate) |
| 275 |
storagetesting.RunTestGuaranteedUpdateWithSuggestionAndConflict(ctx, t, cacher) |
| 276 |
} |
| 277 |
|
| 278 |
func TestTransformationFailure(t *testing.T) { |
| 279 |
// TODO(#109831): Enable use of this test and run it. |
| 280 |
} |
| 281 |
|
| 282 |
func TestCount(t *testing.T) { |
| 283 |
ctx, cacher, terminate := testSetup(t) |
| 284 |
t.Cleanup(terminate) |
| 285 |
storagetesting.RunTestCount(ctx, t, cacher) |
| 286 |
} |
| 287 |
|
| 288 |
func TestWatch(t *testing.T) { |
| 289 |
ctx, cacher, terminate := testSetup(t) |
| 290 |
t.Cleanup(terminate) |
| 291 |
storagetesting.RunTestWatch(ctx, t, cacher) |
| 292 |
} |
| 293 |
|
| 294 |
func TestWatchFromZero(t *testing.T) { |
| 295 |
ctx, cacher, server, terminate := testSetupWithEtcdServer(t) |
| 296 |
t.Cleanup(terminate) |
| 297 |
storagetesting.RunTestWatchFromZero(ctx, t, cacher, compactWatchCache(cacher, server.V3Client.Client)) |
| 298 |
} |
| 299 |
|
| 300 |
func TestDeleteTriggerWatch(t *testing.T) { |
| 301 |
ctx, cacher, terminate := testSetup(t) |
| 302 |
t.Cleanup(terminate) |
| 303 |
storagetesting.RunTestDeleteTriggerWatch(ctx, t, cacher) |
| 304 |
} |
| 305 |
|
| 306 |
func TestWatchFromNonZero(t *testing.T) { |
| 307 |
ctx, cacher, terminate := testSetup(t) |
| 308 |
t.Cleanup(terminate) |
| 309 |
storagetesting.RunTestWatchFromNonZero(ctx, t, cacher) |
| 310 |
} |
| 311 |
|
| 312 |
func TestDelayedWatchDelivery(t *testing.T) { |
| 313 |
ctx, cacher, terminate := testSetup(t) |
| 314 |
t.Cleanup(terminate) |
| 315 |
storagetesting.RunTestDelayedWatchDelivery(ctx, t, cacher) |
| 316 |
} |
| 317 |
|
| 318 |
func TestWatchError(t *testing.T) { |
| 319 |
// TODO(#109831): Enable use of this test and run it. |
| 320 |
} |
| 321 |
|
| 322 |
func TestWatchContextCancel(t *testing.T) { |
| 323 |
// TODO(#109831): Enable use of this test and run it. |
| 324 |
} |
| 325 |
|
| 326 |
func TestWatcherTimeout(t *testing.T) { |
| 327 |
ctx, cacher, terminate := testSetup(t) |
| 328 |
t.Cleanup(terminate) |
| 329 |
storagetesting.RunTestWatcherTimeout(ctx, t, cacher) |
| 330 |
} |
| 331 |
|
| 332 |
func TestWatchDeleteEventObjectHaveLatestRV(t *testing.T) { |
| 333 |
ctx, cacher, terminate := testSetup(t) |
| 334 |
t.Cleanup(terminate) |
| 335 |
storagetesting.RunTestWatchDeleteEventObjectHaveLatestRV(ctx, t, cacher) |
| 336 |
} |
| 337 |
|
| 338 |
func TestWatchInitializationSignal(t *testing.T) { |
| 339 |
ctx, cacher, terminate := testSetup(t) |
| 340 |
t.Cleanup(terminate) |
| 341 |
storagetesting.RunTestWatchInitializationSignal(ctx, t, cacher) |
| 342 |
} |
| 343 |
|
| 344 |
func TestClusterScopedWatch(t *testing.T) { |
| 345 |
ctx, cacher, terminate := testSetup(t, withClusterScopedKeyFunc, withNodeNameAndNamespaceIndex) |
| 346 |
t.Cleanup(terminate) |
| 347 |
storagetesting.RunTestClusterScopedWatch(ctx, t, cacher) |
| 348 |
} |
| 349 |
|
| 350 |
func TestNamespaceScopedWatch(t *testing.T) { |
| 351 |
ctx, cacher, terminate := testSetup(t, withNodeNameAndNamespaceIndex) |
| 352 |
t.Cleanup(terminate) |
| 353 |
storagetesting.RunTestNamespaceScopedWatch(ctx, t, cacher) |
| 354 |
} |
| 355 |
|
| 356 |
func TestWatchDispatchBookmarkEvents(t *testing.T) { |
| 357 |
ctx, cacher, terminate := testSetup(t) |
| 358 |
t.Cleanup(terminate) |
| 359 |
storagetesting.RunTestWatchDispatchBookmarkEvents(ctx, t, cacher, true) |
| 360 |
} |
| 361 |
|
| 362 |
func TestWatchBookmarksWithCorrectResourceVersion(t *testing.T) { |
| 363 |
ctx, cacher, terminate := testSetup(t) |
| 364 |
t.Cleanup(terminate) |
| 365 |
storagetesting.RunTestOptionalWatchBookmarksWithCorrectResourceVersion(ctx, t, cacher) |
| 366 |
} |
| 367 |
|
| 368 |
func TestSendInitialEventsBackwardCompatibility(t *testing.T) { |
| 369 |
ctx, store, terminate := testSetup(t) |
| 370 |
t.Cleanup(terminate) |
| 371 |
storagetesting.RunSendInitialEventsBackwardCompatibility(ctx, t, store) |
| 372 |
} |
| 373 |
|
| 374 |
func TestWatchSemantics(t *testing.T) { |
| 375 |
store, terminate := testSetupWithEtcdAndCreateWrapper(t) |
| 376 |
t.Cleanup(terminate) |
| 377 |
storagetesting.RunWatchSemantics(context.TODO(), t, store) |
| 378 |
} |
| 379 |
|
| 380 |
func TestWatchSemanticInitialEventsExtended(t *testing.T) { |
| 381 |
store, terminate := testSetupWithEtcdAndCreateWrapper(t) |
| 382 |
t.Cleanup(terminate) |
| 383 |
storagetesting.RunWatchSemanticInitialEventsExtended(context.TODO(), t, store) |
| 384 |
} |
| 385 |
|
| 386 |
func TestWatchListMatchSingle(t *testing.T) { |
| 387 |
store, terminate := testSetupWithEtcdAndCreateWrapper(t) |
| 388 |
t.Cleanup(terminate) |
| 389 |
storagetesting.RunWatchListMatchSingle(context.TODO(), t, store) |
| 390 |
} |
| 391 |
|
| 392 |
// =================================================== |
| 393 |
// Test-setup related function are following. |
| 394 |
// =================================================== |
| 395 |
|
| 396 |
type tearDownFunc func() |
| 397 |
|
| 398 |
type setupOptions struct { |
| 399 |
resourcePrefix string |
| 400 |
keyFunc func(runtime.Object) (string, error) |
| 401 |
indexerFuncs map[string]storage.IndexerFunc |
| 402 |
indexers cache.Indexers |
| 403 |
clock clock.WithTicker |
| 404 |
} |
| 405 |
|
| 406 |
type setupOption func(*setupOptions) |
| 407 |
|
| 408 |
func withDefaults(options *setupOptions) { |
| 409 |
prefix := "/pods" |
| 410 |
|
| 411 |
options.resourcePrefix = prefix |
| 412 |
options.keyFunc = func(obj runtime.Object) (string, error) { return storage.NamespaceKeyFunc(prefix, obj) } |
| 413 |
options.clock = clock.RealClock{} |
| 414 |
} |
| 415 |
|
| 416 |
func withClusterScopedKeyFunc(options *setupOptions) { |
| 417 |
options.keyFunc = func(obj runtime.Object) (string, error) { |
| 418 |
return storage.NoNamespaceKeyFunc(options.resourcePrefix, obj) |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
// mirror indexer configuration from pkg/registry/core/pod/strategy.go |
| 423 |
func withNodeNameAndNamespaceIndex(options *setupOptions) { |
| 424 |
options.indexerFuncs = map[string]storage.IndexerFunc{ |
| 425 |
"spec.nodeName": func(obj runtime.Object) string { |
| 426 |
pod, ok := obj.(*example.Pod) |
| 427 |
if !ok { |
| 428 |
return "" |
| 429 |
} |
| 430 |
return pod.Spec.NodeName |
| 431 |
}, |
| 432 |
} |
| 433 |
options.indexers = map[string]cache.IndexFunc{ |
| 434 |
"f:spec.nodeName": func(obj interface{}) ([]string, error) { |
| 435 |
pod := obj.(*example.Pod) |
| 436 |
return []string{pod.Spec.NodeName}, nil |
| 437 |
}, |
| 438 |
"f:metadata.namespace": func(obj interface{}) ([]string, error) { |
| 439 |
pod := obj.(*example.Pod) |
| 440 |
return []string{pod.ObjectMeta.Namespace}, nil |
| 441 |
}, |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
func testSetup(t *testing.T, opts ...setupOption) (context.Context, *CacheDelegator, tearDownFunc) { |
| 446 |
ctx, cacher, _, tearDown := testSetupWithEtcdServer(t, opts...) |
| 447 |
return ctx, cacher, tearDown |
| 448 |
} |
| 449 |
|
| 450 |
func testSetupWithEtcdServer(t testing.TB, opts ...setupOption) (context.Context, *CacheDelegator, *etcd3testing.EtcdTestServer, tearDownFunc) { |
| 451 |
setupOpts := setupOptions{} |
| 452 |
opts = append([]setupOption{withDefaults}, opts...) |
| 453 |
for _, opt := range opts { |
| 454 |
opt(&setupOpts) |
| 455 |
} |
| 456 |
|
| 457 |
server, etcdStorage := newEtcdTestStorage(t, etcd3testing.PathPrefix()) |
| 458 |
// Inject one list error to make sure we test the relist case. |
| 459 |
wrappedStorage := &storagetesting.StorageInjectingListErrors{ |
| 460 |
Interface: etcdStorage, |
| 461 |
Errors: 1, |
| 462 |
} |
| 463 |
|
| 464 |
config := Config{ |
| 465 |
Storage: wrappedStorage, |
| 466 |
Versioner: storage.APIObjectVersioner{}, |
| 467 |
GroupResource: schema.GroupResource{Resource: "pods"}, |
| 468 |
EventsHistoryWindow: DefaultEventFreshDuration, |
| 469 |
ResourcePrefix: setupOpts.resourcePrefix, |
| 470 |
KeyFunc: setupOpts.keyFunc, |
| 471 |
GetAttrsFunc: GetPodAttrs, |
| 472 |
NewFunc: newPod, |
| 473 |
NewListFunc: newPodList, |
| 474 |
IndexerFuncs: setupOpts.indexerFuncs, |
| 475 |
Indexers: &setupOpts.indexers, |
| 476 |
Codec: codecs.LegacyCodec(examplev1.SchemeGroupVersion), |
| 477 |
Clock: setupOpts.clock, |
| 478 |
} |
| 479 |
cacher, err := NewCacherFromConfig(config) |
| 480 |
if err != nil { |
| 481 |
t.Fatalf("Failed to initialize cacher: %v", err) |
| 482 |
} |
| 483 |
ctx := context.Background() |
| 484 |
|
| 485 |
// Since some tests depend on the fact that GetList shouldn't fail, |
| 486 |
// we wait until the error from the underlying storage is consumed. |
| 487 |
if err := wait.PollInfinite(100*time.Millisecond, wrappedStorage.ErrorsConsumed); err != nil { |
| 488 |
t.Fatalf("Failed to inject list errors: %v", err) |
| 489 |
} |
| 490 |
|
| 491 |
if utilfeature.DefaultFeatureGate.Enabled(features.ResilientWatchCacheInitialization) { |
| 492 |
// The tests assume that Get/GetList/Watch calls shouldn't fail. |
| 493 |
// However, 429 error can now be returned if watchcache is under initialization. |
| 494 |
// To avoid rewriting all tests, we wait for watchcache to initialize. |
| 495 |
if err := cacher.Wait(ctx); err != nil { |
| 496 |
t.Fatal(err) |
| 497 |
} |
| 498 |
} |
| 499 |
delegator := NewCacheDelegator(cacher, wrappedStorage) |
| 500 |
terminate := func() { |
| 501 |
delegator.Stop() |
| 502 |
cacher.Stop() |
| 503 |
server.Terminate(t) |
| 504 |
} |
| 505 |
|
| 506 |
return ctx, delegator, server, terminate |
| 507 |
} |
| 508 |
|
| 509 |
func testSetupWithEtcdAndCreateWrapper(t *testing.T, opts ...setupOption) (storage.Interface, tearDownFunc) { |
| 510 |
_, cacher, _, tearDown := testSetupWithEtcdServer(t, opts...) |
| 511 |
|
| 512 |
if !utilfeature.DefaultFeatureGate.Enabled(features.ResilientWatchCacheInitialization) { |
| 513 |
if err := cacher.cacher.ready.wait(context.TODO()); err != nil { |
| 514 |
t.Fatalf("unexpected error waiting for the cache to be ready") |
| 515 |
} |
| 516 |
} |
| 517 |
return &createWrapper{CacheDelegator: cacher}, tearDown |
| 518 |
} |
| 519 |
|
| 520 |
type createWrapper struct { |
| 521 |
*CacheDelegator |
| 522 |
} |
| 523 |
|
| 524 |
func (c *createWrapper) Create(ctx context.Context, key string, obj, out runtime.Object, ttl uint64) error { |
| 525 |
if err := c.CacheDelegator.Create(ctx, key, obj, out, ttl); err != nil { |
| 526 |
return err |
| 527 |
} |
| 528 |
return wait.PollUntilContextTimeout(ctx, 100*time.Millisecond, wait.ForeverTestTimeout, true, func(ctx context.Context) (bool, error) { |
| 529 |
currentObj := c.CacheDelegator.cacher.newFunc() |
| 530 |
err := c.CacheDelegator.Get(ctx, key, storage.GetOptions{ResourceVersion: "0"}, currentObj) |
| 531 |
if err != nil { |
| 532 |
if storage.IsNotFound(err) { |
| 533 |
return false, nil |
| 534 |
} |
| 535 |
return false, err |
| 536 |
} |
| 537 |
if !apiequality.Semantic.DeepEqual(currentObj, out) { |
| 538 |
return false, nil |
| 539 |
} |
| 540 |
return true, nil |
| 541 |
}) |
| 542 |
} |
| 543 |
|
| 544 |
func BenchmarkStoreCreateList(b *testing.B) { |
| 545 |
klog.SetLogger(logr.Discard()) |
| 546 |
storeOptions := []struct { |
| 547 |
name string |
| 548 |
btreeEnabled bool |
| 549 |
}{ |
| 550 |
{ |
| 551 |
name: "Btree", |
| 552 |
btreeEnabled: true, |
| 553 |
}, |
| 554 |
{ |
| 555 |
name: "Map", |
| 556 |
btreeEnabled: false, |
| 557 |
}, |
| 558 |
} |
| 559 |
for _, store := range storeOptions { |
| 560 |
b.Run(fmt.Sprintf("Store=%s", store.name), func(b *testing.B) { |
| 561 |
featuregatetesting.SetFeatureGateDuringTest(b, utilfeature.DefaultFeatureGate, features.BtreeWatchCache, store.btreeEnabled) |
| 562 |
for _, rvm := range []metav1.ResourceVersionMatch{metav1.ResourceVersionMatchNotOlderThan, metav1.ResourceVersionMatchExact} { |
| 563 |
b.Run(fmt.Sprintf("RV=%s", rvm), func(b *testing.B) { |
| 564 |
for _, useIndex := range []bool{true, false} { |
| 565 |
b.Run(fmt.Sprintf("Indexed=%v", useIndex), func(b *testing.B) { |
| 566 |
opts := []setupOption{} |
| 567 |
if useIndex { |
| 568 |
opts = append(opts, withNodeNameAndNamespaceIndex) |
| 569 |
} |
| 570 |
ctx, cacher, _, terminate := testSetupWithEtcdServer(b, opts...) |
| 571 |
b.Cleanup(terminate) |
| 572 |
storagetesting.RunBenchmarkStoreListCreate(ctx, b, cacher, rvm) |
| 573 |
}) |
| 574 |
} |
| 575 |
}) |
| 576 |
} |
| 577 |
}) |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
func BenchmarkStoreList(b *testing.B) { |
| 582 |
klog.SetLogger(logr.Discard()) |
| 583 |
// Based on https://github.com/kubernetes/community/blob/master/sig-scalability/configs-and-limits/thresholds.md |
| 584 |
dimensions := []struct { |
| 585 |
namespaceCount int |
| 586 |
podPerNamespaceCount int |
| 587 |
nodeCount int |
| 588 |
}{ |
| 589 |
{ |
| 590 |
namespaceCount: 10_000, |
| 591 |
podPerNamespaceCount: 15, |
| 592 |
nodeCount: 5_000, |
| 593 |
}, |
| 594 |
{ |
| 595 |
namespaceCount: 50, |
| 596 |
podPerNamespaceCount: 3_000, |
| 597 |
nodeCount: 5_000, |
| 598 |
}, |
| 599 |
{ |
| 600 |
namespaceCount: 100, |
| 601 |
podPerNamespaceCount: 1_100, |
| 602 |
nodeCount: 1000, |
| 603 |
}, |
| 604 |
} |
| 605 |
for _, dims := range dimensions { |
| 606 |
b.Run(fmt.Sprintf("Namespaces=%d/Pods=%d/Nodes=%d", dims.namespaceCount, dims.namespaceCount*dims.podPerNamespaceCount, dims.nodeCount), func(b *testing.B) { |
| 607 |
data := storagetesting.PrepareBenchchmarkData(dims.namespaceCount, dims.podPerNamespaceCount, dims.nodeCount) |
| 608 |
storeOptions := []struct { |
| 609 |
name string |
| 610 |
btreeEnabled bool |
| 611 |
}{ |
| 612 |
{ |
| 613 |
name: "Btree", |
| 614 |
btreeEnabled: true, |
| 615 |
}, |
| 616 |
{ |
| 617 |
name: "Map", |
| 618 |
btreeEnabled: false, |
| 619 |
}, |
| 620 |
} |
| 621 |
for _, store := range storeOptions { |
| 622 |
b.Run(fmt.Sprintf("Store=%s", store.name), func(b *testing.B) { |
| 623 |
featuregatetesting.SetFeatureGateDuringTest(b, utilfeature.DefaultFeatureGate, features.BtreeWatchCache, store.btreeEnabled) |
| 624 |
ctx, cacher, _, terminate := testSetupWithEtcdServer(b, withNodeNameAndNamespaceIndex) |
| 625 |
b.Cleanup(terminate) |
| 626 |
var out example.Pod |
| 627 |
for _, pod := range data.Pods { |
| 628 |
err := cacher.Create(ctx, computePodKey(pod), pod, &out, 0) |
| 629 |
if err != nil { |
| 630 |
b.Fatal(err) |
| 631 |
} |
| 632 |
} |
| 633 |
for _, useIndex := range []bool{true, false} { |
| 634 |
b.Run(fmt.Sprintf("Indexed=%v", useIndex), func(b *testing.B) { |
| 635 |
storagetesting.RunBenchmarkStoreList(ctx, b, cacher, data, useIndex) |
| 636 |
}) |
| 637 |
} |
| 638 |
}) |
| 639 |
} |
| 640 |
}) |
| 641 |
} |
| 642 |
} |