Skip to content

Commit 27bdade

Browse files
authored
Merge pull request kubernetes#125432 from p0lyn0mial/upstream-watch-list-data-consistency-detector
client-go/consistencydetector: add CheckWatchListFromCacheDataConsistencyIfRequested
2 parents 9d63e57 + f6c6890 commit 27bdade

File tree

3 files changed

+61
-9
lines changed

3 files changed

+61
-9
lines changed

staging/src/k8s.io/client-go/tools/cache/reflector_data_consistency_detector.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,12 @@ package cache
1818

1919
import (
2020
"context"
21-
"os"
22-
"strconv"
2321

2422
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2523
"k8s.io/apimachinery/pkg/runtime"
2624
"k8s.io/client-go/util/consistencydetector"
2725
)
2826

29-
var dataConsistencyDetectionForWatchListEnabled = false
30-
31-
func init() {
32-
dataConsistencyDetectionForWatchListEnabled, _ = strconv.ParseBool(os.Getenv("KUBE_WATCHLIST_INCONSISTENCY_DETECTOR"))
33-
}
34-
3527
// checkWatchListDataConsistencyIfRequested performs a data consistency check only when
3628
// the KUBE_WATCHLIST_INCONSISTENCY_DETECTOR environment variable was set during a binary startup.
3729
//
@@ -42,7 +34,7 @@ func init() {
4234
// Note that this function will panic when data inconsistency is detected.
4335
// This is intentional because we want to catch it in the CI.
4436
func checkWatchListDataConsistencyIfRequested[T runtime.Object, U any](ctx context.Context, identity string, lastSyncedResourceVersion string, listFn consistencydetector.ListFunc[T], retrieveItemsFn consistencydetector.RetrieveItemsFunc[U]) {
45-
if !dataConsistencyDetectionForWatchListEnabled {
37+
if !consistencydetector.IsDataConsistencyDetectionForWatchListEnabled() {
4638
return
4739
}
4840
// for informers we pass an empty ListOptions because

staging/src/k8s.io/client-go/util/consistencydetector/list_data_consistency_detector_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ var (
3636
emptyListOptions = metav1.ListOptions{}
3737
)
3838

39+
func TestDriveCheckWatchListFromCacheDataConsistencyIfRequested(t *testing.T) {
40+
ctx := context.TODO()
41+
42+
CheckWatchListFromCacheDataConsistencyIfRequested(ctx, "", emptyListFunc, emptyListOptions, &v1.PodList{})
43+
}
44+
3945
func TestDriveCheckListFromCacheDataConsistencyIfRequested(t *testing.T) {
4046
ctx := context.TODO()
4147

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Copyright 2024 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 consistencydetector
18+
19+
import (
20+
"context"
21+
"os"
22+
"strconv"
23+
24+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
"k8s.io/apimachinery/pkg/runtime"
26+
)
27+
28+
var dataConsistencyDetectionForWatchListEnabled = false
29+
30+
func init() {
31+
dataConsistencyDetectionForWatchListEnabled, _ = strconv.ParseBool(os.Getenv("KUBE_WATCHLIST_INCONSISTENCY_DETECTOR"))
32+
}
33+
34+
// IsDataConsistencyDetectionForWatchListEnabled returns true when
35+
// the KUBE_WATCHLIST_INCONSISTENCY_DETECTOR environment variable was set during a binary startup.
36+
func IsDataConsistencyDetectionForWatchListEnabled() bool {
37+
return dataConsistencyDetectionForWatchListEnabled
38+
}
39+
40+
// CheckWatchListFromCacheDataConsistencyIfRequested performs a data consistency check only when
41+
// the KUBE_WATCHLIST_INCONSISTENCY_DETECTOR environment variable was set during a binary startup.
42+
//
43+
// The consistency check is meant to be enforced only in the CI, not in production.
44+
// The check ensures that data retrieved by the watch-list api call
45+
// is exactly the same as data received by the standard list api call against etcd.
46+
//
47+
// Note that this function will panic when data inconsistency is detected.
48+
// This is intentional because we want to catch it in the CI.
49+
func CheckWatchListFromCacheDataConsistencyIfRequested[T runtime.Object](ctx context.Context, identity string, listItemsFn ListFunc[T], optionsUsedToReceiveList metav1.ListOptions, receivedList runtime.Object) {
50+
if !IsDataConsistencyDetectionForWatchListEnabled() {
51+
return
52+
}
53+
checkListFromCacheDataConsistencyIfRequestedInternal(ctx, identity, listItemsFn, optionsUsedToReceiveList, receivedList)
54+
}

0 commit comments

Comments
 (0)