| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 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 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 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 |
|
| 115 |
|
| 116 |
|
| 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 |
|
| 139 |
|
| 140 |
|
| 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 |
|
| 152 |
go e.run() |
| 153 |
|
| 154 |
doneCh := make(chan struct{}) |
| 155 |
go func() { |
| 156 |
defer close(doneCh) |
| 157 |
defer e.stop() |
| 158 |
|
| 159 |
|
| 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 |
} |