k8s.io/client-go@v0.31.1/util/consistencydetector/watch_list_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 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  }