Skip to content

Commit

Permalink
fix: reconciliation logic (#223)
Browse files Browse the repository at this point in the history
Co-authored-by: aalexand <aalexand@adobe.com>
  • Loading branch information
aalexandru and aalexand authored Aug 16, 2024
1 parent 0d26aef commit 53108d7
Showing 1 changed file with 10 additions and 25 deletions.
35 changes: 10 additions & 25 deletions pkg/sync/manager/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,23 @@ func (c *SyncController) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
instance.Status.LastSyncError = ptr.To(errList[0].Error())
instance.Status.LastSyncTime = &metav1.Time{Time: time.Now()}
log.Error(errList[0], "failed to sync resources")

if err := c.updateStatus(ctx, instance); err != nil {
return requeueAfter(10*time.Second, err)
}
return noRequeue()
}

err = c.reconcile(ctx, instance)
syncedData, err := c.ResourceParser.Diff()
if err != nil {
log.Error(err, "failed to reconcile")
return noRequeue()
}

instance.Status.SyncedData = ptr.To(string(syncedData))

if c.shouldEnqueueData(instance) {
instance.Status.SyncedDataHash = ptr.To(hash(instance.Status.SyncedData))
instance.Status.LastSyncTime = &metav1.Time{Time: time.Now()}
if err := c.enqueueData(instance); err != nil {
log.Error(err, "failed to enqueue message")
if err := c.updateStatus(ctx, instance); err != nil {
Expand Down Expand Up @@ -257,28 +264,6 @@ func (c *SyncController) enqueueRequestsFromMapFunc(gvk schema.GroupVersionKind)
}
}

// reconcile merges the provided patch with the initial data and updates the status of the ClusterSync object
func (c *SyncController) reconcile(ctx context.Context, instance *registryv1alpha1.ClusterSync) error {
syncedData, err := c.ResourceParser.Diff()
if err != nil {
return err
}

instance.Status.SyncedData = ptr.To(string(syncedData))

if !c.hasDifferentHash(instance) {
c.Log.Info("no data changes detected",
"name", instance.GetName(),
"namespace", instance.GetNamespace())
return nil
}

instance.Status.SyncedDataHash = ptr.To(hash(instance.Status.SyncedData))
instance.Status.LastSyncTime = &metav1.Time{Time: time.Now()}

return c.updateStatus(ctx, instance)
}

func (c *SyncController) enqueueData(instance *registryv1alpha1.ClusterSync) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
Expand Down Expand Up @@ -318,7 +303,7 @@ func (c *SyncController) enqueueData(instance *registryv1alpha1.ClusterSync) err
}

func (c *SyncController) shouldEnqueueData(instance *registryv1alpha1.ClusterSync) bool {
return c.isLastSyncSuccessful(instance)
return c.isLastSyncSuccessful(instance) && c.hasDifferentHash(instance)
}

func (c *SyncController) hasDifferentHash(object runtime.Object) bool {
Expand Down

0 comments on commit 53108d7

Please sign in to comment.