github.com/enmand/kubernetes@v1.2.0-alpha.0/pkg/watch/filter.go (about)

     1  /*
     2  Copyright 2014 The Kubernetes Authors All rights reserved.
     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  // FilterFunc should take an event, possibly modify it in some way, and return
    20  // the modified event. If the event should be ignored, then return keep=false.
    21  type FilterFunc func(in Event) (out Event, keep bool)
    22  
    23  // Filter passes all events through f before allowing them to pass on.
    24  // Putting a filter on a watch, as an unavoidable side-effect due to the way
    25  // go channels work, effectively causes the watch's event channel to have its
    26  // queue length increased by one.
    27  //
    28  // WARNING: filter has a fatal flaw, in that it can't properly update the
    29  // Type field (Add/Modified/Deleted) to reflect items beginning to pass the
    30  // filter when they previously didn't.
    31  //
    32  func Filter(w Interface, f FilterFunc) Interface {
    33  	fw := &filteredWatch{
    34  		incoming: w,
    35  		result:   make(chan Event),
    36  		f:        f,
    37  	}
    38  	go fw.loop()
    39  	return fw
    40  }
    41  
    42  type filteredWatch struct {
    43  	incoming Interface
    44  	result   chan Event
    45  	f        FilterFunc
    46  }
    47  
    48  // ResultChan returns a channel which will receive filtered events.
    49  func (fw *filteredWatch) ResultChan() <-chan Event {
    50  	return fw.result
    51  }
    52  
    53  // Stop stops the upstream watch, which will eventually stop this watch.
    54  func (fw *filteredWatch) Stop() {
    55  	fw.incoming.Stop()
    56  }
    57  
    58  // loop waits for new values, filters them, and resends them.
    59  func (fw *filteredWatch) loop() {
    60  	defer close(fw.result)
    61  	for {
    62  		event, ok := <-fw.incoming.ResultChan()
    63  		if !ok {
    64  			break
    65  		}
    66  		filtered, keep := fw.f(event)
    67  		if keep {
    68  			fw.result <- filtered
    69  		}
    70  	}
    71  }