Merge pull request #125432 from p0lyn0mial/upstream-watch-list-data-consistency-detector

client-go/consistencydetector: add CheckWatchListFromCacheDataConsistencyIfRequested
This commit is contained in:
Kubernetes Prow Robot
2024-06-11 01:22:25 -07:00
committed by GitHub
3 changed files with 61 additions and 9 deletions

View File

@@ -18,20 +18,12 @@ package cache
import ( import (
"context" "context"
"os"
"strconv"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/util/consistencydetector" "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 // checkWatchListDataConsistencyIfRequested performs a data consistency check only when
// the KUBE_WATCHLIST_INCONSISTENCY_DETECTOR environment variable was set during a binary startup. // the KUBE_WATCHLIST_INCONSISTENCY_DETECTOR environment variable was set during a binary startup.
// //
@@ -42,7 +34,7 @@ func init() {
// Note that this function will panic when data inconsistency is detected. // Note that this function will panic when data inconsistency is detected.
// This is intentional because we want to catch it in the CI. // 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]) { func checkWatchListDataConsistencyIfRequested[T runtime.Object, U any](ctx context.Context, identity string, lastSyncedResourceVersion string, listFn consistencydetector.ListFunc[T], retrieveItemsFn consistencydetector.RetrieveItemsFunc[U]) {
if !dataConsistencyDetectionForWatchListEnabled { if !consistencydetector.IsDataConsistencyDetectionForWatchListEnabled() {
return return
} }
// for informers we pass an empty ListOptions because // for informers we pass an empty ListOptions because

View File

@@ -36,6 +36,12 @@ var (
emptyListOptions = metav1.ListOptions{} emptyListOptions = metav1.ListOptions{}
) )
func TestDriveCheckWatchListFromCacheDataConsistencyIfRequested(t *testing.T) {
ctx := context.TODO()
CheckWatchListFromCacheDataConsistencyIfRequested(ctx, "", emptyListFunc, emptyListOptions, &v1.PodList{})
}
func TestDriveCheckListFromCacheDataConsistencyIfRequested(t *testing.T) { func TestDriveCheckListFromCacheDataConsistencyIfRequested(t *testing.T) {
ctx := context.TODO() ctx := context.TODO()

View File

@@ -0,0 +1,54 @@
/*
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 consistencydetector
import (
"context"
"os"
"strconv"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
var dataConsistencyDetectionForWatchListEnabled = false
func init() {
dataConsistencyDetectionForWatchListEnabled, _ = strconv.ParseBool(os.Getenv("KUBE_WATCHLIST_INCONSISTENCY_DETECTOR"))
}
// IsDataConsistencyDetectionForWatchListEnabled returns true when
// the KUBE_WATCHLIST_INCONSISTENCY_DETECTOR environment variable was set during a binary startup.
func IsDataConsistencyDetectionForWatchListEnabled() bool {
return dataConsistencyDetectionForWatchListEnabled
}
// CheckWatchListFromCacheDataConsistencyIfRequested 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 CheckWatchListFromCacheDataConsistencyIfRequested[T runtime.Object](ctx context.Context, identity string, listItemsFn ListFunc[T], optionsUsedToReceiveList metav1.ListOptions, receivedList runtime.Object) {
if !IsDataConsistencyDetectionForWatchListEnabled() {
return
}
checkListFromCacheDataConsistencyIfRequestedInternal(ctx, identity, listItemsFn, optionsUsedToReceiveList, receivedList)
}