Skip to content

Commit 171ef8c

Browse files
liggittk8s-publishing-bot
authored andcommitted
Use transformer in consistency checker
Kubernetes-commit: 91368adbb556286942d996c60ab6cc39306415b7
1 parent 7cf6a05 commit 171ef8c

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

tools/cache/reflector.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -733,9 +733,11 @@ func (r *Reflector) watchList(ctx context.Context) (watch.Interface, error) {
733733
return false
734734
}
735735

736+
var transformer TransformFunc
736737
storeOpts := []StoreOption{}
737738
if tr, ok := r.store.(TransformingStore); ok && tr.Transformer() != nil {
738-
storeOpts = append(storeOpts, WithTransformer(tr.Transformer()))
739+
transformer = tr.Transformer()
740+
storeOpts = append(storeOpts, WithTransformer(transformer))
739741
}
740742

741743
initTrace := trace.New("Reflector WatchList", trace.Field{Key: "name", Value: r.name})
@@ -795,7 +797,7 @@ func (r *Reflector) watchList(ctx context.Context) (watch.Interface, error) {
795797
// we utilize the temporaryStore to ensure independence from the current store implementation.
796798
// as of today, the store is implemented as a queue and will be drained by the higher-level
797799
// component as soon as it finishes replacing the content.
798-
checkWatchListDataConsistencyIfRequested(ctx, r.name, resourceVersion, r.listerWatcher.ListWithContext, temporaryStore.List)
800+
checkWatchListDataConsistencyIfRequested(ctx, r.name, resourceVersion, r.listerWatcher.ListWithContext, transformer, temporaryStore.List)
799801

800802
if err := r.store.Replace(temporaryStore.List(), resourceVersion); err != nil {
801803
return nil, fmt.Errorf("unable to sync watch-list result: %w", err)

tools/cache/reflector_data_consistency_detector.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ import (
3333
//
3434
// Note that this function will panic when data inconsistency is detected.
3535
// This is intentional because we want to catch it in the CI.
36-
func checkWatchListDataConsistencyIfRequested[T runtime.Object, U any](ctx context.Context, identity string, lastSyncedResourceVersion string, listFn consistencydetector.ListFunc[T], retrieveItemsFn consistencydetector.RetrieveItemsFunc[U]) {
36+
func checkWatchListDataConsistencyIfRequested[T runtime.Object, U any](ctx context.Context, identity string, lastSyncedResourceVersion string, listFn consistencydetector.ListFunc[T], listItemTransformFunc func(interface{}) (interface{}, error), retrieveItemsFn consistencydetector.RetrieveItemsFunc[U]) {
3737
if !consistencydetector.IsDataConsistencyDetectionForWatchListEnabled() {
3838
return
3939
}
4040
// for informers we pass an empty ListOptions because
4141
// listFn might be wrapped for filtering during informer construction.
42-
consistencydetector.CheckDataConsistency(ctx, identity, lastSyncedResourceVersion, listFn, metav1.ListOptions{}, retrieveItemsFn)
42+
consistencydetector.CheckDataConsistency(ctx, identity, lastSyncedResourceVersion, listFn, listItemTransformFunc, metav1.ListOptions{}, retrieveItemsFn)
4343
}

util/consistencydetector/data_consistency_detector.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,14 @@ type RetrieveItemsFunc[U any] func() []U
5959

6060
type ListFunc[T runtime.Object] func(ctx context.Context, options metav1.ListOptions) (T, error)
6161

62+
type TransformFunc func(interface{}) (interface{}, error)
63+
6264
// CheckDataConsistency exists solely for testing purposes.
6365
// we cannot use checkWatchListDataConsistencyIfRequested because
6466
// it is guarded by an environmental variable.
6567
// we cannot manipulate the environmental variable because
6668
// it will affect other tests in this package.
67-
func CheckDataConsistency[T runtime.Object, U any](ctx context.Context, identity string, lastSyncedResourceVersion string, listFn ListFunc[T], listOptions metav1.ListOptions, retrieveItemsFn RetrieveItemsFunc[U]) {
69+
func CheckDataConsistency[T runtime.Object, U any](ctx context.Context, identity string, lastSyncedResourceVersion string, listFn ListFunc[T], listItemTransformFunc TransformFunc, listOptions metav1.ListOptions, retrieveItemsFn RetrieveItemsFunc[U]) {
6870
if !canFormAdditionalListCall(lastSyncedResourceVersion, listOptions) {
6971
klog.V(4).Infof("data consistency check for %s is enabled but the parameters (RV, ListOptions) doesn't allow for creating a valid LIST request. Skipping the data consistency check.", identity)
7072
return
@@ -94,6 +96,15 @@ func CheckDataConsistency[T runtime.Object, U any](ctx context.Context, identity
9496
if err != nil {
9597
panic(err) // this should never happen
9698
}
99+
if listItemTransformFunc != nil {
100+
for i := range rawListItems {
101+
obj, err := listItemTransformFunc(rawListItems[i])
102+
if err != nil {
103+
panic(err)
104+
}
105+
rawListItems[i] = obj.(runtime.Object)
106+
}
107+
}
97108
listItems := toMetaObjectSliceOrDie(rawListItems)
98109

99110
sort.Sort(byUID(listItems))

util/consistencydetector/data_consistency_detector_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,10 @@ func TestDataConsistencyChecker(t *testing.T) {
215215

216216
if scenario.expectPanic {
217217
require.Panics(t, func() {
218-
CheckDataConsistency(ctx, "", scenario.lastSyncedResourceVersion, fakeLister.List, scenario.requestOptions, retrievedItemsFunc)
218+
CheckDataConsistency(ctx, "", scenario.lastSyncedResourceVersion, fakeLister.List, nil, scenario.requestOptions, retrievedItemsFunc)
219219
})
220220
} else {
221-
CheckDataConsistency(ctx, "", scenario.lastSyncedResourceVersion, fakeLister.List, scenario.requestOptions, retrievedItemsFunc)
221+
CheckDataConsistency(ctx, "", scenario.lastSyncedResourceVersion, fakeLister.List, nil, scenario.requestOptions, retrievedItemsFunc)
222222
}
223223

224224
require.Equal(t, scenario.expectedListRequests, fakeLister.counter)
@@ -235,7 +235,7 @@ func TestDataConsistencyCheckerRetry(t *testing.T) {
235235
stopListErrorAfter := 5
236236
fakeErrLister := &errorLister{stopErrorAfter: stopListErrorAfter}
237237

238-
CheckDataConsistency(ctx, "", "", fakeErrLister.List, metav1.ListOptions{}, retrievedItemsFunc)
238+
CheckDataConsistency(ctx, "", "", fakeErrLister.List, nil, metav1.ListOptions{}, retrievedItemsFunc)
239239
require.Equal(t, fakeErrLister.listCounter, fakeErrLister.stopErrorAfter)
240240
}
241241

0 commit comments

Comments
 (0)