k8s.io/apiserver@v0.29.3/pkg/storage/cacher/cacher_testing_utils_test.go (about)

     1  /*
     2  Copyright 2023 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 cacher
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"testing"
    23  
    24  	clientv3 "go.etcd.io/etcd/client/v3"
    25  
    26  	"k8s.io/apimachinery/pkg/api/apitesting"
    27  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    28  	"k8s.io/apimachinery/pkg/runtime"
    29  	"k8s.io/apimachinery/pkg/runtime/schema"
    30  	"k8s.io/apimachinery/pkg/runtime/serializer"
    31  	utilruntime "k8s.io/apimachinery/pkg/util/runtime"
    32  	"k8s.io/apiserver/pkg/apis/example"
    33  	examplev1 "k8s.io/apiserver/pkg/apis/example/v1"
    34  	example2v1 "k8s.io/apiserver/pkg/apis/example2/v1"
    35  	"k8s.io/apiserver/pkg/storage"
    36  	"k8s.io/apiserver/pkg/storage/etcd3"
    37  	etcd3testing "k8s.io/apiserver/pkg/storage/etcd3/testing"
    38  	storagetesting "k8s.io/apiserver/pkg/storage/testing"
    39  	"k8s.io/apiserver/pkg/storage/value/encrypt/identity"
    40  )
    41  
    42  var (
    43  	scheme   = runtime.NewScheme()
    44  	codecs   = serializer.NewCodecFactory(scheme)
    45  	errDummy = fmt.Errorf("dummy error")
    46  )
    47  
    48  func init() {
    49  	metav1.AddToGroupVersion(scheme, metav1.SchemeGroupVersion)
    50  	utilruntime.Must(example.AddToScheme(scheme))
    51  	utilruntime.Must(examplev1.AddToScheme(scheme))
    52  	utilruntime.Must(example2v1.AddToScheme(scheme))
    53  }
    54  
    55  func newPod() runtime.Object     { return &example.Pod{} }
    56  func newPodList() runtime.Object { return &example.PodList{} }
    57  
    58  func newEtcdTestStorage(t *testing.T, prefix string) (*etcd3testing.EtcdTestServer, storage.Interface) {
    59  	server, _ := etcd3testing.NewUnsecuredEtcd3TestClientServer(t)
    60  	storage := etcd3.New(
    61  		server.V3Client,
    62  		apitesting.TestCodec(codecs, examplev1.SchemeGroupVersion),
    63  		newPod,
    64  		newPodList,
    65  		prefix,
    66  		"/pods",
    67  		schema.GroupResource{Resource: "pods"},
    68  		identity.NewEncryptCheckTransformer(),
    69  		etcd3.NewDefaultLeaseManagerConfig())
    70  	return server, storage
    71  }
    72  
    73  func computePodKey(obj *example.Pod) string {
    74  	return fmt.Sprintf("/pods/%s/%s", obj.Namespace, obj.Name)
    75  }
    76  
    77  func compactStorage(c *Cacher, client *clientv3.Client) storagetesting.Compaction {
    78  	return func(ctx context.Context, t *testing.T, resourceVersion string) {
    79  		versioner := storage.APIObjectVersioner{}
    80  		rv, err := versioner.ParseResourceVersion(resourceVersion)
    81  		if err != nil {
    82  			t.Fatal(err)
    83  		}
    84  
    85  		err = c.watchCache.waitUntilFreshAndBlock(context.TODO(), rv)
    86  		if err != nil {
    87  			t.Fatalf("WatchCache didn't caught up to RV: %v", rv)
    88  		}
    89  		c.watchCache.RUnlock()
    90  
    91  		c.watchCache.Lock()
    92  		defer c.watchCache.Unlock()
    93  		c.Lock()
    94  		defer c.Unlock()
    95  
    96  		if c.watchCache.resourceVersion < rv {
    97  			t.Fatalf("Can't compact into a future version: %v", resourceVersion)
    98  		}
    99  
   100  		if len(c.watchers.allWatchers) > 0 || len(c.watchers.valueWatchers) > 0 {
   101  			// We could consider terminating those watchers, but given
   102  			// watchcache doesn't really support compaction and we don't
   103  			// exercise it in tests, we just throw an error here.
   104  			t.Error("Open watchers are not supported during compaction")
   105  		}
   106  
   107  		for c.watchCache.startIndex < c.watchCache.endIndex {
   108  			index := c.watchCache.startIndex % c.watchCache.capacity
   109  			if c.watchCache.cache[index].ResourceVersion > rv {
   110  				break
   111  			}
   112  
   113  			c.watchCache.startIndex++
   114  		}
   115  		c.watchCache.listResourceVersion = rv
   116  
   117  		if _, err = client.KV.Put(ctx, "compact_rev_key", resourceVersion); err != nil {
   118  			t.Fatalf("Could not update compact_rev_key: %v", err)
   119  		}
   120  		if _, err = client.Compact(ctx, int64(rv)); err != nil {
   121  			t.Fatalf("Could not compact: %v", err)
   122  		}
   123  	}
   124  }