2 Copyright 2017 The Kubernetes Authors.
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
23 "k8s.io/apimachinery/pkg/api/errors"
24 "k8s.io/apimachinery/pkg/api/meta"
25 metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
26 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27 "k8s.io/apimachinery/pkg/runtime"
30 const defaultPageSize = 500
32 // ListPageFunc returns a list object for the given list options.
33 type ListPageFunc func(ctx context.Context, opts metav1.ListOptions) (runtime.Object, error)
35 // SimplePageFunc adapts a context-less list function into one that accepts a context.
36 func SimplePageFunc(fn func(opts metav1.ListOptions) (runtime.Object, error)) ListPageFunc {
37 return func(ctx context.Context, opts metav1.ListOptions) (runtime.Object, error) {
42 // ListPager assists client code in breaking large list queries into multiple
43 // smaller chunks of PageSize or smaller. PageFn is expected to accept a
44 // metav1.ListOptions that supports paging and return a list. The pager does
45 // not alter the field or label selectors on the initial options list.
46 type ListPager struct {
50 FullListIfExpired bool
53 // New creates a new pager from the provided pager function using the default
54 // options. It will fall back to a full list if an expiration error is encountered
56 func New(fn ListPageFunc) *ListPager {
58 PageSize: defaultPageSize,
60 FullListIfExpired: true,
64 // TODO: introduce other types of paging functions - such as those that retrieve from a list
67 // List returns a single list object, but attempts to retrieve smaller chunks from the
68 // server to reduce the impact on the server. If the chunk attempt fails, it will load
69 // the full list instead. The Limit field on options, if unset, will default to the page size.
70 func (p *ListPager) List(ctx context.Context, options metav1.ListOptions) (runtime.Object, error) {
71 if options.Limit == 0 {
72 options.Limit = p.PageSize
74 var list *metainternalversion.List
76 obj, err := p.PageFn(ctx, options)
78 if !errors.IsResourceExpired(err) || !p.FullListIfExpired {
81 // the list expired while we were processing, fall back to a full list
84 return p.PageFn(ctx, options)
86 m, err := meta.ListAccessor(obj)
88 return nil, fmt.Errorf("returned object must be a list: %v", err)
91 // exit early and return the object we got if we haven't processed any pages
92 if len(m.GetContinue()) == 0 && list == nil {
96 // initialize the list and fill its contents
98 list = &metainternalversion.List{Items: make([]runtime.Object, 0, options.Limit+1)}
99 list.ResourceVersion = m.GetResourceVersion()
100 list.SelfLink = m.GetSelfLink()
102 if err := meta.EachListItem(obj, func(obj runtime.Object) error {
103 list.Items = append(list.Items, obj)
109 // if we have no more items, return the list
110 if len(m.GetContinue()) == 0 {
114 // set the next loop up
115 options.Continue = m.GetContinue()