k8s.io/client-go@v0.31.1/tools/cache/listwatch.go (about) 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 ) 28 29 // Lister is any object that knows how to perform an initial list. 30 type Lister interface { 31 // List should return a list type object; the Items field will be extracted, and the 32 // ResourceVersion field will be used to start the watch in the right place. 33 List(options metav1.ListOptions) (runtime.Object, error) 34 } 35 36 // Watcher is any object that knows how to start a watch on a resource. 37 type Watcher interface { 38 // Watch should begin a watch at the specified version. 39 // 40 // If Watch returns an error, it should handle its own cleanup, including 41 // but not limited to calling Stop() on the watch, if one was constructed. 42 // This allows the caller to ignore the watch, if the error is non-nil. 43 Watch(options metav1.ListOptions) (watch.Interface, error) 44 } 45 46 // ListerWatcher is any object that knows how to perform an initial list and start a watch on a resource. 47 type ListerWatcher interface { 48 Lister 49 Watcher 50 } 51 52 // ListFunc knows how to list resources 53 type ListFunc func(options metav1.ListOptions) (runtime.Object, error) 54 55 // WatchFunc knows how to watch resources 56 type WatchFunc func(options metav1.ListOptions) (watch.Interface, error) 57 58 // ListWatch knows how to list and watch a set of apiserver resources. It satisfies the ListerWatcher interface. 59 // It is a convenience function for users of NewReflector, etc. 60 // ListFunc and WatchFunc must not be nil 61 type ListWatch struct { 62 ListFunc ListFunc 63 WatchFunc WatchFunc 64 // DisableChunking requests no chunking for this list watcher. 65 DisableChunking bool 66 } 67 68 // Getter interface knows how to access Get method from RESTClient. 69 type Getter interface { 70 Get() *restclient.Request 71 } 72 73 // NewListWatchFromClient creates a new ListWatch from the specified client, resource, namespace and field selector. 74 func NewListWatchFromClient(c Getter, resource string, namespace string, fieldSelector fields.Selector) *ListWatch { 75 optionsModifier := func(options *metav1.ListOptions) { 76 options.FieldSelector = fieldSelector.String() 77 } 78 return NewFilteredListWatchFromClient(c, resource, namespace, optionsModifier) 79 } 80 81 // NewFilteredListWatchFromClient creates a new ListWatch from the specified client, resource, namespace, and option modifier. 82 // Option modifier is a function takes a ListOptions and modifies the consumed ListOptions. Provide customized modifier function 83 // to apply modification to ListOptions with a field selector, a label selector, or any other desired options. 84 func NewFilteredListWatchFromClient(c Getter, resource string, namespace string, optionsModifier func(options *metav1.ListOptions)) *ListWatch { 85 listFunc := func(options metav1.ListOptions) (runtime.Object, error) { 86 optionsModifier(&options) 87 return c.Get(). 88 Namespace(namespace). 89 Resource(resource). 90 VersionedParams(&options, metav1.ParameterCodec). 91 Do(context.TODO()). 92 Get() 93 } 94 watchFunc := func(options metav1.ListOptions) (watch.Interface, error) { 95 options.Watch = true 96 optionsModifier(&options) 97 return c.Get(). 98 Namespace(namespace). 99 Resource(resource). 100 VersionedParams(&options, metav1.ParameterCodec). 101 Watch(context.TODO()) 102 } 103 return &ListWatch{ListFunc: listFunc, WatchFunc: watchFunc} 104 } 105 106 // List a set of apiserver resources 107 func (lw *ListWatch) List(options metav1.ListOptions) (runtime.Object, error) { 108 // ListWatch is used in Reflector, which already supports pagination. 109 // Don't paginate here to avoid duplication. 110 return lw.ListFunc(options) 111 } 112 113 // Watch a set of apiserver resources 114 func (lw *ListWatch) Watch(options metav1.ListOptions) (watch.Interface, error) { 115 return lw.WatchFunc(options) 116 }