github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/watch/filter.go (about) 1 /* 2 Copyright 2014 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 23 // FilterFunc should take an event, possibly modify it in some way, and return 24 // the modified event. If the event should be ignored, then return keep=false. 25 type FilterFunc func(in Event) (out Event, keep bool) 26 27 // Filter passes all events through f before allowing them to pass on. 28 // Putting a filter on a watch, as an unavoidable side-effect due to the way 29 // go channels work, effectively causes the watch's event channel to have its 30 // queue length increased by one. 31 // 32 // WARNING: filter has a fatal flaw, in that it can't properly update the 33 // Type field (Add/Modified/Deleted) to reflect items beginning to pass the 34 // filter when they previously didn't. 35 func Filter(w Interface, f FilterFunc) Interface { 36 fw := &filteredWatch{ 37 incoming: w, 38 result: make(chan Event), 39 f: f, 40 } 41 go fw.loop() 42 return fw 43 } 44 45 type filteredWatch struct { 46 incoming Interface 47 result chan Event 48 f FilterFunc 49 } 50 51 // ResultChan returns a channel which will receive filtered events. 52 func (fw *filteredWatch) ResultChan() <-chan Event { 53 return fw.result 54 } 55 56 // Stop stops the upstream watch, which will eventually stop this watch. 57 func (fw *filteredWatch) Stop() { 58 fw.incoming.Stop() 59 } 60 61 // loop waits for new values, filters them, and resends them. 62 func (fw *filteredWatch) loop() { 63 defer close(fw.result) 64 for event := range fw.incoming.ResultChan() { 65 filtered, keep := fw.f(event) 66 if keep { 67 fw.result <- filtered 68 } 69 } 70 } 71 72 // Recorder records all events that are sent from the watch until it is closed. 73 type Recorder struct { 74 Interface 75 76 lock sync.Mutex 77 events []Event 78 } 79 80 var _ Interface = &Recorder{} 81 82 // NewRecorder wraps an Interface and records any changes sent across it. 83 func NewRecorder(w Interface) *Recorder { 84 r := &Recorder{} 85 r.Interface = Filter(w, r.record) 86 return r 87 } 88 89 // record is a FilterFunc and tracks each received event. 90 func (r *Recorder) record(in Event) (Event, bool) { 91 r.lock.Lock() 92 defer r.lock.Unlock() 93 r.events = append(r.events, in) 94 return in, true 95 } 96 97 // Events returns a copy of the events sent across this recorder. 98 func (r *Recorder) Events() []Event { 99 r.lock.Lock() 100 defer r.lock.Unlock() 101 copied := make([]Event, len(r.events)) 102 copy(copied, r.events) 103 return copied 104 }