Add a new paging utility for client side ranging

This commit is contained in:
Clayton Coleman 2017-07-17 14:31:11 -04:00
parent 6a845c67f0
commit fb68d1d3a7
No known key found for this signature in database
GPG Key ID: 3D16906B4F1C5CB3
3 changed files with 140 additions and 0 deletions

View File

@ -182,6 +182,7 @@ filegroup(
"//staging/src/k8s.io/client-go/tools/clientcmd:all-srcs", "//staging/src/k8s.io/client-go/tools/clientcmd:all-srcs",
"//staging/src/k8s.io/client-go/tools/leaderelection:all-srcs", "//staging/src/k8s.io/client-go/tools/leaderelection:all-srcs",
"//staging/src/k8s.io/client-go/tools/metrics:all-srcs", "//staging/src/k8s.io/client-go/tools/metrics:all-srcs",
"//staging/src/k8s.io/client-go/tools/pager:all-srcs",
"//staging/src/k8s.io/client-go/tools/portforward:all-srcs", "//staging/src/k8s.io/client-go/tools/portforward:all-srcs",
"//staging/src/k8s.io/client-go/tools/record:all-srcs", "//staging/src/k8s.io/client-go/tools/record:all-srcs",
"//staging/src/k8s.io/client-go/tools/reference:all-srcs", "//staging/src/k8s.io/client-go/tools/reference:all-srcs",

View File

@ -0,0 +1,36 @@
package(default_visibility = ["//visibility:public"])
licenses(["notice"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["pager.go"],
tags = ["automanaged"],
deps = [
"//vendor/golang.org/x/net/context:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/meta:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/internalversion:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@ -0,0 +1,103 @@
/*
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"
"golang.org/x/net/context"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
const defaultPageSize = 500
type ListPageFunc func(ctx context.Context, opts metav1.ListOptions) (runtime.Object, error)
func SimplePageFunc(fn func(opts metav1.ListOptions) (runtime.Object, error)) ListPageFunc {
return func(ctx context.Context, opts metav1.ListOptions) (runtime.Object, error) {
return fn(opts)
}
}
type ListPager struct {
PageSize int64
PageFn ListPageFunc
FullListIfExpired bool
}
func New(fn ListPageFunc) *ListPager {
return &ListPager{
PageSize: defaultPageSize,
PageFn: fn,
FullListIfExpired: true,
}
}
func (p *ListPager) List(ctx context.Context, options metav1.ListOptions) (runtime.Object, error) {
if options.Limit == 0 {
options.Limit = p.PageSize
}
var list *metainternalversion.List
for {
obj, err := p.PageFn(ctx, options)
if err != nil {
if !errors.IsResourceExpired(err) || !p.FullListIfExpired {
return nil, err
}
// the list expired while we were processing, fall back to a full list
options.Limit = 0
options.Continue = ""
return p.PageFn(ctx, options)
}
m, err := meta.ListAccessor(obj)
if err != nil {
return nil, fmt.Errorf("returned object must be a list: %v", err)
}
// exit early and return the object we got if we haven't processed any pages
if len(m.GetContinue()) == 0 && list == nil {
return obj, nil
}
// initialize the list and fill its contents
if list == nil {
list = &metainternalversion.List{Items: make([]runtime.Object, 0, options.Limit+1)}
list.ResourceVersion = m.GetResourceVersion()
list.SelfLink = m.GetSelfLink()
}
if err := meta.EachListItem(obj, func(obj runtime.Object) error {
list.Items = append(list.Items, obj)
return nil
}); err != nil {
return nil, err
}
// if we have no more items, return the list
if len(m.GetContinue()) == 0 {
return list, nil
}
// set the next loop up
options.Continue = m.GetContinue()
}
}