From 1604c441b43e0ccda1c55797a17801c9f93ed278 Mon Sep 17 00:00:00 2001 From: aalexand Date: Fri, 16 Aug 2024 15:42:44 +0300 Subject: [PATCH] fix: reconciliation logic --- pkg/sync/manager/controller.go | 35 ++++++++++------------------------ 1 file changed, 10 insertions(+), 25 deletions(-) diff --git a/pkg/sync/manager/controller.go b/pkg/sync/manager/controller.go index f816ca3..41cfb5e 100644 --- a/pkg/sync/manager/controller.go +++ b/pkg/sync/manager/controller.go @@ -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 { @@ -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() @@ -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 {