move checkWatchListDataConsistencyIfRequested back to client-go/tools/cache
This commit is contained in:
		
							
								
								
									
										51
									
								
								staging/src/k8s.io/client-go/tools/cache/reflector_data_consistency_detector.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								staging/src/k8s.io/client-go/tools/cache/reflector_data_consistency_detector.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@@ -0,0 +1,51 @@
 | 
				
			|||||||
 | 
					/*
 | 
				
			||||||
 | 
					Copyright 2024 The Kubernetes Authors.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Licensed under the Apache License, Version 2.0 (the "License");
 | 
				
			||||||
 | 
					you may not use this file except in compliance with the License.
 | 
				
			||||||
 | 
					You may obtain a copy of the License at
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    http://www.apache.org/licenses/LICENSE-2.0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Unless required by applicable law or agreed to in writing, software
 | 
				
			||||||
 | 
					distributed under the License is distributed on an "AS IS" BASIS,
 | 
				
			||||||
 | 
					WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
				
			||||||
 | 
					See the License for the specific language governing permissions and
 | 
				
			||||||
 | 
					limitations under the License.
 | 
				
			||||||
 | 
					*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					package cache
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"context"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
						"strconv"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 | 
				
			||||||
 | 
						"k8s.io/apimachinery/pkg/runtime"
 | 
				
			||||||
 | 
						"k8s.io/client-go/util/consistencydetector"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var dataConsistencyDetectionForWatchListEnabled = false
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						dataConsistencyDetectionForWatchListEnabled, _ = strconv.ParseBool(os.Getenv("KUBE_WATCHLIST_INCONSISTENCY_DETECTOR"))
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// checkWatchListDataConsistencyIfRequested performs a data consistency check only when
 | 
				
			||||||
 | 
					// the KUBE_WATCHLIST_INCONSISTENCY_DETECTOR environment variable was set during a binary startup.
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					// The consistency check is meant to be enforced only in the CI, not in production.
 | 
				
			||||||
 | 
					// The check ensures that data retrieved by the watch-list api call
 | 
				
			||||||
 | 
					// is exactly the same as data received by the standard list api call against etcd.
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					// Note that this function will panic when data inconsistency is detected.
 | 
				
			||||||
 | 
					// This is intentional because we want to catch it in the CI.
 | 
				
			||||||
 | 
					func checkWatchListDataConsistencyIfRequested[T runtime.Object, U any](ctx context.Context, identity string, lastSyncedResourceVersion string, listFn consistencydetector.ListFunc[T], retrieveItemsFn consistencydetector.RetrieveItemsFunc[U]) {
 | 
				
			||||||
 | 
						if !dataConsistencyDetectionForWatchListEnabled {
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						// for informers we pass an empty ListOptions because
 | 
				
			||||||
 | 
						// listFn might be wrapped for filtering during informer construction.
 | 
				
			||||||
 | 
						consistencydetector.CheckDataConsistency(ctx, identity, lastSyncedResourceVersion, listFn, metav1.ListOptions{}, retrieveItemsFn)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										29
									
								
								staging/src/k8s.io/client-go/tools/cache/reflector_data_consistency_detector_test.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								staging/src/k8s.io/client-go/tools/cache/reflector_data_consistency_detector_test.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@@ -0,0 +1,29 @@
 | 
				
			|||||||
 | 
					/*
 | 
				
			||||||
 | 
					Copyright 2024 The Kubernetes Authors.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Licensed under the Apache License, Version 2.0 (the "License");
 | 
				
			||||||
 | 
					you may not use this file except in compliance with the License.
 | 
				
			||||||
 | 
					You may obtain a copy of the License at
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    http://www.apache.org/licenses/LICENSE-2.0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Unless required by applicable law or agreed to in writing, software
 | 
				
			||||||
 | 
					distributed under the License is distributed on an "AS IS" BASIS,
 | 
				
			||||||
 | 
					WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
				
			||||||
 | 
					See the License for the specific language governing permissions and
 | 
				
			||||||
 | 
					limitations under the License.
 | 
				
			||||||
 | 
					*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					package cache
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"context"
 | 
				
			||||||
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"k8s.io/apimachinery/pkg/runtime"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestDriveWatchLisConsistencyIfRequired(t *testing.T) {
 | 
				
			||||||
 | 
						ctx := context.TODO()
 | 
				
			||||||
 | 
						checkWatchListDataConsistencyIfRequested[runtime.Object, runtime.Object](ctx, "", "", nil, nil)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -19,9 +19,7 @@ package consistencydetector
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"context"
 | 
						"context"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"os"
 | 
					 | 
				
			||||||
	"sort"
 | 
						"sort"
 | 
				
			||||||
	"strconv"
 | 
					 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/google/go-cmp/cmp"
 | 
						"github.com/google/go-cmp/cmp"
 | 
				
			||||||
@@ -33,34 +31,10 @@ import (
 | 
				
			|||||||
	"k8s.io/klog/v2"
 | 
						"k8s.io/klog/v2"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var dataConsistencyDetectionForWatchListEnabled = false
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func init() {
 | 
					 | 
				
			||||||
	dataConsistencyDetectionForWatchListEnabled, _ = strconv.ParseBool(os.Getenv("KUBE_WATCHLIST_INCONSISTENCY_DETECTOR"))
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type RetrieveItemsFunc[U any] func() []U
 | 
					type RetrieveItemsFunc[U any] func() []U
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type ListFunc[T runtime.Object] func(ctx context.Context, options metav1.ListOptions) (T, error)
 | 
					type ListFunc[T runtime.Object] func(ctx context.Context, options metav1.ListOptions) (T, error)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// checkWatchListDataConsistencyIfRequested performs a data consistency check only when
 | 
					 | 
				
			||||||
// the KUBE_WATCHLIST_INCONSISTENCY_DETECTOR environment variable was set during a binary startup.
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// The consistency check is meant to be enforced only in the CI, not in production.
 | 
					 | 
				
			||||||
// The check ensures that data retrieved by the watch-list api call
 | 
					 | 
				
			||||||
// is exactly the same as data received by the standard list api call against etcd.
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// Note that this function will panic when data inconsistency is detected.
 | 
					 | 
				
			||||||
// This is intentional because we want to catch it in the CI.
 | 
					 | 
				
			||||||
func checkWatchListDataConsistencyIfRequested[T runtime.Object, U any](ctx context.Context, identity string, lastSyncedResourceVersion string, listFn ListFunc[T], retrieveItemsFn RetrieveItemsFunc[U]) {
 | 
					 | 
				
			||||||
	if !dataConsistencyDetectionForWatchListEnabled {
 | 
					 | 
				
			||||||
		return
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	// for informers we pass an empty ListOptions because
 | 
					 | 
				
			||||||
	// listFn might be wrapped for filtering during informer construction.
 | 
					 | 
				
			||||||
	CheckDataConsistency(ctx, identity, lastSyncedResourceVersion, listFn, metav1.ListOptions{}, retrieveItemsFn)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// CheckDataConsistency exists solely for testing purposes.
 | 
					// CheckDataConsistency exists solely for testing purposes.
 | 
				
			||||||
// we cannot use checkWatchListDataConsistencyIfRequested because
 | 
					// we cannot use checkWatchListDataConsistencyIfRequested because
 | 
				
			||||||
// it is guarded by an environmental variable.
 | 
					// it is guarded by an environmental variable.
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -118,11 +118,6 @@ func TestDataConsistencyChecker(t *testing.T) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestDriveWatchLisConsistencyIfRequired(t *testing.T) {
 | 
					 | 
				
			||||||
	ctx := context.TODO()
 | 
					 | 
				
			||||||
	checkWatchListDataConsistencyIfRequested[runtime.Object, runtime.Object](ctx, "", "", nil, nil)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func TestDataConsistencyCheckerRetry(t *testing.T) {
 | 
					func TestDataConsistencyCheckerRetry(t *testing.T) {
 | 
				
			||||||
	ctx := context.TODO()
 | 
						ctx := context.TODO()
 | 
				
			||||||
	retrievedItemsFunc := func() []*v1.Pod {
 | 
						retrievedItemsFunc := func() []*v1.Pod {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user