k8s.io/client-go@v0.31.1/util/consistencydetector/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  	"k8s.io/apimachinery/pkg/api/meta"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"k8s.io/apimachinery/pkg/runtime"
    27  )
    28  
    29  var dataConsistencyDetectionForListFromCacheEnabled = false
    30  
    31  func init() {
    32  	dataConsistencyDetectionForListFromCacheEnabled, _ = strconv.ParseBool(os.Getenv("KUBE_LIST_FROM_CACHE_INCONSISTENCY_DETECTOR"))
    33  }
    34  
    35  // CheckListFromCacheDataConsistencyIfRequested performs a data consistency check only when
    36  // the KUBE_LIST_FROM_CACHE_INCONSISTENCY_DETECTOR environment variable was set during a binary startup
    37  // for requests that have a high chance of being served from the watch-cache.
    38  //
    39  // The consistency check is meant to be enforced only in the CI, not in production.
    40  // The check ensures that data retrieved by a list api call from the watch-cache
    41  // is exactly the same as data received by the list api call from etcd.
    42  //
    43  // Note that this function will panic when data inconsistency is detected.
    44  // This is intentional because we want to catch it in the CI.
    45  //
    46  // Note that this function doesn't examine the ListOptions to determine
    47  // if the original request has hit the cache because it would be challenging
    48  // to maintain consistency with the server-side implementation.
    49  // For simplicity, we assume that the first request retrieved data from
    50  // the cache (even though this might not be true for some requests)
    51  // and issue the second call to get data from etcd for comparison.
    52  func CheckListFromCacheDataConsistencyIfRequested[T runtime.Object](ctx context.Context, identity string, listItemsFn ListFunc[T], optionsUsedToReceiveList metav1.ListOptions, receivedList runtime.Object) {
    53  	if !dataConsistencyDetectionForListFromCacheEnabled {
    54  		return
    55  	}
    56  	checkListFromCacheDataConsistencyIfRequestedInternal(ctx, identity, listItemsFn, optionsUsedToReceiveList, receivedList)
    57  }
    58  
    59  func checkListFromCacheDataConsistencyIfRequestedInternal[T runtime.Object](ctx context.Context, identity string, listItemsFn ListFunc[T], optionsUsedToReceiveList metav1.ListOptions, receivedList runtime.Object) {
    60  	receivedListMeta, err := meta.ListAccessor(receivedList)
    61  	if err != nil {
    62  		panic(err)
    63  	}
    64  	rawListItems, err := meta.ExtractListWithAlloc(receivedList)
    65  	if err != nil {
    66  		panic(err) // this should never happen
    67  	}
    68  	lastSyncedResourceVersion := receivedListMeta.GetResourceVersion()
    69  	CheckDataConsistency(ctx, identity, lastSyncedResourceVersion, listItemsFn, optionsUsedToReceiveList, func() []runtime.Object { return rawListItems })
    70  }