Skip to content

Commit 255fda4

Browse files
committed
Update the requeue logic for cases where the status could not be updated
1 parent f6d3b23 commit 255fda4

File tree

4 files changed

+46
-18
lines changed

4 files changed

+46
-18
lines changed

controllers/backup_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func (r *FoundationDBBackupReconciler) Reconcile(
111111
continue
112112
}
113113

114-
return processRequeue(req, subReconciler, backup, r.Recorder, backupLog)
114+
return processResult(processRequeue(req, subReconciler, backup, r.Recorder, backupLog))
115115
}
116116

117117
if backup.Status.Generations.Reconciled < originalGeneration {

controllers/cluster_controller.go

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ import (
3030
"syscall"
3131
"time"
3232

33-
"k8s.io/utils/ptr"
3433
"k8s.io/apimachinery/pkg/api/equality"
34+
"k8s.io/utils/ptr"
3535

3636
"github.com/FoundationDB/fdb-kubernetes-operator/v2/pkg/fdbadminclient"
3737
"github.com/FoundationDB/fdb-kubernetes-operator/v2/pkg/podmanager"
@@ -181,15 +181,23 @@ func (r *FoundationDBClusterReconciler) Reconcile(
181181
ctx context.Context,
182182
request ctrl.Request,
183183
) (ctrl.Result, error) {
184+
return processResult(r.doReconcile(ctx, request))
185+
}
186+
187+
// doReconcile will run the reconcile logic for the FoundationDBClusterReconciler.
188+
func (r *FoundationDBClusterReconciler) doReconcile(
189+
ctx context.Context,
190+
request ctrl.Request,
191+
) (*ctrl.Result, error) {
184192
cluster := &fdbv1beta2.FoundationDBCluster{}
185193

186194
err := r.Get(ctx, request.NamespacedName, cluster)
187195
if err != nil {
188196
if k8serrors.IsNotFound(err) {
189-
return ctrl.Result{}, nil
197+
return nil, nil
190198
}
191199
// Error reading the object - requeue the request.
192-
return ctrl.Result{}, err
200+
return nil, err
193201
}
194202

195203
clusterLog := globalControllerLogger.WithValues(
@@ -206,6 +214,7 @@ func (r *FoundationDBClusterReconciler) Reconcile(
206214

207215
originalStatus := cluster.Status.DeepCopy()
208216
startTime := time.Now()
217+
result := &ctrl.Result{}
209218
defer func() {
210219
// If the cluster.Status has changed compared to the original Status, we have to update the status.
211220
// See: https://github.com/kubernetes-sigs/kubebuilder/issues/592
@@ -216,6 +225,10 @@ func (r *FoundationDBClusterReconciler) Reconcile(
216225
err = r.updateOrApply(ctx, cluster)
217226
if err != nil {
218227
clusterLog.Error(err, "Error updating cluster clusterStatus")
228+
// If no requeue is planned, we should target a requeue to ensure that the status will be updated.
229+
if result.IsZero() || result.RequeueAfter == 0 {
230+
result.RequeueAfter = 2 * time.Second
231+
}
219232
}
220233
}
221234

@@ -232,33 +245,33 @@ func (r *FoundationDBClusterReconciler) Reconcile(
232245
if cluster.Spec.Skip {
233246
clusterLog.Info("Skipping cluster with skip value true", "skip", cluster.Spec.Skip)
234247
// Don't requeue
235-
return ctrl.Result{}, nil
248+
return nil, nil
236249
}
237250

238251
err = internal.NormalizeClusterSpec(cluster, r.DeprecationOptions)
239252
if err != nil {
240-
return ctrl.Result{}, err
253+
return nil, nil
241254
}
242255

243256
err = cluster.Validate()
244257
if err != nil {
245258
r.Recorder.Event(cluster, corev1.EventTypeWarning, "ClusterSpec not valid", err.Error())
246-
return ctrl.Result{}, fmt.Errorf("ClusterSpec is not valid: %w", err)
259+
return nil, fmt.Errorf("ClusterSpec is not valid: %w", err)
247260
}
248261

249262
adminClient, err := r.getAdminClient(clusterLog, cluster)
250263
if err != nil {
251-
return ctrl.Result{}, err
264+
return nil, err
252265
}
253266
defer func() {
254267
_ = adminClient.Close()
255268
}()
256269
supportedVersion, err := adminClient.VersionSupported(cluster.Spec.Version)
257270
if err != nil {
258-
return ctrl.Result{}, err
271+
return nil, err
259272
}
260273
if !supportedVersion {
261-
return ctrl.Result{}, fmt.Errorf("version %s is not supported", cluster.Spec.Version)
274+
return nil, fmt.Errorf("version %s is not supported", cluster.Spec.Version)
262275
}
263276

264277
// When using DNS entries in the cluster file, we want to make sure to create pods if required before doing any
@@ -336,13 +349,13 @@ func (r *FoundationDBClusterReconciler) Reconcile(
336349
process, err := os.FindProcess(os.Getpid())
337350
if err != nil {
338351
fmt.Printf("Error finding process: %v\n", err)
339-
return ctrl.Result{RequeueAfter: 5 * time.Second}, err
352+
return &ctrl.Result{RequeueAfter: 5 * time.Second}, err
340353
}
341354

342355
err = process.Signal(syscall.SIGTERM)
343356
if err != nil {
344357
fmt.Printf("Error sending signal: %v\n", err)
345-
return ctrl.Result{RequeueAfter: 5 * time.Second}, err
358+
return &ctrl.Result{RequeueAfter: 5 * time.Second}, err
346359
}
347360
}
348361
}
@@ -399,7 +412,7 @@ func (r *FoundationDBClusterReconciler) Reconcile(
399412
delayedRequeueDuration = 2 * time.Second
400413
}
401414

402-
return ctrl.Result{RequeueAfter: delayedRequeueDuration}, nil
415+
return &ctrl.Result{RequeueAfter: delayedRequeueDuration}, nil
403416
}
404417

405418
clusterLog.Info("Reconciliation complete", "generation", cluster.Status.Generations.Reconciled)
@@ -410,7 +423,7 @@ func (r *FoundationDBClusterReconciler) Reconcile(
410423
fmt.Sprintf("Reconciled generation %d", cluster.Status.Generations.Reconciled),
411424
)
412425

413-
return ctrl.Result{}, nil
426+
return result, nil
414427
}
415428

416429
// runClusterSubReconciler will start the subReconciler and will log the duration of the subReconciler.

controllers/controllers.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func processRequeue(
6464
object runtime.Object,
6565
recorder record.EventRecorder,
6666
logger logr.Logger,
67-
) (ctrl.Result, error) {
67+
) (*ctrl.Result, error) {
6868
curLog := logger.WithValues(
6969
"reconciler",
7070
fmt.Sprintf("%T", subReconciler),
@@ -90,9 +90,23 @@ func processRequeue(
9090
recorder.Event(object, corev1.EventTypeNormal, "ReconciliationTerminatedEarly", requeue.message)
9191
if err != nil {
9292
curLog.Error(err, "Error in reconciliation")
93-
return ctrl.Result{}, err
93+
return nil, err
9494
}
9595
curLog.Info("Reconciliation terminated early", "message", requeue.message)
9696

97-
return ctrl.Result{RequeueAfter: requeue.delay}, nil
97+
return &ctrl.Result{RequeueAfter: requeue.delay}, nil
98+
}
99+
100+
// processResult will return a ctrl.Result and error based on the input, e.g. if the result is nil, it will ensure
101+
// that an empty ctrl.Result is returned.
102+
func processResult(result *ctrl.Result, err error) (ctrl.Result, error) {
103+
if err != nil {
104+
return ctrl.Result{}, err
105+
}
106+
107+
if result.IsZero() {
108+
return ctrl.Result{}, nil
109+
}
110+
111+
return *result, nil
98112
}

controllers/restore_controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ func (r *FoundationDBRestoreReconciler) Reconcile(
8989
if req == nil {
9090
continue
9191
}
92-
return processRequeue(req, subReconciler, restore, r.Recorder, restoreLog)
92+
93+
return processResult(processRequeue(req, subReconciler, restore, r.Recorder, restoreLog))
9394
}
9495

9596
if restore.Status.State != fdbv1beta2.CompletedFoundationDBRestoreState {

0 commit comments

Comments
 (0)