Skip to content

Commit

Permalink
Remove print statement and incorporate review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
vjsamuel committed Jul 20, 2020
1 parent ca872dd commit ba464e1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
1 change: 0 additions & 1 deletion libbeat/autodiscover/providers/kubernetes/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ func isUpdated(o, n interface{}) bool {
newCopy.ResourceVersion = ""

// If the old object and new object are different in either meta or spec then there is a valid change
fmt.Println(equality.Semantic.DeepDerivative(oldCopy.Spec, newCopy.Spec), equality.Semantic.DeepDerivative(oldCopy.ObjectMeta, newCopy.ObjectMeta))
if !equality.Semantic.DeepEqual(oldCopy.Spec, newCopy.Spec) || !equality.Semantic.DeepEqual(oldCopy.ObjectMeta, newCopy.ObjectMeta) {
return true
}
Expand Down
23 changes: 16 additions & 7 deletions libbeat/common/kubernetes/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ type WatchOptions struct {
Node string
// Namespace is used for filtering watched resource to given namespace, use "" for all namespaces
Namespace string

// IsUpdated allows registering a func that allows the invoker of the Watch to decide what amounts to an update
// vs what does not.
IsUpdated func(old, new interface{}) bool
}

Expand Down Expand Up @@ -102,6 +103,19 @@ func NewWatcher(client kubernetes.Interface, resource Resource, opts WatchOption
queue = workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), objType)
ctx, cancel := context.WithCancel(context.Background())

if opts.IsUpdated == nil {
opts.IsUpdated = func(o, n interface{}) bool {
old, _ := accessor.ResourceVersion(o.(runtime.Object))
new, _ := accessor.ResourceVersion(n.(runtime.Object))

// Only enqueue changes that have a different resource versions to avoid processing resyncs.
if old != new {
return true
}
return false
}
}

w := &watcher{
client: client,
informer: informer,
Expand All @@ -121,12 +135,7 @@ func NewWatcher(client kubernetes.Interface, resource Resource, opts WatchOption
w.enqueue(o, delete)
},
UpdateFunc: func(o, n interface{}) {
old, _ := accessor.ResourceVersion(o.(runtime.Object))
new, _ := accessor.ResourceVersion(n.(runtime.Object))

// Only enqueue changes that have a different resource versions to avoid processing resyncs.
// Also if there is a registered function to check for updates, use it to check for updates.
if old != new && opts.IsUpdated != nil && opts.IsUpdated(o, n) {
if opts.IsUpdated(o, n) {
w.enqueue(n, update)
}
},
Expand Down

0 comments on commit ba464e1

Please sign in to comment.