...
1
16
17 package resource
18
19 import (
20 "k8s.io/apimachinery/pkg/api/meta"
21 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22 "k8s.io/apimachinery/pkg/runtime"
23 "k8s.io/apimachinery/pkg/watch"
24 )
25
26
27 type Selector struct {
28 Client RESTClient
29 Mapping *meta.RESTMapping
30 Namespace string
31 LabelSelector string
32 FieldSelector string
33 LimitChunks int64
34 }
35
36
37 func NewSelector(client RESTClient, mapping *meta.RESTMapping, namespace, labelSelector, fieldSelector string, limitChunks int64) *Selector {
38 return &Selector{
39 Client: client,
40 Mapping: mapping,
41 Namespace: namespace,
42 LabelSelector: labelSelector,
43 FieldSelector: fieldSelector,
44 LimitChunks: limitChunks,
45 }
46 }
47
48
49 func (r *Selector) Visit(fn VisitorFunc) error {
50 helper := NewHelper(r.Client, r.Mapping)
51 initialOpts := metav1.ListOptions{
52 LabelSelector: r.LabelSelector,
53 FieldSelector: r.FieldSelector,
54 Limit: r.LimitChunks,
55 }
56 return FollowContinue(&initialOpts, func(options metav1.ListOptions) (runtime.Object, error) {
57 list, err := helper.List(
58 r.Namespace,
59 r.ResourceMapping().GroupVersionKind.GroupVersion().String(),
60 &options,
61 )
62 if err != nil {
63 return nil, EnhanceListError(err, options, r.Mapping.Resource.String())
64 }
65 resourceVersion, _ := metadataAccessor.ResourceVersion(list)
66
67 info := &Info{
68 Client: r.Client,
69 Mapping: r.Mapping,
70
71 Namespace: r.Namespace,
72 ResourceVersion: resourceVersion,
73
74 Object: list,
75 }
76
77 if err := fn(info, nil); err != nil {
78 return nil, err
79 }
80 return list, nil
81 })
82 }
83
84 func (r *Selector) Watch(resourceVersion string) (watch.Interface, error) {
85 return NewHelper(r.Client, r.Mapping).Watch(r.Namespace, r.ResourceMapping().GroupVersionKind.GroupVersion().String(),
86 &metav1.ListOptions{ResourceVersion: resourceVersion, LabelSelector: r.LabelSelector, FieldSelector: r.FieldSelector})
87 }
88
89
90 func (r *Selector) ResourceMapping() *meta.RESTMapping {
91 return r.Mapping
92 }
93
View as plain text