Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / k8s.io / client-go / tools / cache / listwatch.go
1 /*
2 Copyright 2015 The Kubernetes Authors.
3
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
7
8     http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 */
16
17 package cache
18
19 import (
20         "context"
21
22         metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23         "k8s.io/apimachinery/pkg/fields"
24         "k8s.io/apimachinery/pkg/runtime"
25         "k8s.io/apimachinery/pkg/watch"
26         restclient "k8s.io/client-go/rest"
27         "k8s.io/client-go/tools/pager"
28 )
29
30 // ListerWatcher is any object that knows how to perform an initial list and start a watch on a resource.
31 type ListerWatcher interface {
32         // List should return a list type object; the Items field will be extracted, and the
33         // ResourceVersion field will be used to start the watch in the right place.
34         List(options metav1.ListOptions) (runtime.Object, error)
35         // Watch should begin a watch at the specified version.
36         Watch(options metav1.ListOptions) (watch.Interface, error)
37 }
38
39 // ListFunc knows how to list resources
40 type ListFunc func(options metav1.ListOptions) (runtime.Object, error)
41
42 // WatchFunc knows how to watch resources
43 type WatchFunc func(options metav1.ListOptions) (watch.Interface, error)
44
45 // ListWatch knows how to list and watch a set of apiserver resources.  It satisfies the ListerWatcher interface.
46 // It is a convenience function for users of NewReflector, etc.
47 // ListFunc and WatchFunc must not be nil
48 type ListWatch struct {
49         ListFunc  ListFunc
50         WatchFunc WatchFunc
51         // DisableChunking requests no chunking for this list watcher.
52         DisableChunking bool
53 }
54
55 // Getter interface knows how to access Get method from RESTClient.
56 type Getter interface {
57         Get() *restclient.Request
58 }
59
60 // NewListWatchFromClient creates a new ListWatch from the specified client, resource, namespace and field selector.
61 func NewListWatchFromClient(c Getter, resource string, namespace string, fieldSelector fields.Selector) *ListWatch {
62         optionsModifier := func(options *metav1.ListOptions) {
63                 options.FieldSelector = fieldSelector.String()
64         }
65         return NewFilteredListWatchFromClient(c, resource, namespace, optionsModifier)
66 }
67
68 // NewFilteredListWatchFromClient creates a new ListWatch from the specified client, resource, namespace, and option modifier.
69 // Option modifier is a function takes a ListOptions and modifies the consumed ListOptions. Provide customized modifier function
70 // to apply modification to ListOptions with a field selector, a label selector, or any other desired options.
71 func NewFilteredListWatchFromClient(c Getter, resource string, namespace string, optionsModifier func(options *metav1.ListOptions)) *ListWatch {
72         listFunc := func(options metav1.ListOptions) (runtime.Object, error) {
73                 optionsModifier(&options)
74                 return c.Get().
75                         Namespace(namespace).
76                         Resource(resource).
77                         VersionedParams(&options, metav1.ParameterCodec).
78                         Do().
79                         Get()
80         }
81         watchFunc := func(options metav1.ListOptions) (watch.Interface, error) {
82                 options.Watch = true
83                 optionsModifier(&options)
84                 return c.Get().
85                         Namespace(namespace).
86                         Resource(resource).
87                         VersionedParams(&options, metav1.ParameterCodec).
88                         Watch()
89         }
90         return &ListWatch{ListFunc: listFunc, WatchFunc: watchFunc}
91 }
92
93 // List a set of apiserver resources
94 func (lw *ListWatch) List(options metav1.ListOptions) (runtime.Object, error) {
95         if !lw.DisableChunking {
96                 return pager.New(pager.SimplePageFunc(lw.ListFunc)).List(context.TODO(), options)
97         }
98         return lw.ListFunc(options)
99 }
100
101 // Watch a set of apiserver resources
102 func (lw *ListWatch) Watch(options metav1.ListOptions) (watch.Interface, error) {
103         return lw.WatchFunc(options)
104 }