...
1
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
30 type Lister interface {
31
32
33 List(options metav1.ListOptions) (runtime.Object, error)
34 }
35
36
37 type Watcher interface {
38
39 Watch(options metav1.ListOptions) (watch.Interface, error)
40 }
41
42
43 type ListerWatcher interface {
44 Lister
45 Watcher
46 }
47
48
49 type ListFunc func(options metav1.ListOptions) (runtime.Object, error)
50
51
52 type WatchFunc func(options metav1.ListOptions) (watch.Interface, error)
53
54
55
56
57 type ListWatch struct {
58 ListFunc ListFunc
59 WatchFunc WatchFunc
60
61 DisableChunking bool
62 }
63
64
65 type Getter interface {
66 Get() *restclient.Request
67 }
68
69
70 func NewListWatchFromClient(c Getter, resource string, namespace string, fieldSelector fields.Selector) *ListWatch {
71 optionsModifier := func(options *metav1.ListOptions) {
72 options.FieldSelector = fieldSelector.String()
73 }
74 return NewFilteredListWatchFromClient(c, resource, namespace, optionsModifier)
75 }
76
77
78
79
80 func NewFilteredListWatchFromClient(c Getter, resource string, namespace string, optionsModifier func(options *metav1.ListOptions)) *ListWatch {
81 listFunc := func(options metav1.ListOptions) (runtime.Object, error) {
82 optionsModifier(&options)
83 return c.Get().
84 Namespace(namespace).
85 Resource(resource).
86 VersionedParams(&options, metav1.ParameterCodec).
87 Do(context.TODO()).
88 Get()
89 }
90 watchFunc := func(options metav1.ListOptions) (watch.Interface, error) {
91 options.Watch = true
92 optionsModifier(&options)
93 return c.Get().
94 Namespace(namespace).
95 Resource(resource).
96 VersionedParams(&options, metav1.ParameterCodec).
97 Watch(context.TODO())
98 }
99 return &ListWatch{ListFunc: listFunc, WatchFunc: watchFunc}
100 }
101
102
103 func (lw *ListWatch) List(options metav1.ListOptions) (runtime.Object, error) {
104
105
106 return lw.ListFunc(options)
107 }
108
109
110 func (lw *ListWatch) Watch(options metav1.ListOptions) (watch.Interface, error) {
111 return lw.WatchFunc(options)
112 }
113
View as plain text