| 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.TB, prefix string) (*etcd3testing.EtcdTestServer, storage.Interface) { |
| 59 |
server, _ := etcd3testing.NewUnsecuredEtcd3TestClientServer(t) |
| 60 |
versioner := storage.APIObjectVersioner{} |
| 61 |
codec := apitesting.TestCodec(codecs, examplev1.SchemeGroupVersion) |
| 62 |
storage := etcd3.New( |
| 63 |
server.V3Client, |
| 64 |
codec, |
| 65 |
newPod, |
| 66 |
newPodList, |
| 67 |
prefix, |
| 68 |
"/pods", |
| 69 |
schema.GroupResource{Resource: "pods"}, |
| 70 |
identity.NewEncryptCheckTransformer(), |
| 71 |
etcd3.NewDefaultLeaseManagerConfig(), |
| 72 |
etcd3.NewDefaultDecoder(codec, versioner), |
| 73 |
versioner) |
| 74 |
return server, storage |
| 75 |
} |
| 76 |
|
| 77 |
func computePodKey(obj *example.Pod) string { |
| 78 |
return fmt.Sprintf("/pods/%s/%s", obj.Namespace, obj.Name) |
| 79 |
} |
| 80 |
|
| 81 |
func compactWatchCache(c *CacheDelegator, client *clientv3.Client) storagetesting.Compaction { |
| 82 |
return func(ctx context.Context, t *testing.T, resourceVersion string) { |
| 83 |
versioner := storage.APIObjectVersioner{} |
| 84 |
rv, err := versioner.ParseResourceVersion(resourceVersion) |
| 85 |
if err != nil { |
| 86 |
t.Fatal(err) |
| 87 |
} |
| 88 |
|
| 89 |
err = c.cacher.watchCache.waitUntilFreshAndBlock(context.TODO(), rv) |
| 90 |
if err != nil { |
| 91 |
t.Fatalf("WatchCache didn't caught up to RV: %v", rv) |
| 92 |
} |
| 93 |
c.cacher.watchCache.RUnlock() |
| 94 |
|
| 95 |
c.cacher.watchCache.Lock() |
| 96 |
defer c.cacher.watchCache.Unlock() |
| 97 |
c.cacher.Lock() |
| 98 |
defer c.cacher.Unlock() |
| 99 |
|
| 100 |
if c.cacher.watchCache.resourceVersion < rv { |
| 101 |
t.Fatalf("Can't compact into a future version: %v", resourceVersion) |
| 102 |
} |
| 103 |
|
| 104 |
if len(c.cacher.watchers.allWatchers) > 0 || len(c.cacher.watchers.valueWatchers) > 0 { |
| 105 |
// We could consider terminating those watchers, but given |
| 106 |
// watchcache doesn't really support compaction and we don't |
| 107 |
// exercise it in tests, we just throw an error here. |
| 108 |
t.Error("Open watchers are not supported during compaction") |
| 109 |
} |
| 110 |
|
| 111 |
for c.cacher.watchCache.startIndex < c.cacher.watchCache.endIndex { |
| 112 |
index := c.cacher.watchCache.startIndex % c.cacher.watchCache.capacity |
| 113 |
if c.cacher.watchCache.cache[index].ResourceVersion > rv { |
| 114 |
break |
| 115 |
} |
| 116 |
|
| 117 |
c.cacher.watchCache.startIndex++ |
| 118 |
} |
| 119 |
c.cacher.watchCache.listResourceVersion = rv |
| 120 |
if _, err := client.Compact(ctx, int64(rv)); err != nil { |
| 121 |
t.Fatalf("Could not compact: %v", err) |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
func increaseRV(client *clientv3.Client) storagetesting.IncreaseRVFunc { |
| 127 |
return func(ctx context.Context, t *testing.T) { |
| 128 |
if _, err := client.KV.Put(ctx, "increaseRV", "ok"); err != nil { |
| 129 |
t.Fatalf("Could not update increaseRV: %v", err) |
| 130 |
} |
| 131 |
} |
| 132 |
} |