Skip to content

Commit f9b27ed

Browse files
authored
Merge pull request kubernetes#130925 from serathius/watchcache-snapshotter-interface
Create Snapshotter interface to fake the implementation
2 parents 593906d + 3edeb60 commit f9b27ed

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

staging/src/k8s.io/apiserver/pkg/storage/cacher/store_btree.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,16 @@ func newStoreSnapshotter() *storeSnapshotter {
435435
return s
436436
}
437437

438+
var _ Snapshotter = (*storeSnapshotter)(nil)
439+
440+
type Snapshotter interface {
441+
Reset()
442+
GetLessOrEqual(rv uint64) (orderedLister, bool)
443+
Add(rv uint64, indexer orderedLister)
444+
RemoveLess(rv uint64)
445+
Len() int
446+
}
447+
438448
type storeSnapshotter struct {
439449
mux sync.RWMutex
440450
snapshots *btree.BTreeG[rvSnapshot]
@@ -486,3 +496,10 @@ func (s *storeSnapshotter) RemoveLess(rv uint64) {
486496
s.snapshots.DeleteMin()
487497
}
488498
}
499+
500+
func (s *storeSnapshotter) Len() int {
501+
s.mux.RLock()
502+
defer s.mux.RUnlock()
503+
504+
return s.snapshots.Len()
505+
}

staging/src/k8s.io/apiserver/pkg/storage/cacher/store_btree_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,22 @@ func (f fakeOrderedLister) ListPrefix(prefixKey, continueKey string) []interface
137137
return nil
138138
}
139139
func (f fakeOrderedLister) Count(prefixKey, continueKey string) int { return 0 }
140+
141+
type fakeSnapshotter struct {
142+
getLessOrEqual func(rv uint64) (orderedLister, bool)
143+
}
144+
145+
var _ Snapshotter = (*fakeSnapshotter)(nil)
146+
147+
func (f *fakeSnapshotter) Reset() {}
148+
func (f *fakeSnapshotter) GetLessOrEqual(rv uint64) (orderedLister, bool) {
149+
if f.getLessOrEqual == nil {
150+
return nil, false
151+
}
152+
return f.getLessOrEqual(rv)
153+
}
154+
func (f *fakeSnapshotter) Add(rv uint64, indexer orderedLister) {}
155+
func (f *fakeSnapshotter) RemoveLess(rv uint64) {}
156+
func (f *fakeSnapshotter) Len() int {
157+
return 0
158+
}

staging/src/k8s.io/apiserver/pkg/storage/cacher/watch_cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ type watchCache struct {
154154
waitingUntilFresh *progress.ConditionalProgressRequester
155155

156156
// Stores previous snapshots of orderedLister to allow serving requests from previous revisions.
157-
snapshots *storeSnapshotter
157+
snapshots Snapshotter
158158
}
159159

160160
func newWatchCache(

staging/src/k8s.io/apiserver/pkg/storage/cacher/watch_cache_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,7 +1363,7 @@ func TestCacheSnapshots(t *testing.T) {
13631363
clock.Step(DefaultEventFreshDuration + 1)
13641364
require.NoError(t, store.Update(makeTestPod("foo", 500)))
13651365
assert.Equal(t, 1, store.capacity)
1366-
assert.Equal(t, 1, store.snapshots.snapshots.Len())
1366+
assert.Equal(t, 1, store.snapshots.Len())
13671367
_, found = store.snapshots.GetLessOrEqual(499)
13681368
assert.False(t, found, "Expected overfilled cache to delete events below 500")
13691369

@@ -1377,7 +1377,7 @@ func TestCacheSnapshots(t *testing.T) {
13771377
t.Log("Add event to force capacity upsize")
13781378
require.NoError(t, store.Update(makeTestPod("foo", 600)))
13791379
assert.Equal(t, 2, store.capacity)
1380-
assert.Equal(t, 2, store.snapshots.snapshots.Len())
1380+
assert.Equal(t, 2, store.snapshots.Len())
13811381

13821382
t.Log("Test cache on rev 600")
13831383
lister, found = store.snapshots.GetLessOrEqual(600)

0 commit comments

Comments
 (0)