pager.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package pager
  14. import (
  15. "context"
  16. "fmt"
  17. "k8s.io/apimachinery/pkg/api/errors"
  18. "k8s.io/apimachinery/pkg/api/meta"
  19. metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/runtime"
  22. )
  23. const defaultPageSize = 500
  24. // ListPageFunc returns a list object for the given list options.
  25. type ListPageFunc func(ctx context.Context, opts metav1.ListOptions) (runtime.Object, error)
  26. // SimplePageFunc adapts a context-less list function into one that accepts a context.
  27. func SimplePageFunc(fn func(opts metav1.ListOptions) (runtime.Object, error)) ListPageFunc {
  28. return func(ctx context.Context, opts metav1.ListOptions) (runtime.Object, error) {
  29. return fn(opts)
  30. }
  31. }
  32. // ListPager assists client code in breaking large list queries into multiple
  33. // smaller chunks of PageSize or smaller. PageFn is expected to accept a
  34. // metav1.ListOptions that supports paging and return a list. The pager does
  35. // not alter the field or label selectors on the initial options list.
  36. type ListPager struct {
  37. PageSize int64
  38. PageFn ListPageFunc
  39. FullListIfExpired bool
  40. }
  41. // New creates a new pager from the provided pager function using the default
  42. // options. It will fall back to a full list if an expiration error is encountered
  43. // as a last resort.
  44. func New(fn ListPageFunc) *ListPager {
  45. return &ListPager{
  46. PageSize: defaultPageSize,
  47. PageFn: fn,
  48. FullListIfExpired: true,
  49. }
  50. }
  51. // TODO: introduce other types of paging functions - such as those that retrieve from a list
  52. // of namespaces.
  53. // List returns a single list object, but attempts to retrieve smaller chunks from the
  54. // server to reduce the impact on the server. If the chunk attempt fails, it will load
  55. // the full list instead. The Limit field on options, if unset, will default to the page size.
  56. func (p *ListPager) List(ctx context.Context, options metav1.ListOptions) (runtime.Object, error) {
  57. if options.Limit == 0 {
  58. options.Limit = p.PageSize
  59. }
  60. var list *metainternalversion.List
  61. for {
  62. obj, err := p.PageFn(ctx, options)
  63. if err != nil {
  64. if !errors.IsResourceExpired(err) || !p.FullListIfExpired {
  65. return nil, err
  66. }
  67. // the list expired while we were processing, fall back to a full list
  68. options.Limit = 0
  69. options.Continue = ""
  70. return p.PageFn(ctx, options)
  71. }
  72. m, err := meta.ListAccessor(obj)
  73. if err != nil {
  74. return nil, fmt.Errorf("returned object must be a list: %v", err)
  75. }
  76. // exit early and return the object we got if we haven't processed any pages
  77. if len(m.GetContinue()) == 0 && list == nil {
  78. return obj, nil
  79. }
  80. // initialize the list and fill its contents
  81. if list == nil {
  82. list = &metainternalversion.List{Items: make([]runtime.Object, 0, options.Limit+1)}
  83. list.ResourceVersion = m.GetResourceVersion()
  84. list.SelfLink = m.GetSelfLink()
  85. }
  86. if err := meta.EachListItem(obj, func(obj runtime.Object) error {
  87. list.Items = append(list.Items, obj)
  88. return nil
  89. }); err != nil {
  90. return nil, err
  91. }
  92. // if we have no more items, return the list
  93. if len(m.GetContinue()) == 0 {
  94. return list, nil
  95. }
  96. // set the next loop up
  97. options.Continue = m.GetContinue()
  98. }
  99. }