mirror of
https://github.com/kubernetes/client-go.git
synced 2025-07-16 08:15:56 +00:00
move checkWatchListDataConsistencyIfRequested back to client-go/tools/cache
Kubernetes-commit: cb44f83b3d500bb2ed29f7634095bc64c9b21729
This commit is contained in:
parent
e6e45e172b
commit
538b7809aa
51
tools/cache/reflector_data_consistency_detector.go
vendored
Normal file
51
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
tools/cache/reflector_data_consistency_detector_test.go
vendored
Normal file
29
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 {
|
||||||
|
Loading…
Reference in New Issue
Block a user