Skip to content

Commit b2a8a74

Browse files
authored
Merge pull request #11 from hoppscale/custom-reconciliation-requeue-interval
Use custom requeue interval for reconciliation
2 parents 3386874 + 00bf373 commit b2a8a74

File tree

6 files changed

+128
-64
lines changed

6 files changed

+128
-64
lines changed

cmd/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"flag"
77
"os"
88
"path/filepath"
9+
"time"
910

1011
"go.uber.org/zap/zapcore"
1112
_ "k8s.io/client-go/plugin/pkg/client/auth"
@@ -50,6 +51,7 @@ func main() {
5051
var enableHTTP2 bool
5152
var tlsOpts []func(*tls.Config)
5253
var operatorInstanceName string
54+
var reconciliationRequeueInterval time.Duration
5355

5456
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metrics endpoint binds to. "+
5557
"Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service.")
@@ -66,6 +68,8 @@ func main() {
6668
flag.BoolVar(&enableHTTP2, "enable-http2", false,
6769
"If set, HTTP/2 will be enabled for the metrics and webhook servers")
6870
flag.StringVar(&operatorInstanceName, "operator-instance-name", "", "The name of this operator instance.")
71+
flag.DurationVar(&reconciliationRequeueInterval, "reconciliation-requeue-interval", 5*time.Minute,
72+
"Default interval between resource reconciliation")
6973

7074
opts := zap.Options{
7175
Development: true,
@@ -180,6 +184,7 @@ func main() {
180184
if err = (&controller.PostgresDatabaseReconciler{
181185
Client: mgr.GetClient(),
182186
Scheme: mgr.GetScheme(),
187+
RequeueInterval: reconciliationRequeueInterval,
183188
PGPools: pgpools,
184189
OperatorInstanceName: operatorInstanceName,
185190
}).SetupWithManager(mgr); err != nil {
@@ -190,6 +195,7 @@ func main() {
190195
if err = (&controller.PostgresRoleReconciler{
191196
Client: mgr.GetClient(),
192197
Scheme: mgr.GetScheme(),
198+
RequeueInterval: reconciliationRequeueInterval,
193199
PGPools: pgpools,
194200
OperatorInstanceName: operatorInstanceName,
195201
CacheRolePasswords: cacheRolePasswords,
@@ -200,6 +206,7 @@ func main() {
200206
if err = (&controller.PostgresSchemaReconciler{
201207
Client: mgr.GetClient(),
202208
Scheme: mgr.GetScheme(),
209+
RequeueInterval: reconciliationRequeueInterval,
203210
PGPools: pgpools,
204211
OperatorInstanceName: operatorInstanceName,
205212
}).SetupWithManager(mgr); err != nil {

deploy/charts/managed-postgres-operator/templates/deployment.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ spec:
4949
{{- if .Values.operatorInstanceName }}
5050
- --operator-instance-name={{ .Values.operatorInstanceName }}
5151
{{- end }}
52+
{{- if .Values.reconciliationRequeueInterval }}
53+
- --reconciliation-requeue-interval={{ .Values.reconciliationRequeueInterval }}
54+
{{- end }}
5255
{{- with .Values.extraEnv }}
5356
env:
5457
{{- toYaml . | nindent 12 }}

deploy/charts/managed-postgres-operator/values.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ serviceAccount:
2020
name: ""
2121

2222
operatorInstanceName: ""
23+
reconciliationRequeueInterval: ""
2324

2425
extraEnv: []
2526
envFrom: []

internal/controller/postgresdatabase_controller.go

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ import (
77
"time"
88

99
"github.com/go-logr/logr"
10+
"golang.org/x/time/rate"
1011

1112
"k8s.io/apimachinery/pkg/runtime"
13+
"k8s.io/client-go/util/workqueue"
1214
ctrl "sigs.k8s.io/controller-runtime"
1315
"sigs.k8s.io/controller-runtime/pkg/client"
16+
"sigs.k8s.io/controller-runtime/pkg/controller"
1417
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
1518
"sigs.k8s.io/controller-runtime/pkg/log"
19+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
1620

1721
managedpostgresoperatorhoppscalecomv1alpha1 "github.com/hoppscale/managed-postgres-operator/api/v1alpha1"
1822
"github.com/hoppscale/managed-postgres-operator/internal/postgresql"
@@ -27,6 +31,8 @@ type PostgresDatabaseReconciler struct {
2731
Scheme *runtime.Scheme
2832
logging logr.Logger
2933

34+
RequeueInterval time.Duration
35+
3036
PGPools *postgresql.PGPools
3137
OperatorInstanceName string
3238
}
@@ -37,23 +43,20 @@ type PostgresDatabaseReconciler struct {
3743
func (r *PostgresDatabaseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
3844
r.logging = log.FromContext(ctx)
3945

40-
ctrlSuccessResult := ctrl.Result{RequeueAfter: time.Minute}
41-
ctrlFailResult := ctrl.Result{}
42-
4346
resource := &managedpostgresoperatorhoppscalecomv1alpha1.PostgresDatabase{}
4447

4548
if err := r.Client.Get(ctx, req.NamespacedName, resource); err != nil {
46-
return ctrlFailResult, client.IgnoreNotFound(err)
49+
return r.Result(client.IgnoreNotFound(err))
4750
}
4851

4952
// Skip reconcile if the resource is not managed by this operator
5053
if !utils.IsManagedByOperatorInstance(resource.ObjectMeta.Annotations, r.OperatorInstanceName) {
51-
return ctrlSuccessResult, nil
54+
return r.Result(nil)
5255
}
5356

5457
existingDatabase, err := postgresql.GetDatabase(r.PGPools.Default, resource.Spec.Name)
5558
if err != nil {
56-
return ctrlFailResult, fmt.Errorf("failed to retrieve database: %s", err)
59+
return r.Result(fmt.Errorf("failed to retrieve database: %s", err))
5760
}
5861

5962
desiredDatabase := postgresql.Database{
@@ -62,36 +65,37 @@ func (r *PostgresDatabaseReconciler) Reconcile(ctx context.Context, req ctrl.Req
6265
Extensions: resource.Spec.Extensions,
6366
}
6467

65-
//
66-
// Deletion logic
67-
//
68-
6968
if resource.ObjectMeta.DeletionTimestamp.IsZero() {
7069
if !controllerutil.ContainsFinalizer(resource, PostgresDatabaseFinalizer) {
7170
controllerutil.AddFinalizer(resource, PostgresDatabaseFinalizer)
7271
if err := r.Update(ctx, resource); err != nil {
73-
return ctrlFailResult, err
72+
return r.Result(err)
7473
}
7574
}
7675
} else {
76+
77+
//
78+
// Deletion logic
79+
//
80+
7781
// If there is no finalizer, delete the resource immediately
7882
if !controllerutil.ContainsFinalizer(resource, PostgresDatabaseFinalizer) {
79-
return ctrlSuccessResult, nil
83+
return r.Result(nil)
8084
}
8185

8286
err = r.reconcileOnDeletion(resource, existingDatabase)
8387
if err != nil {
84-
return ctrlFailResult, err
88+
return r.Result(err)
8589
}
8690

8791
// Remove our finalizer from the list and update it.
8892
controllerutil.RemoveFinalizer(resource, PostgresDatabaseFinalizer)
8993
if err := r.Update(ctx, resource); err != nil {
90-
return ctrlFailResult, err
94+
return r.Result(err)
9195
}
9296

9397
// Stop reconciliation as the item is being deleted
94-
return ctrlSuccessResult, nil
98+
return r.Result(nil)
9599
}
96100

97101
//
@@ -100,12 +104,12 @@ func (r *PostgresDatabaseReconciler) Reconcile(ctx context.Context, req ctrl.Req
100104

101105
err = r.reconcileOnCreation(existingDatabase, &desiredDatabase)
102106
if err != nil {
103-
return ctrlFailResult, err
107+
return r.Result(err)
104108
}
105109

106110
err = r.reconcileExtensions(&desiredDatabase)
107111
if err != nil {
108-
return ctrlFailResult, err
112+
return r.Result(err)
109113
}
110114

111115
for roleName, rolePrivileges := range resource.Spec.PrivilegesByRole {
@@ -115,28 +119,42 @@ func (r *PostgresDatabaseReconciler) Reconcile(ctx context.Context, req ctrl.Req
115119
r.convertPrivilegesSpecToList(rolePrivileges),
116120
)
117121
if err != nil {
118-
return ctrlFailResult, err
122+
return r.Result(err)
119123
}
120124
}
121125

122126
if !resource.Status.Succeeded {
123127
resource.Status.Succeeded = true
124128
if err = r.Client.Status().Update(context.Background(), resource); err != nil {
125-
return ctrlFailResult, fmt.Errorf("failed to update object: %s", err)
129+
return r.Result(fmt.Errorf("failed to update object: %s", err))
126130
}
127131
}
128132

129-
return ctrlSuccessResult, nil
133+
return r.Result(nil)
130134
}
131135

132136
// SetupWithManager sets up the controller with the Manager.
133137
func (r *PostgresDatabaseReconciler) SetupWithManager(mgr ctrl.Manager) error {
134138
return ctrl.NewControllerManagedBy(mgr).
135139
For(&managedpostgresoperatorhoppscalecomv1alpha1.PostgresDatabase{}).
136140
Named("postgresdatabase").
141+
WithOptions(controller.Options{
142+
RateLimiter: workqueue.NewTypedMaxOfRateLimiter(
143+
workqueue.NewTypedItemExponentialFailureRateLimiter[reconcile.Request](time.Second, r.RequeueInterval),
144+
&workqueue.TypedBucketRateLimiter[reconcile.Request]{Limiter: rate.NewLimiter(rate.Limit(10), 100)},
145+
),
146+
}).
137147
Complete(r)
138148
}
139149

150+
// Result builds reconciler result depending on error
151+
func (r *PostgresDatabaseReconciler) Result(err error) (ctrl.Result, error) {
152+
if err != nil {
153+
return ctrl.Result{}, err
154+
}
155+
return ctrl.Result{RequeueAfter: r.RequeueInterval}, nil
156+
}
157+
140158
// reconcileOnDeletion performs all actions related to deleting the resource
141159
func (r *PostgresDatabaseReconciler) reconcileOnDeletion(resource *managedpostgresoperatorhoppscalecomv1alpha1.PostgresDatabase, existingDatabase *postgresql.Database) (err error) {
142160
if existingDatabase == nil {

0 commit comments

Comments
 (0)