Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reconciliation logic #223

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading