Skip to content

Commit f7f3809

Browse files
committed
client-go/consistencydetector: introduce CheckWatchListFromCacheDataConsistencyIfRequested
1 parent 9d63e57 commit f7f3809

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

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)