Skip to content

Commit

Permalink
Merge pull request #85 from utkarsh-pro/utkarsh-pro/improve-meshsync-…
Browse files Browse the repository at this point in the history
…updates

Ignore redundant updates
  • Loading branch information
leecalcote authored Sep 11, 2021
2 parents b69726d + aecde7a commit b7a70d6
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions internal/pipeline/handlers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package pipeline

import (
"fmt"
"strconv"

"github.com/layer5io/meshkit/broker"
"github.com/layer5io/meshsync/pkg/model"

Expand All @@ -15,12 +18,33 @@ func (c *ResourceWatcher) startWatching(s cache.SharedIndexInformer) {
c.publishItem(obj.(*unstructured.Unstructured), broker.Add)
},
UpdateFunc: func(oldObj, obj interface{}) {
c.log.Info("received update event for:", obj.(*unstructured.Unstructured).GetName())
c.publishItem(obj.(*unstructured.Unstructured), broker.Update)
oldObjCasted := oldObj.(*unstructured.Unstructured)
objCasted := obj.(*unstructured.Unstructured)

oldRV, _ := strconv.ParseInt(oldObjCasted.GetResourceVersion(), 0, 64)
newRV, _ := strconv.ParseInt(oldObjCasted.GetResourceVersion(), 0, 64)

if oldRV < newRV {
c.log.Info("received update event for:", objCasted.GetName())

c.publishItem(objCasted, broker.Update)
} else {
c.log.Info(fmt.Sprintf(
"skipping update event for: %s => [No changes detected]: %d %d",
objCasted.GetName(),
oldRV,
newRV,
))
}
},
DeleteFunc: func(obj interface{}) {
c.log.Info("received delete event for:", obj.(*unstructured.Unstructured).GetName())
c.publishItem(obj.(*unstructured.Unstructured), broker.Delete)
// It is not guaranteed that this will always be unstructured
// to avoid panicking check if it is truly unstructured
objCasted, ok := obj.(*unstructured.Unstructured)
if ok {
c.log.Info("received delete event for:", objCasted.GetName())
c.publishItem(objCasted, broker.Delete)
}
},
}
s.AddEventHandler(handlers)
Expand Down

0 comments on commit b7a70d6

Please sign in to comment.