Skip to content

Commit 319875e

Browse files
committed
Add constructors for valid reconcile.Result values
The result list of Reconciler.Reconcile is a pair of types with multiple fields and special values. Some combinations are confusing, ignored, or cause warnings at runtime. The following values can be combined eighteen ways: Result.Requeue = { true, false } Result.RequeueAfter = { negative, zero, positive } error = { nil, non-nil, terminal } These constructors provide names and documentation for four of the valid combinations.
1 parent dc9d21b commit 319875e

15 files changed

+229
-250
lines changed

internal/bridge/crunchybridgecluster/crunchybridgecluster_controller.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"sigs.k8s.io/controller-runtime/pkg/event"
3434

3535
"github.com/crunchydata/postgres-operator/internal/bridge"
36+
"github.com/crunchydata/postgres-operator/internal/controller/runtime"
3637
pgoRuntime "github.com/crunchydata/postgres-operator/internal/controller/runtime"
3738
"github.com/crunchydata/postgres-operator/internal/naming"
3839
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
@@ -152,11 +153,6 @@ func (r *CrunchyBridgeClusterReconciler) Reconcile(ctx context.Context, req ctrl
152153
return ctrl.Result{}, err
153154
} else if result != nil {
154155
if log := log.V(1); log.Enabled() {
155-
if result.RequeueAfter > 0 {
156-
// RequeueAfter implies Requeue, but set both to make the next
157-
// log message more clear.
158-
result.Requeue = true
159-
}
160156
log.Info("deleting", "result", fmt.Sprintf("%+v", *result))
161157
}
162158
return *result, err
@@ -238,7 +234,7 @@ func (r *CrunchyBridgeClusterReconciler) Reconcile(ctx context.Context, req ctrl
238234
// TODO(crunchybridgecluster): Do we want the operator to interrupt
239235
// upgrades created through the GUI/API?
240236
if len(crunchybridgecluster.Status.OngoingUpgrade) != 0 {
241-
return ctrl.Result{RequeueAfter: 3 * time.Minute}, nil
237+
return runtime.RequeueWithoutBackoff(3 * time.Minute), nil
242238
}
243239

244240
// Check if there's an upgrade difference for the three upgradeable fields that hit the upgrade endpoint
@@ -268,7 +264,7 @@ func (r *CrunchyBridgeClusterReconciler) Reconcile(ctx context.Context, req ctrl
268264
log.Info("Reconciled")
269265
// TODO(crunchybridgecluster): do we always want to requeue? Does the Watch mean we
270266
// don't need this, or do we want both?
271-
return ctrl.Result{RequeueAfter: 3 * time.Minute}, nil
267+
return runtime.RequeueWithoutBackoff(3 * time.Minute), nil
272268
}
273269

274270
// reconcileBridgeConnectionSecret looks for the Bridge connection secret specified by the cluster,
@@ -418,7 +414,7 @@ func (r *CrunchyBridgeClusterReconciler) handleCreateCluster(ctx context.Context
418414
Message: "The condition of the upgrade(s) is unknown.",
419415
})
420416

421-
return ctrl.Result{RequeueAfter: 3 * time.Minute}
417+
return runtime.RequeueWithoutBackoff(3 * time.Minute)
422418
}
423419

424420
// handleGetCluster handles getting the cluster details from Bridge and
@@ -579,7 +575,7 @@ func (r *CrunchyBridgeClusterReconciler) handleUpgrade(ctx context.Context,
579575
})
580576
}
581577

582-
return ctrl.Result{RequeueAfter: 3 * time.Minute}
578+
return runtime.RequeueWithoutBackoff(3 * time.Minute)
583579
}
584580

585581
// handleUpgradeHA handles upgrades that hit the
@@ -626,7 +622,7 @@ func (r *CrunchyBridgeClusterReconciler) handleUpgradeHA(ctx context.Context,
626622
})
627623
}
628624

629-
return ctrl.Result{RequeueAfter: 3 * time.Minute}
625+
return runtime.RequeueWithoutBackoff(3 * time.Minute)
630626
}
631627

632628
// handleUpdate handles upgrades that hit the "PATCH /clusters/<id>" endpoint
@@ -671,7 +667,7 @@ func (r *CrunchyBridgeClusterReconciler) handleUpdate(ctx context.Context,
671667
clusterUpdate.ClusterName, *clusterUpdate.IsProtected),
672668
})
673669

674-
return ctrl.Result{RequeueAfter: 3 * time.Minute}
670+
return runtime.RequeueWithoutBackoff(3 * time.Minute)
675671
}
676672

677673
// GetSecretKeys gets the secret and returns the expected API key and team id

internal/bridge/crunchybridgecluster/crunchybridgecluster_controller_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ func TestHandleCreateCluster(t *testing.T) {
197197
cluster.Namespace = ns
198198

199199
controllerResult := reconciler.handleCreateCluster(ctx, testApiKey, testTeamId, cluster)
200-
assert.Equal(t, controllerResult, ctrl.Result{RequeueAfter: 3 * time.Minute})
200+
assert.Equal(t, controllerResult.RequeueAfter, 3*time.Minute)
201201
assert.Equal(t, cluster.Status.ID, "0")
202202

203203
readyCondition := meta.FindStatusCondition(cluster.Status.Conditions, v1beta1.ConditionReady)
@@ -484,7 +484,7 @@ func TestHandleUpgrade(t *testing.T) {
484484
cluster.Spec.Plan = "standard-16" // originally "standard-8"
485485

486486
controllerResult := reconciler.handleUpgrade(ctx, testApiKey, cluster)
487-
assert.Equal(t, controllerResult, ctrl.Result{RequeueAfter: 3 * time.Minute})
487+
assert.Equal(t, controllerResult.RequeueAfter, 3*time.Minute)
488488
upgradingCondition := meta.FindStatusCondition(cluster.Status.Conditions, v1beta1.ConditionUpgrading)
489489
if assert.Check(t, upgradingCondition != nil) {
490490
assert.Equal(t, upgradingCondition.Status, metav1.ConditionTrue)
@@ -506,7 +506,7 @@ func TestHandleUpgrade(t *testing.T) {
506506
cluster.Spec.PostgresVersion = 16 // originally "15"
507507

508508
controllerResult := reconciler.handleUpgrade(ctx, testApiKey, cluster)
509-
assert.Equal(t, controllerResult, ctrl.Result{RequeueAfter: 3 * time.Minute})
509+
assert.Equal(t, controllerResult.RequeueAfter, 3*time.Minute)
510510
upgradingCondition := meta.FindStatusCondition(cluster.Status.Conditions, v1beta1.ConditionUpgrading)
511511
if assert.Check(t, upgradingCondition != nil) {
512512
assert.Equal(t, upgradingCondition.Status, metav1.ConditionTrue)
@@ -528,7 +528,7 @@ func TestHandleUpgrade(t *testing.T) {
528528
cluster.Spec.Storage = resource.MustParse("15Gi") // originally "10Gi"
529529

530530
controllerResult := reconciler.handleUpgrade(ctx, testApiKey, cluster)
531-
assert.Equal(t, controllerResult, ctrl.Result{RequeueAfter: 3 * time.Minute})
531+
assert.Equal(t, controllerResult.RequeueAfter, 3*time.Minute)
532532
upgradingCondition := meta.FindStatusCondition(cluster.Status.Conditions, v1beta1.ConditionUpgrading)
533533
if assert.Check(t, upgradingCondition != nil) {
534534
assert.Equal(t, upgradingCondition.Status, metav1.ConditionTrue)
@@ -592,7 +592,7 @@ func TestHandleUpgradeHA(t *testing.T) {
592592
cluster.Spec.IsHA = true // originally "false"
593593

594594
controllerResult := reconciler.handleUpgradeHA(ctx, testApiKey, cluster)
595-
assert.Equal(t, controllerResult, ctrl.Result{RequeueAfter: 3 * time.Minute})
595+
assert.Equal(t, controllerResult.RequeueAfter, 3*time.Minute)
596596
upgradingCondition := meta.FindStatusCondition(cluster.Status.Conditions, v1beta1.ConditionUpgrading)
597597
if assert.Check(t, upgradingCondition != nil) {
598598
assert.Equal(t, upgradingCondition.Status, metav1.ConditionTrue)
@@ -613,7 +613,7 @@ func TestHandleUpgradeHA(t *testing.T) {
613613
cluster.Status.ID = "2345"
614614

615615
controllerResult := reconciler.handleUpgradeHA(ctx, testApiKey, cluster)
616-
assert.Equal(t, controllerResult, ctrl.Result{RequeueAfter: 3 * time.Minute})
616+
assert.Equal(t, controllerResult.RequeueAfter, 3*time.Minute)
617617
upgradingCondition := meta.FindStatusCondition(cluster.Status.Conditions, v1beta1.ConditionUpgrading)
618618
if assert.Check(t, upgradingCondition != nil) {
619619
assert.Equal(t, upgradingCondition.Status, metav1.ConditionTrue)
@@ -672,7 +672,7 @@ func TestHandleUpdate(t *testing.T) {
672672
cluster.Spec.ClusterName = "new-cluster-name" // originally "hippo-cluster"
673673

674674
controllerResult := reconciler.handleUpdate(ctx, testApiKey, cluster)
675-
assert.Equal(t, controllerResult, ctrl.Result{RequeueAfter: 3 * time.Minute})
675+
assert.Equal(t, controllerResult.RequeueAfter, 3*time.Minute)
676676
upgradingCondition := meta.FindStatusCondition(cluster.Status.Conditions, v1beta1.ConditionUpgrading)
677677
if assert.Check(t, upgradingCondition != nil) {
678678
assert.Equal(t, upgradingCondition.Status, metav1.ConditionTrue)
@@ -690,7 +690,7 @@ func TestHandleUpdate(t *testing.T) {
690690
cluster.Spec.IsProtected = true // originally "false"
691691

692692
controllerResult := reconciler.handleUpdate(ctx, testApiKey, cluster)
693-
assert.Equal(t, controllerResult, ctrl.Result{RequeueAfter: 3 * time.Minute})
693+
assert.Equal(t, controllerResult.RequeueAfter, 3*time.Minute)
694694
upgradingCondition := meta.FindStatusCondition(cluster.Status.Conditions, v1beta1.ConditionUpgrading)
695695
if assert.Check(t, upgradingCondition != nil) {
696696
assert.Equal(t, upgradingCondition.Status, metav1.ConditionTrue)

internal/bridge/crunchybridgecluster/delete.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
ctrl "sigs.k8s.io/controller-runtime"
2323
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
2424

25+
"github.com/crunchydata/postgres-operator/internal/controller/runtime"
26+
"github.com/crunchydata/postgres-operator/internal/initialize"
2527
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
2628
)
2729

@@ -58,7 +60,7 @@ func (r *CrunchyBridgeClusterReconciler) handleDelete(
5860
}
5961

6062
if !deletedAlready {
61-
return &ctrl.Result{RequeueAfter: 1 * time.Second}, err
63+
return initialize.Pointer(runtime.RequeueWithoutBackoff(time.Second)), err
6264
}
6365

6466
// Remove finalizer if deleted already

internal/bridge/crunchybridgecluster/delete_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func TestHandleDeleteCluster(t *testing.T) {
8585
cluster.Status.ID = "1234"
8686
controllerResult, err = reconciler.handleDelete(ctx, cluster, "9012")
8787
assert.NilError(t, err)
88-
assert.Equal(t, *controllerResult, ctrl.Result{RequeueAfter: 1 * time.Second})
88+
assert.Equal(t, controllerResult.RequeueAfter, 1*time.Second)
8989
assert.Equal(t, len(testBridgeClient.Clusters), 1)
9090
assert.Equal(t, testBridgeClient.Clusters[0].ClusterName, "bridge-cluster-2")
9191

internal/bridge/installation.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,15 @@ func (r *InstallationReconciler) Reconcile(
131131
result.RequeueAfter, err = r.reconcile(ctx, secret)
132132
}
133133

134-
// TODO: Check for corev1.NamespaceTerminatingCause after
135-
// k8s.io/[email protected]; see https://issue.k8s.io/108528.
134+
// Nothing can be written to a deleted namespace.
135+
if err != nil && apierrors.HasStatusCause(err, corev1.NamespaceTerminatingCause) {
136+
return runtime.ErrorWithoutBackoff(err)
137+
}
136138

137139
// Write conflicts are returned as errors; log and retry with backoff.
138140
if err != nil && apierrors.IsConflict(err) {
139141
logging.FromContext(ctx).Info("Requeue", "reason", err)
140-
err, result.Requeue, result.RequeueAfter = nil, true, 0
142+
return runtime.RequeueWithBackoff(), nil
141143
}
142144

143145
return result, err

internal/controller/pgupgrade/pgupgrade_controller.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"sigs.k8s.io/controller-runtime/pkg/handler"
3232

3333
"github.com/crunchydata/postgres-operator/internal/config"
34+
"github.com/crunchydata/postgres-operator/internal/controller/runtime"
3435
"github.com/crunchydata/postgres-operator/internal/registration"
3536
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
3637
)
@@ -493,17 +494,15 @@ func (r *PGUpgradeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
493494
}
494495

495496
// Requeue to verify that Patroni endpoints are deleted
496-
return ctrl.Result{Requeue: true}, err // FIXME
497+
return runtime.RequeueWithBackoff(), err // FIXME
497498
}
498499

499500
// TODO: write upgradeJob back to world? No, we will wake and see it when it
500501
// has some progress. OTOH, whatever we just wrote has the latest metadata.generation.
501502
// TODO: consider what it means to "re-use" the same PGUpgrade for more than
502503
// one postgres version. Should the job name include the version number?
503504

504-
log.Info("Reconciled", "requeue", err != nil ||
505-
result.Requeue ||
506-
result.RequeueAfter > 0)
505+
log.Info("Reconciled", "requeue", !result.IsZero() || err != nil)
507506
return
508507
}
509508

0 commit comments

Comments
 (0)