Debian Codegraph @

tools/watch/informerwatcher.go

raw symbols SCIP
1 /*
2 Copyright 2017 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 watch
18
19 import (
20 "sync"
21
22 "k8s.io/apimachinery/pkg/runtime"
23 "k8s.io/apimachinery/pkg/util/wait"
24 "k8s.io/apimachinery/pkg/watch"
25 "k8s.io/client-go/tools/cache"
26 "k8s.io/klog/v2"
27 )
28
29 func newEventProcessor(out chan<- watch.Event) *eventProcessor {
30 return &eventProcessor{
31 out: out,
32 cond: sync.NewCond(&sync.Mutex{}),
33 done: make(chan struct{}),
34 }
35 }
36
37 // eventProcessor buffers events and writes them to an out chan when a reader
38 // is waiting. Because of the requirement to buffer events, it synchronizes
39 // input with a condition, and synchronizes output with a channels. It needs to
40 // be able to yield while both waiting on an input condition and while blocked
41 // on writing to the output channel.
42 type eventProcessor struct {
43 out chan<- watch.Event
44
45 cond *sync.Cond
46 buff []watch.Event
47
48 done chan struct{}
49 }
50
51 func (e *eventProcessor) run() {
52 for {
53 batch := e.takeBatch()
54 e.writeBatch(batch)
55 if e.stopped() {
56 return
57 }
58 }
59 }
60
61 func (e *eventProcessor) takeBatch() []watch.Event {
62 e.cond.L.Lock()
63 defer e.cond.L.Unlock()
64
65 for len(e.buff) == 0 && !e.stopped() {
66 e.cond.Wait()
67 }
68
69 batch := e.buff
70 e.buff = nil
71 return batch
72 }
73
74 func (e *eventProcessor) writeBatch(events []watch.Event) {
75 for _, event := range events {
76 select {
77 case e.out <- event:
78 case <-e.done:
79 return
80 }
81 }
82 }
83
84 func (e *eventProcessor) push(event watch.Event) {
85 e.cond.L.Lock()
86 defer e.cond.L.Unlock()
87 defer e.cond.Signal()
88 e.buff = append(e.buff, event)
89 }
90
91 func (e *eventProcessor) stopped() bool {
92 select {
93 case <-e.done:
94 return true
95 default:
96 return false
97 }
98 }
99
100 func (e *eventProcessor) stop() {
101 close(e.done)
102 e.cond.Signal()
103 }
104
105 // NewIndexerInformerWatcher will create an IndexerInformer and wrap it into watch.Interface
106 // so you can use it anywhere where you'd have used a regular Watcher returned from Watch method.
107 // it also returns a channel you can use to wait for the informers to fully shutdown.
108 //
109 // Contextual logging: NewIndexerInformerWatcherWithLogger should be used instead of NewIndexerInformerWatcher in code which supports contextual logging.
110 func NewIndexerInformerWatcher(lw cache.ListerWatcher, objType runtime.Object) (cache.Indexer, cache.Controller, watch.Interface, <-chan struct{}) {
111 return NewIndexerInformerWatcherWithLogger(klog.Background(), lw, objType)
112 }
113
114 // NewIndexerInformerWatcherWithLogger will create an IndexerInformer and wrap it into watch.Interface
115 // so you can use it anywhere where you'd have used a regular Watcher returned from Watch method.
116 // it also returns a channel you can use to wait for the informers to fully shutdown.
117 func NewIndexerInformerWatcherWithLogger(logger klog.Logger, lw cache.ListerWatcher, objType runtime.Object) (cache.Indexer, cache.Controller, watch.Interface, <-chan struct{}) {
118 ch := make(chan watch.Event)
119 w := watch.NewProxyWatcher(ch)
120 e := newEventProcessor(ch)
121
122 indexer, informer := cache.NewIndexerInformer(lw, objType, 0, cache.ResourceEventHandlerFuncs{
123 AddFunc: func(obj interface{}) {
124 e.push(watch.Event{
125 Type: watch.Added,
126 Object: obj.(runtime.Object),
127 })
128 },
129 UpdateFunc: func(old, new interface{}) {
130 e.push(watch.Event{
131 Type: watch.Modified,
132 Object: new.(runtime.Object),
133 })
134 },
135 DeleteFunc: func(obj interface{}) {
136 staleObj, stale := obj.(cache.DeletedFinalStateUnknown)
137 if stale {
138 // We have no means of passing the additional information down using
139 // watch API based on watch.Event but the caller can filter such
140 // objects by checking if metadata.deletionTimestamp is set
141 obj = staleObj.Obj
142 }
143
144 e.push(watch.Event{
145 Type: watch.Deleted,
146 Object: obj.(runtime.Object),
147 })
148 },
149 }, cache.Indexers{})
150
151 // This will get stopped, but without waiting for it.
152 go e.run()
153
154 doneCh := make(chan struct{})
155 go func() {
156 defer close(doneCh)
157 defer e.stop()
158 // Waiting for w.StopChan() is the traditional behavior which gets
159 // preserved here, with the logger added to support contextual logging.
160 ctx := wait.ContextForChannel(w.StopChan())
161 ctx = klog.NewContext(ctx, logger)
162 informer.RunWithContext(ctx)
163 }()
164
165 return indexer, informer, w, doneCh
166 }