-
Notifications
You must be signed in to change notification settings - Fork 38.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Disable default paging in list watches #51876
Merged
k8s-github-robot
merged 1 commit into
kubernetes:master
from
smarterclayton:disable_client_paging
Sep 8, 2017
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
/* | ||
Copyright 2017 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package pager | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
|
||
"golang.org/x/net/context" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
metav1alpha1 "k8s.io/apimachinery/pkg/apis/meta/v1alpha1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
) | ||
|
||
func list(count int, rv string) *metainternalversion.List { | ||
var list metainternalversion.List | ||
for i := 0; i < count; i++ { | ||
list.Items = append(list.Items, &metav1alpha1.PartialObjectMetadata{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: fmt.Sprintf("%d", i), | ||
}, | ||
}) | ||
} | ||
list.ResourceVersion = rv | ||
return &list | ||
} | ||
|
||
type testPager struct { | ||
t *testing.T | ||
rv string | ||
index int | ||
remaining int | ||
last int | ||
continuing bool | ||
done bool | ||
expectPage int64 | ||
} | ||
|
||
func (p *testPager) reset() { | ||
p.continuing = false | ||
p.remaining += p.index | ||
p.index = 0 | ||
p.last = 0 | ||
p.done = false | ||
} | ||
|
||
func (p *testPager) PagedList(ctx context.Context, options metav1.ListOptions) (runtime.Object, error) { | ||
if p.done { | ||
p.t.Errorf("did not expect additional call to paged list") | ||
return nil, fmt.Errorf("unexpected list call") | ||
} | ||
expectedContinue := fmt.Sprintf("%s:%d", p.rv, p.last) | ||
if options.Limit != p.expectPage || (p.continuing && options.Continue != expectedContinue) { | ||
p.t.Errorf("invariant violated, expected limit %d and continue %s, got %#v", p.expectPage, expectedContinue, options) | ||
return nil, fmt.Errorf("invariant violated") | ||
} | ||
var list metainternalversion.List | ||
total := options.Limit | ||
if total == 0 { | ||
total = int64(p.remaining) | ||
} | ||
for i := int64(0); i < total; i++ { | ||
if p.remaining <= 0 { | ||
break | ||
} | ||
list.Items = append(list.Items, &metav1alpha1.PartialObjectMetadata{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: fmt.Sprintf("%d", p.index), | ||
}, | ||
}) | ||
p.remaining-- | ||
p.index++ | ||
} | ||
p.last = p.index | ||
if p.remaining > 0 { | ||
list.Continue = fmt.Sprintf("%s:%d", p.rv, p.last) | ||
p.continuing = true | ||
} else { | ||
p.done = true | ||
} | ||
list.ResourceVersion = p.rv | ||
return &list, nil | ||
} | ||
|
||
func (p *testPager) ExpiresOnSecondPage(ctx context.Context, options metav1.ListOptions) (runtime.Object, error) { | ||
if p.continuing { | ||
p.done = true | ||
return nil, errors.NewResourceExpired("this list has expired") | ||
} | ||
return p.PagedList(ctx, options) | ||
} | ||
|
||
func (p *testPager) ExpiresOnSecondPageThenFullList(ctx context.Context, options metav1.ListOptions) (runtime.Object, error) { | ||
if p.continuing { | ||
p.reset() | ||
p.expectPage = 0 | ||
return nil, errors.NewResourceExpired("this list has expired") | ||
} | ||
return p.PagedList(ctx, options) | ||
} | ||
|
||
func TestListPager_List(t *testing.T) { | ||
type fields struct { | ||
PageSize int64 | ||
PageFn ListPageFunc | ||
FullListIfExpired bool | ||
} | ||
type args struct { | ||
ctx context.Context | ||
options metav1.ListOptions | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
want runtime.Object | ||
wantErr bool | ||
isExpired bool | ||
}{ | ||
{ | ||
name: "empty page", | ||
fields: fields{PageSize: 10, PageFn: (&testPager{t: t, expectPage: 10, remaining: 0, rv: "rv:20"}).PagedList}, | ||
args: args{}, | ||
want: list(0, "rv:20"), | ||
}, | ||
{ | ||
name: "one page", | ||
fields: fields{PageSize: 10, PageFn: (&testPager{t: t, expectPage: 10, remaining: 9, rv: "rv:20"}).PagedList}, | ||
args: args{}, | ||
want: list(9, "rv:20"), | ||
}, | ||
{ | ||
name: "one full page", | ||
fields: fields{PageSize: 10, PageFn: (&testPager{t: t, expectPage: 10, remaining: 10, rv: "rv:20"}).PagedList}, | ||
args: args{}, | ||
want: list(10, "rv:20"), | ||
}, | ||
{ | ||
name: "two pages", | ||
fields: fields{PageSize: 10, PageFn: (&testPager{t: t, expectPage: 10, remaining: 11, rv: "rv:20"}).PagedList}, | ||
args: args{}, | ||
want: list(11, "rv:20"), | ||
}, | ||
{ | ||
name: "three pages", | ||
fields: fields{PageSize: 10, PageFn: (&testPager{t: t, expectPage: 10, remaining: 21, rv: "rv:20"}).PagedList}, | ||
args: args{}, | ||
want: list(21, "rv:20"), | ||
}, | ||
{ | ||
name: "expires on second page", | ||
fields: fields{PageSize: 10, PageFn: (&testPager{t: t, expectPage: 10, remaining: 21, rv: "rv:20"}).ExpiresOnSecondPage}, | ||
args: args{}, | ||
wantErr: true, | ||
isExpired: true, | ||
}, | ||
{ | ||
name: "expires on second page and then lists", | ||
fields: fields{ | ||
FullListIfExpired: true, | ||
PageSize: 10, | ||
PageFn: (&testPager{t: t, expectPage: 10, remaining: 21, rv: "rv:20"}).ExpiresOnSecondPageThenFullList, | ||
}, | ||
args: args{}, | ||
want: list(21, "rv:20"), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
p := &ListPager{ | ||
PageSize: tt.fields.PageSize, | ||
PageFn: tt.fields.PageFn, | ||
FullListIfExpired: tt.fields.FullListIfExpired, | ||
} | ||
got, err := p.List(tt.args.ctx, tt.args.options) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("ListPager.List() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if tt.isExpired != errors.IsResourceExpired(err) { | ||
t.Errorf("ListPager.List() error = %v, isExpired %v", err, tt.isExpired) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("ListPager.List() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can this be named in the positive, especially since that would get the default behavior you want?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The plan was in 1.9 to remove the
false &&
below. All of the test suites are already configured to bypass chunking by setting this flag, whereas all clients would still be opt out for beta. I wanted to avoid pointer insanity in internal code as well as bad globals (since this is core library code).