Skip to content

Commit

Permalink
replacing oldobject with cachedobject
Browse files Browse the repository at this point in the history
  • Loading branch information
gizas committed Jan 10, 2024
1 parent 8ad0d7c commit a63bbf3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 34 deletions.
20 changes: 10 additions & 10 deletions kubernetes/eventhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,22 +138,22 @@ type podUpdaterStore interface {
List() []interface{}
}

// OldObjectWatcher is the interface that an object needs to implement to be
// able to use Oldobject cache event function from watcher.
type oldObjectWatcher interface {
Oldobject() runtime.Object
// CachedObjectWatcher is the interface that an object needs to implement to be
// able to use CachedObject cache event function from watcher.
type cachedObjectWatcher interface {
CachedObject() runtime.Object
}

// namespacePodUpdater notifies updates on pods when their namespaces are updated.
type namespacePodUpdater struct {
handler podUpdaterHandlerFunc
store podUpdaterStore
namespaceWatcher oldObjectWatcher
namespaceWatcher cachedObjectWatcher
locker sync.Locker
}

// NewNamespacePodUpdater creates a namespacePodUpdater
func NewNamespacePodUpdater(handler podUpdaterHandlerFunc, store podUpdaterStore, namespaceWatcher oldObjectWatcher, locker sync.Locker) *namespacePodUpdater {
func NewNamespacePodUpdater(handler podUpdaterHandlerFunc, store podUpdaterStore, namespaceWatcher cachedObjectWatcher, locker sync.Locker) *namespacePodUpdater {
return &namespacePodUpdater{
handler: handler,
store: store,
Expand All @@ -177,7 +177,7 @@ func (n *namespacePodUpdater) OnUpdate(obj interface{}) {
n.locker.Lock()
defer n.locker.Unlock()
}
cachedObject := n.namespaceWatcher.Oldobject()
cachedObject := n.namespaceWatcher.CachedObject()
cachedNamespace, ok := cachedObject.(*Namespace)

if ok && ns.Name == cachedNamespace.Name {
Expand Down Expand Up @@ -208,12 +208,12 @@ func (*namespacePodUpdater) OnDelete(interface{}) {}
type nodePodUpdater struct {
handler podUpdaterHandlerFunc
store podUpdaterStore
nodeWatcher oldObjectWatcher
nodeWatcher cachedObjectWatcher
locker sync.Locker
}

// NewNodePodUpdater creates a nodePodUpdater
func NewNodePodUpdater(handler podUpdaterHandlerFunc, store podUpdaterStore, nodeWatcher oldObjectWatcher, locker sync.Locker) *nodePodUpdater {
func NewNodePodUpdater(handler podUpdaterHandlerFunc, store podUpdaterStore, nodeWatcher cachedObjectWatcher, locker sync.Locker) *nodePodUpdater {
return &nodePodUpdater{
handler: handler,
store: store,
Expand All @@ -237,7 +237,7 @@ func (n *nodePodUpdater) OnUpdate(obj interface{}) {
n.locker.Lock()
defer n.locker.Unlock()
}
cachedObject := n.nodeWatcher.Oldobject()
cachedObject := n.nodeWatcher.CachedObject()
cachedNode, ok := cachedObject.(*Node)

if ok && node.Name == cachedNode.Name {
Expand Down
48 changes: 24 additions & 24 deletions kubernetes/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ type Watcher interface {
// Client returns the kubernetes client object used by the watcher
Client() kubernetes.Interface

// Oldobject returns the old object before change during the last updated event
Oldobject() runtime.Object
// CachedObject returns the old object before change during the last updated event
CachedObject() runtime.Object
}

// WatchOptions controls watch behaviors
Expand All @@ -86,15 +86,15 @@ type item struct {
}

type watcher struct {
client kubernetes.Interface
informer cache.SharedInformer
store cache.Store
queue workqueue.Interface
ctx context.Context
stop context.CancelFunc
handler ResourceEventHandler
logger *logp.Logger
oldobject runtime.Object
client kubernetes.Interface
informer cache.SharedInformer
store cache.Store
queue workqueue.Interface
ctx context.Context
stop context.CancelFunc
handler ResourceEventHandler
logger *logp.Logger
cachedObject runtime.Object
}

// NewWatcher initializes the watcher client to provide a events handler for
Expand All @@ -110,7 +110,7 @@ func NewWatcher(client kubernetes.Interface, resource Resource, opts WatchOption
func NewNamedWatcher(name string, client kubernetes.Interface, resource Resource, opts WatchOptions, indexers cache.Indexers) (Watcher, error) {
var store cache.Store
var queue workqueue.Interface
var oldobject runtime.Object
var cachedObject runtime.Object
informer, _, err := NewInformer(client, resource, opts, indexers)
if err != nil {
return nil, err
Expand All @@ -131,15 +131,15 @@ func NewNamedWatcher(name string, client kubernetes.Interface, resource Resource

ctx, cancel := context.WithCancel(context.TODO())
w := &watcher{
client: client,
informer: informer,
store: store,
queue: queue,
ctx: ctx,
oldobject: oldobject,
stop: cancel,
logger: logp.NewLogger("kubernetes"),
handler: NoOpEventHandlerFuncs{},
client: client,
informer: informer,
store: store,
queue: queue,
ctx: ctx,
cachedObject: cachedObject,
stop: cancel,
logger: logp.NewLogger("kubernetes"),
handler: NoOpEventHandlerFuncs{},
}

w.informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
Expand Down Expand Up @@ -186,8 +186,8 @@ func (w *watcher) Client() kubernetes.Interface {
}

// Oldbject returns the old object in cache during the last updated event
func (w *watcher) Oldobject() runtime.Object {
return w.oldobject
func (w *watcher) CachedObject() runtime.Object {
return w.cachedObject
}

// Start watching pods
Expand Down Expand Up @@ -234,7 +234,7 @@ func (w *watcher) cacheObject(o interface{}) {
if old, ok := o.(runtime.Object); !ok {
utilruntime.HandleError(fmt.Errorf("expected object in cache got %#v", o))
} else {
w.oldobject = old
w.cachedObject = old
}
}

Expand Down

0 comments on commit a63bbf3

Please sign in to comment.