Skip to content

Commit

Permalink
Use RequeueAfter to poll instead of SyncPeriod
Browse files Browse the repository at this point in the history
When polling for changes to resources which can't be watched (i.e.
current alerts in alertmanager), controller-runtime recommends setting
RequeueAfter individually on each result over reducing the global
SyncPeriod. This reduces load on the API server, and avoids
re-reconciling any resources which have already been reconciled within
the polling interval for another reason, such as a watch event.
  • Loading branch information
cbroglie committed Jun 23, 2022
1 parent 5dcd1b5 commit e28a039
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cmd/sciuro/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ func main() {
entryLog := log.WithName("entrypoint")

mgr, err := manager.New(clientconfig.GetConfigOrDie(), manager.Options{
SyncPeriod: &cfg.NodeResync,
LeaderElection: true,
LeaderElectionID: cfg.LeaderElectionID,
LeaderElectionNamespace: cfg.LeaderElectionNamespace,
Expand Down Expand Up @@ -118,6 +117,7 @@ func main() {
mgr.GetClient(),
log.WithName("reconciler"),
metrics.Registry,
cfg.NodeResync,
cfg.ReconcileTimeout,
cfg.LingerResolvedDuration,
as,
Expand Down
7 changes: 5 additions & 2 deletions internal/node/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const (
type nodeStatusReconciler struct {
c client.Client
log logr.Logger
resyncInterval time.Duration
reconcileTimeout time.Duration
linger time.Duration
alertCache alert.Cache
Expand Down Expand Up @@ -71,6 +72,7 @@ func NewNodeStatusReconciler(
c client.Client,
log logr.Logger,
prom prometheus.Registerer,
resyncInterval,
reconcileTimeout,
linger time.Duration,
ac alert.Cache,
Expand All @@ -87,6 +89,7 @@ func NewNodeStatusReconciler(
return &nodeStatusReconciler{
c: c,
log: log,
resyncInterval: resyncInterval,
reconcileTimeout: reconcileTimeout,
linger: linger,
alertCache: ac,
Expand Down Expand Up @@ -115,14 +118,14 @@ func (n *nodeStatusReconciler) Reconcile(ctx context.Context, request reconcile.
return reconcile.Result{}, err
}
if equality.Semantic.DeepEqual(desiredNode, currentNode) {
return reconcile.Result{}, nil
return reconcile.Result{RequeueAfter: n.resyncInterval}, nil
}
patch := client.MergeFrom(currentNode)
if err := n.c.Status().Patch(ctx, desiredNode, patch); err != nil {
log.Error(err, "could not patch node")
return reconcile.Result{}, err
}
return reconcile.Result{}, nil
return reconcile.Result{RequeueAfter: n.resyncInterval}, nil
}

func (n *nodeStatusReconciler) updateNodeStatuses(log logr.Logger, node *corev1.Node) error {
Expand Down
7 changes: 4 additions & 3 deletions internal/node/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var (
)

func Test_Reconcile(t *testing.T) {
const resyncInterval = 2 * time.Minute
tests := []struct {
name string
updateMocks func(c *mockK8SClient, cache *mockAlertCache)
Expand Down Expand Up @@ -85,7 +86,7 @@ func Test_Reconcile(t *testing.T) {
}
c.On("Patch", mock.Anything, mock.MatchedBy(match), mock.Anything, mock.Anything).Return(nil)
},
want: reconcile.Result{},
want: reconcile.Result{RequeueAfter: resyncInterval},
wantErr: false,
},
{
Expand Down Expand Up @@ -129,7 +130,7 @@ func Test_Reconcile(t *testing.T) {
},
).Return(nil)
},
want: reconcile.Result{},
want: reconcile.Result{RequeueAfter: resyncInterval},
wantErr: false,
},
}
Expand All @@ -141,7 +142,7 @@ func Test_Reconcile(t *testing.T) {
c := &mockK8SClient{}
ac := &mockAlertCache{}
tt.updateMocks(c, ac)
n := NewNodeStatusReconciler(c, logr.Discard(), prometheus.NewRegistry(), time.Minute, time.Minute, ac)
n := NewNodeStatusReconciler(c, logr.Discard(), prometheus.NewRegistry(), resyncInterval, time.Minute, time.Minute, ac)
got, err := n.Reconcile(context.Background(), request)
if (err != nil) != tt.wantErr {
t.Errorf("Reconcile() error = %v, wantErr %v", err, tt.wantErr)
Expand Down

0 comments on commit e28a039

Please sign in to comment.