Make GetRemainingItemCount/SetRemainingItemCount use pointers

Kubernetes-commit: 145e8c46cf81ff16b65ff729c392ced262984847
This commit is contained in:
Jordan Liggitt 2019-05-27 12:03:26 -04:00 committed by Kubernetes Publisher
parent 74be843d4b
commit 00e2435130
7 changed files with 20 additions and 17 deletions

View File

@ -67,7 +67,7 @@ func (c defaultTableConvertor) ConvertToTable(ctx context.Context, object runtim
table.ResourceVersion = m.GetResourceVersion() table.ResourceVersion = m.GetResourceVersion()
table.SelfLink = m.GetSelfLink() table.SelfLink = m.GetSelfLink()
table.Continue = m.GetContinue() table.Continue = m.GetContinue()
table.SetRemainingItemCount(m.GetRemainingItemCount()) table.RemainingItemCount = m.GetRemainingItemCount()
} else { } else {
if m, err := meta.CommonAccessor(object); err == nil { if m, err := meta.CommonAccessor(object); err == nil {
table.ResourceVersion = m.GetResourceVersion() table.ResourceVersion = m.GetResourceVersion()

View File

@ -605,7 +605,7 @@ func (c *Cacher) GetToList(ctx context.Context, key string, resourceVersion stri
} }
} }
if c.versioner != nil { if c.versioner != nil {
if err := c.versioner.UpdateList(listObj, readResourceVersion, "", 0); err != nil { if err := c.versioner.UpdateList(listObj, readResourceVersion, "", nil); err != nil {
return err return err
} }
} }
@ -680,7 +680,7 @@ func (c *Cacher) List(ctx context.Context, key string, resourceVersion string, p
} }
trace.Step(fmt.Sprintf("Filtered %d items", listVal.Len())) trace.Step(fmt.Sprintf("Filtered %d items", listVal.Len()))
if c.versioner != nil { if c.versioner != nil {
if err := c.versioner.UpdateList(listObj, readResourceVersion, "", 0); err != nil { if err := c.versioner.UpdateList(listObj, readResourceVersion, "", nil); err != nil {
return err return err
} }
} }

View File

@ -217,7 +217,7 @@ type testVersioner struct{}
func (testVersioner) UpdateObject(obj runtime.Object, resourceVersion uint64) error { func (testVersioner) UpdateObject(obj runtime.Object, resourceVersion uint64) error {
return meta.NewAccessor().SetResourceVersion(obj, strconv.FormatUint(resourceVersion, 10)) return meta.NewAccessor().SetResourceVersion(obj, strconv.FormatUint(resourceVersion, 10))
} }
func (testVersioner) UpdateList(obj runtime.Object, resourceVersion uint64, continueValue string, count int64) error { func (testVersioner) UpdateList(obj runtime.Object, resourceVersion uint64, continueValue string, count *int64) error {
listAccessor, err := meta.ListAccessor(obj) listAccessor, err := meta.ListAccessor(obj)
if err != nil || listAccessor == nil { if err != nil || listAccessor == nil {
return err return err

View File

@ -44,7 +44,7 @@ func (a APIObjectVersioner) UpdateObject(obj runtime.Object, resourceVersion uin
} }
// UpdateList implements Versioner // UpdateList implements Versioner
func (a APIObjectVersioner) UpdateList(obj runtime.Object, resourceVersion uint64, nextKey string, count int64) error { func (a APIObjectVersioner) UpdateList(obj runtime.Object, resourceVersion uint64, nextKey string, count *int64) error {
listAccessor, err := meta.ListAccessor(obj) listAccessor, err := meta.ListAccessor(obj)
if err != nil || listAccessor == nil { if err != nil || listAccessor == nil {
return err return err

View File

@ -391,7 +391,7 @@ func (s *store) GetToList(ctx context.Context, key string, resourceVersion strin
} }
} }
// update version with cluster level revision // update version with cluster level revision
return s.versioner.UpdateList(listObj, uint64(getResp.Header.Revision), "", 0) return s.versioner.UpdateList(listObj, uint64(getResp.Header.Revision), "", nil)
} }
func (s *store) Count(key string) (int64, error) { func (s *store) Count(key string) (int64, error) {
@ -618,17 +618,19 @@ func (s *store) List(ctx context.Context, key, resourceVersion string, pred stor
if err != nil { if err != nil {
return err return err
} }
remainingItemCount := getResp.Count - pred.Limit var remainingItemCount *int64
// getResp.Count counts in objects that do not match the pred. // getResp.Count counts in objects that do not match the pred.
// Instead of returning inaccurate count, return 0. // Instead of returning inaccurate count for non-empty selectors, we return nil.
if !pred.Empty() { // Only set remainingItemCount if the predicate is empty.
remainingItemCount = 0 if pred.Empty() {
c := int64(getResp.Count - pred.Limit)
remainingItemCount = &c
} }
return s.versioner.UpdateList(listObj, uint64(returnedRV), next, remainingItemCount) return s.versioner.UpdateList(listObj, uint64(returnedRV), next, remainingItemCount)
} }
// no continuation // no continuation
return s.versioner.UpdateList(listObj, uint64(returnedRV), "", 0) return s.versioner.UpdateList(listObj, uint64(returnedRV), "", nil)
} }
// growSlice takes a slice value and grows its capacity up // growSlice takes a slice value and grows its capacity up

View File

@ -47,6 +47,7 @@ import (
"k8s.io/apiserver/pkg/storage/etcd" "k8s.io/apiserver/pkg/storage/etcd"
storagetests "k8s.io/apiserver/pkg/storage/tests" storagetests "k8s.io/apiserver/pkg/storage/tests"
"k8s.io/apiserver/pkg/storage/value" "k8s.io/apiserver/pkg/storage/value"
utilpointer "k8s.io/utils/pointer"
) )
var scheme = runtime.NewScheme() var scheme = runtime.NewScheme()
@ -831,7 +832,7 @@ func TestList(t *testing.T) {
pred storage.SelectionPredicate pred storage.SelectionPredicate
expectedOut []*example.Pod expectedOut []*example.Pod
expectContinue bool expectContinue bool
expectedRemainingItemCount int64 expectedRemainingItemCount *int64
expectError bool expectError bool
}{ }{
{ {
@ -884,7 +885,7 @@ func TestList(t *testing.T) {
}, },
expectedOut: []*example.Pod{preset[1].storedObj}, expectedOut: []*example.Pod{preset[1].storedObj},
expectContinue: true, expectContinue: true,
expectedRemainingItemCount: 1, expectedRemainingItemCount: utilpointer.Int64Ptr(1),
}, },
{ {
name: "test List with limit when paging disabled", name: "test List with limit when paging disabled",
@ -1062,8 +1063,8 @@ func TestList(t *testing.T) {
t.Errorf("(%s): length of list want=%d, got=%d", tt.name, len(tt.expectedOut), len(out.Items)) t.Errorf("(%s): length of list want=%d, got=%d", tt.name, len(tt.expectedOut), len(out.Items))
continue continue
} }
if e, a := tt.expectedRemainingItemCount, out.ListMeta.GetRemainingItemCount(); e != a { if e, a := tt.expectedRemainingItemCount, out.ListMeta.GetRemainingItemCount(); (e == nil) != (a == nil) || (e != nil && a != nil && *e != *a) {
t.Errorf("(%s): remainingItemCount want=%d, got=%d", tt.name, e, a) t.Errorf("(%s): remainingItemCount want=%#v, got=%#v", tt.name, e, a)
} }
for j, wantPod := range tt.expectedOut { for j, wantPod := range tt.expectedOut {
getPod := &out.Items[j] getPod := &out.Items[j]

View File

@ -44,8 +44,8 @@ type Versioner interface {
// database. continueValue is optional and indicates that more results are available if the client // database. continueValue is optional and indicates that more results are available if the client
// passes that value to the server in a subsequent call. remainingItemCount indicates the number // passes that value to the server in a subsequent call. remainingItemCount indicates the number
// of remaining objects if the list is partial. The remainingItemCount field is omitted during // of remaining objects if the list is partial. The remainingItemCount field is omitted during
// serialization if it is set to 0. // serialization if it is set to nil.
UpdateList(obj runtime.Object, resourceVersion uint64, continueValue string, remainingItemCount int64) error UpdateList(obj runtime.Object, resourceVersion uint64, continueValue string, remainingItemCount *int64) error
// PrepareObjectForStorage should set SelfLink and ResourceVersion to the empty value. Should // PrepareObjectForStorage should set SelfLink and ResourceVersion to the empty value. Should
// return an error if the specified object cannot be updated. // return an error if the specified object cannot be updated.
PrepareObjectForStorage(obj runtime.Object) error PrepareObjectForStorage(obj runtime.Object) error