Skip to content

Commit

Permalink
Agents stop update managedcluster status when clock is out of sync.
Browse files Browse the repository at this point in the history
Signed-off-by: xuezhaojun <zxue@redhat.com>
  • Loading branch information
xuezhaojun committed Dec 18, 2024
1 parent 17e05da commit 8ff2ec6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
5 changes: 3 additions & 2 deletions pkg/registration/hub/lease/clocksynccontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ func (c *clockSyncController) sync(ctx context.Context, syncCtx factory.SyncCont
return err
}
// When the agent's lease get renewed, the "now" on hub should close to the RenewTime on agent.
// If the two time are not close(over 1 lease duration), we assume the clock is out of sync.
oneLeaseDuration := time.Duration(LeaseDurationSeconds) * time.Second
// If the two time are not close(the same duration in the lease controller), we assume the clock is out of sync.
// Then, if the Clock is out of sync, the agent will not be able to update the status of managed cluster.
oneLeaseDuration := time.Duration(LeaseDurationSeconds*leaseDurationTimes) * time.Second
if err := c.updateClusterStatusClockSynced(ctx, cluster,
now.Sub(observedLease.Spec.RenewTime.Time) < oneLeaseDuration && observedLease.Spec.RenewTime.Time.Sub(now) < oneLeaseDuration); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/registration/hub/lease/clocksynccontroller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestClockSyncController(t *testing.T) {
testinghelpers.NewManagedCluster(),
},
leases: []runtime.Object{
testinghelpers.NewManagedClusterLease("managed-cluster-lease", now.Add(61*time.Second)),
testinghelpers.NewManagedClusterLease("managed-cluster-lease", now.Add(301*time.Second)),
},
validateActions: func(t *testing.T, leaseActions, clusterActions []clienttesting.Action) {
expected := metav1.Condition{
Expand Down
9 changes: 9 additions & 0 deletions pkg/registration/spoke/managedcluster/status_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type managedClusterStatusController struct {
patcher patcher.Patcher[*clusterv1.ManagedCluster, clusterv1.ManagedClusterSpec, clusterv1.ManagedClusterStatus]
hubClusterLister clusterv1listers.ManagedClusterLister
hubEventRecorder kevents.EventRecorder
recorder events.Recorder
}

type statusReconcile interface {
Expand Down Expand Up @@ -97,6 +98,7 @@ func newManagedClusterStatusController(
},
hubClusterLister: hubClusterInformer.Lister(),
hubEventRecorder: hubEventRecorder,
recorder: recorder,
}
}

Expand All @@ -121,6 +123,13 @@ func (c *managedClusterStatusController) sync(ctx context.Context, syncCtx facto
}
}

// check if managedcluster's clock is out of sync, if so, the agent will not be able to update the status of managed cluster.
outOfSynced := meta.IsStatusConditionFalse(newCluster.Status.Conditions, clusterv1.ManagedClusterConditionClockSynced)
if outOfSynced {
c.recorder.Eventf("ClockOutOfSync", "The managed cluster's clock is out of sync, the agent will not be able to update the status of managed cluster.")
return fmt.Errorf("the managed cluster's clock is out of sync, the agent will not be able to update the status of managed cluster.")
}

changed, err := c.patcher.PatchStatus(ctx, newCluster, newCluster.Status, cluster.Status)
if err != nil {
errs = append(errs, err)
Expand Down

0 comments on commit 8ff2ec6

Please sign in to comment.