github.com/cilium/cilium@v1.16.2/pkg/k8s/watchers/subscriber/raw.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package subscriber
     5  
     6  import (
     7  	"k8s.io/client-go/tools/cache"
     8  )
     9  
    10  var _ cache.ResourceEventHandler = (*RawChain)(nil)
    11  
    12  // RawChain holds the raw subscribers to any K8s resource / object changes in
    13  // the K8s watchers.
    14  //
    15  // RawChain itself is an implementation of cache.ResourceEventHandler with
    16  // an additional Register method for attaching children subscribers to the
    17  // chain.
    18  type RawChain struct {
    19  	list
    20  
    21  	subs []cache.ResourceEventHandler
    22  }
    23  
    24  // NewRaw creates a new raw subscriber list.
    25  func NewRawChain() *RawChain {
    26  	return &RawChain{}
    27  }
    28  
    29  // Register registers the raw event handler as a subscriber.
    30  func (l *RawChain) Register(cb cache.ResourceEventHandler) {
    31  	l.Lock()
    32  	l.subs = append(l.subs, cb)
    33  	l.Unlock()
    34  }
    35  
    36  // NotifyAdd notifies all the subscribers of an add event to an object.
    37  func (l *RawChain) OnAdd(obj interface{}, isInInitialList bool) {
    38  	l.RLock()
    39  	defer l.RUnlock()
    40  	for _, s := range l.subs {
    41  		s.OnAdd(obj, isInInitialList)
    42  	}
    43  }
    44  
    45  // NotifyUpdate notifies all the subscribers of an update event to an object.
    46  func (l *RawChain) OnUpdate(oldObj, newObj interface{}) {
    47  	l.RLock()
    48  	defer l.RUnlock()
    49  	for _, s := range l.subs {
    50  		s.OnUpdate(oldObj, newObj)
    51  	}
    52  }
    53  
    54  // NotifyDelete notifies all the subscribers of an update event to an object.
    55  func (l *RawChain) OnDelete(obj interface{}) {
    56  	l.RLock()
    57  	defer l.RUnlock()
    58  	for _, s := range l.subs {
    59  		s.OnDelete(obj)
    60  	}
    61  }