github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/watch/watch.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  import (
    20  	"sync"
    21  
    22  	"k8s.io/kubernetes/pkg/runtime"
    23  )
    24  
    25  // Interface can be implemented by anything that knows how to watch and report changes.
    26  type Interface interface {
    27  	// Stops watching. Will close the channel returned by ResultChan(). Releases
    28  	// any resources used by the watch.
    29  	Stop()
    30  
    31  	// Returns a chan which will receive all the events. If an error occurs
    32  	// or Stop() is called, this channel will be closed, in which case the
    33  	// watch should be completely cleaned up.
    34  	ResultChan() <-chan Event
    35  }
    36  
    37  // EventType defines the possible types of events.
    38  type EventType string
    39  
    40  const (
    41  	Added    EventType = "ADDED"
    42  	Modified EventType = "MODIFIED"
    43  	Deleted  EventType = "DELETED"
    44  	Error    EventType = "ERROR"
    45  )
    46  
    47  // Event represents a single event to a watched resource.
    48  type Event struct {
    49  	Type EventType
    50  
    51  	// Object is:
    52  	//  * If Type is Added or Modified: the new state of the object.
    53  	//  * If Type is Deleted: the state of the object immediately before deletion.
    54  	//  * If Type is Error: *api.Status is recommended; other types may make sense
    55  	//    depending on context.
    56  	Object runtime.Object
    57  }
    58  
    59  type emptyWatch chan Event
    60  
    61  // NewEmptyWatch returns a watch interface that returns no results and is closed.
    62  // May be used in certain error conditions where no information is available but
    63  // an error is not warranted.
    64  func NewEmptyWatch() Interface {
    65  	ch := make(chan Event)
    66  	close(ch)
    67  	return emptyWatch(ch)
    68  }
    69  
    70  // Stop implements Interface
    71  func (w emptyWatch) Stop() {
    72  }
    73  
    74  // ResultChan implements Interface
    75  func (w emptyWatch) ResultChan() <-chan Event {
    76  	return chan Event(w)
    77  }
    78  
    79  // FakeWatcher lets you test anything that consumes a watch.Interface; threadsafe.
    80  type FakeWatcher struct {
    81  	result  chan Event
    82  	Stopped bool
    83  	sync.Mutex
    84  }
    85  
    86  func NewFake() *FakeWatcher {
    87  	return &FakeWatcher{
    88  		result: make(chan Event),
    89  	}
    90  }
    91  
    92  // Stop implements Interface.Stop().
    93  func (f *FakeWatcher) Stop() {
    94  	f.Lock()
    95  	defer f.Unlock()
    96  	if !f.Stopped {
    97  		close(f.result)
    98  		f.Stopped = true
    99  	}
   100  }
   101  
   102  func (f *FakeWatcher) ResultChan() <-chan Event {
   103  	return f.result
   104  }
   105  
   106  // Add sends an add event.
   107  func (f *FakeWatcher) Add(obj runtime.Object) {
   108  	f.result <- Event{Added, obj}
   109  }
   110  
   111  // Modify sends a modify event.
   112  func (f *FakeWatcher) Modify(obj runtime.Object) {
   113  	f.result <- Event{Modified, obj}
   114  }
   115  
   116  // Delete sends a delete event.
   117  func (f *FakeWatcher) Delete(lastValue runtime.Object) {
   118  	f.result <- Event{Deleted, lastValue}
   119  }
   120  
   121  // Error sends an Error event.
   122  func (f *FakeWatcher) Error(errValue runtime.Object) {
   123  	f.result <- Event{Error, errValue}
   124  }
   125  
   126  // Action sends an event of the requested type, for table-based testing.
   127  func (f *FakeWatcher) Action(action EventType, obj runtime.Object) {
   128  	f.result <- Event{action, obj}
   129  }