Skip to content

Commit abaa73b

Browse files
authored
Merge pull request kubernetes#130688 from serathius/watchcache-validate
Use ValidateListOptions in watch cache
2 parents 8906223 + 9e7c080 commit abaa73b

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ func (c *CacheDelegator) Get(ctx context.Context, key string, opts storage.GetOp
176176
}
177177

178178
func (c *CacheDelegator) GetList(ctx context.Context, key string, opts storage.ListOptions, listObj runtime.Object) error {
179+
_, _, err := storage.ValidateListOptions(c.cacher.resourcePrefix, c.cacher.versioner, opts)
180+
if err != nil {
181+
return err
182+
}
179183
shouldDelegate, consistentRead := shouldDelegateList(opts)
180184
if shouldDelegate {
181185
return c.storage.GetList(ctx, key, opts, listObj)

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"context"
2121
"testing"
2222

23+
"k8s.io/apimachinery/pkg/apis/meta/internalversion"
24+
"k8s.io/apimachinery/pkg/apis/meta/internalversion/validation"
2325
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2426
"k8s.io/apimachinery/pkg/runtime"
2527
"k8s.io/apiserver/pkg/apis/example"
@@ -192,3 +194,56 @@ func TestCalculateDigest(t *testing.T) {
192194
})
193195
}
194196
}
197+
198+
func TestValidateUndelegatedListOptions(t *testing.T) {
199+
opts := []storage.ListOptions{}
200+
keyPrefix := "/pods/"
201+
continueOnRV1, err := storage.EncodeContinue("/pods/a", keyPrefix, 1)
202+
if err != nil {
203+
t.Fatalf("Unexpected error: %v", err)
204+
}
205+
continueOnNegativeRV, err := storage.EncodeContinue("/pods/a", keyPrefix, -1)
206+
if err != nil {
207+
t.Fatalf("Unexpected error: %v", err)
208+
}
209+
for _, rv := range []string{"", "0", "1"} {
210+
for _, match := range []metav1.ResourceVersionMatch{"", metav1.ResourceVersionMatchExact, metav1.ResourceVersionMatchNotOlderThan} {
211+
for _, c := range []string{"", continueOnRV1, continueOnNegativeRV} {
212+
for _, limit := range []int64{0, 100} {
213+
for _, recursive := range []bool{true, false} {
214+
opt := storage.ListOptions{
215+
ResourceVersion: rv,
216+
ResourceVersionMatch: match,
217+
Predicate: storage.SelectionPredicate{
218+
Limit: limit,
219+
Continue: c,
220+
},
221+
Recursive: recursive,
222+
}
223+
// Skip requests that will not pass apiserver validation
224+
if errs := validation.ValidateListOptions(&internalversion.ListOptions{
225+
ResourceVersion: opt.ResourceVersion,
226+
ResourceVersionMatch: opt.ResourceVersionMatch,
227+
Limit: opt.Predicate.Limit,
228+
Continue: opt.Predicate.Continue,
229+
}, false); len(errs) != 0 {
230+
continue
231+
}
232+
// Skip requests sent directly to etcd
233+
if delegateList, _ := shouldDelegateList(opt); delegateList {
234+
continue
235+
}
236+
opts = append(opts, opt)
237+
}
238+
239+
}
240+
}
241+
}
242+
}
243+
for _, opt := range opts {
244+
_, _, err := storage.ValidateListOptions(keyPrefix, storage.APIObjectVersioner{}, opt)
245+
if err != nil {
246+
t.Errorf("Expected List requests %+v to pass validation, got: %v", opt, err)
247+
}
248+
}
249+
}

0 commit comments

Comments
 (0)