k8s.io/client-go@v0.31.1/tools/cache/reflector_data_consistency_detector.go (about)

     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 cache
    18  
    19  import (
    20  	"context"
    21  
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  	"k8s.io/apimachinery/pkg/runtime"
    24  	"k8s.io/client-go/util/consistencydetector"
    25  )
    26  
    27  // checkWatchListDataConsistencyIfRequested performs a data consistency check only when
    28  // the KUBE_WATCHLIST_INCONSISTENCY_DETECTOR environment variable was set during a binary startup.
    29  //
    30  // The consistency check is meant to be enforced only in the CI, not in production.
    31  // The check ensures that data retrieved by the watch-list api call
    32  // is exactly the same as data received by the standard list api call against etcd.
    33  //
    34  // Note that this function will panic when data inconsistency is detected.
    35  // This is intentional because we want to catch it in the CI.
    36  func checkWatchListDataConsistencyIfRequested[T runtime.Object, U any](ctx context.Context, identity string, lastSyncedResourceVersion string, listFn consistencydetector.ListFunc[T], retrieveItemsFn consistencydetector.RetrieveItemsFunc[U]) {
    37  	if !consistencydetector.IsDataConsistencyDetectionForWatchListEnabled() {
    38  		return
    39  	}
    40  	// for informers we pass an empty ListOptions because
    41  	// listFn might be wrapped for filtering during informer construction.
    42  	consistencydetector.CheckDataConsistency(ctx, identity, lastSyncedResourceVersion, listFn, metav1.ListOptions{}, retrieveItemsFn)
    43  }