Skip to content

Commit 25afc75

Browse files
committed
Add debug logging
1 parent 35d61aa commit 25afc75

File tree

2 files changed

+84
-8
lines changed

2 files changed

+84
-8
lines changed

internal/controller/postgrescluster/controller.go

+58-7
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ type Reconciler struct {
8181

8282
// Reconcile reconciles a ConfigMap in a namespace managed by the PostgreSQL Operator
8383
func (r *Reconciler) Reconcile(
84-
ctx context.Context, request reconcile.Request) (reconcile.Result, error,
85-
) {
84+
ctx context.Context, request reconcile.Request,
85+
) (reconcile.Result, error) {
8686
ctx, span := r.Tracer.Start(ctx, "Reconcile")
8787
log := logging.FromContext(ctx)
8888
defer span.End()
@@ -100,6 +100,11 @@ func (r *Reconciler) Reconcile(
100100
return runtime.ErrorWithBackoff(err)
101101
}
102102

103+
log.V(1).Info("starting reconciliation",
104+
"cluster", request.Name,
105+
"namespace", request.Namespace,
106+
"clusterwide", cluster.Spec.Standby == nil)
107+
103108
// Set any defaults that may not have been stored in the API. No DeepCopy
104109
// is necessary because controller-runtime makes a copy before returning
105110
// from its cache.
@@ -111,6 +116,19 @@ func (r *Reconciler) Reconcile(
111116

112117
// Keep a copy of cluster prior to any manipulations.
113118
before := cluster.DeepCopy()
119+
var err error
120+
result := reconcile.Result{}
121+
defer func() {
122+
if !equality.Semantic.DeepEqual(before.Status, cluster.Status) {
123+
statusErr := r.Client.Status().Patch(ctx, cluster, client.MergeFrom(before), r.Owner)
124+
if statusErr != nil {
125+
log.Error(statusErr, "patching cluster status")
126+
}
127+
if err == nil {
128+
err = statusErr
129+
}
130+
}
131+
}()
114132

115133
// NOTE(cbandy): When a namespace is deleted, objects owned by a
116134
// PostgresCluster may be deleted before the PostgresCluster is deleted.
@@ -235,8 +253,11 @@ func (r *Reconciler) Reconcile(
235253
// Set huge_pages = try if a hugepages resource limit > 0, otherwise set "off"
236254
postgres.SetHugePages(cluster, &pgParameters)
237255

256+
result := reconcile.Result{}
257+
238258
if err == nil {
239259
rootCA, err = r.reconcileRootCertificate(ctx, cluster)
260+
log.V(1).Info("reconciled root certificate", "cluster", cluster.Name, "error", err)
240261
}
241262

242263
if err == nil {
@@ -248,43 +269,49 @@ func (r *Reconciler) Reconcile(
248269
// return is no longer needed, and reconciliation can proceed normally.
249270
returnEarly, err := r.reconcileDirMoveJobs(ctx, cluster)
250271
if err != nil || returnEarly {
272+
log.V(1).Info("waiting for directory move jobs", "cluster", cluster.Name, "error", err)
251273
return runtime.ErrorWithBackoff(errors.Join(err, patchClusterStatus()))
252274
}
253275
}
254276
if err == nil {
255277
clusterVolumes, err = r.observePersistentVolumeClaims(ctx, cluster)
278+
log.V(1).Info("observed persistent volume claims", "cluster", cluster.Name, "count", len(clusterVolumes), "error", err)
256279
}
257280
if err == nil {
258281
clusterVolumes, err = r.configureExistingPVCs(ctx, cluster, clusterVolumes)
282+
log.V(1).Info("configured existing PVCs", "cluster", cluster.Name, "count", len(clusterVolumes), "error", err)
259283
}
260284
if err == nil {
261285
instances, err = r.observeInstances(ctx, cluster)
286+
log.V(1).Info("observed instances", "cluster", cluster.Name, "error", err)
262287
}
263288

264-
result := reconcile.Result{}
265-
266289
if err == nil {
267290
var requeue time.Duration
268291
if requeue, err = r.reconcilePatroniStatus(ctx, cluster, instances); err == nil && requeue > 0 {
269292
result.RequeueAfter = requeue
293+
log.V(1).Info("requeuing for patroni status", "cluster", cluster.Name, "after", requeue)
270294
}
271295
}
272296
if err == nil {
273297
err = r.reconcilePatroniSwitchover(ctx, cluster, instances)
298+
log.V(1).Info("reconciled patroni switchover", "cluster", cluster.Name, "error", err)
274299
}
275300
// reconcile the Pod service before reconciling any data source in case it is necessary
276301
// to start Pods during data source reconciliation that require network connections (e.g.
277302
// if it is necessary to start a dedicated repo host to bootstrap a new cluster using its
278303
// own existing backups).
279304
if err == nil {
280305
clusterPodService, err = r.reconcileClusterPodService(ctx, cluster)
306+
log.V(1).Info("reconciled cluster pod service", "cluster", cluster.Name, "error", err)
281307
}
282308
// reconcile the RBAC resources before reconciling any data source in case
283309
// restore/move Job pods require the ServiceAccount to access any data source.
284310
// e.g., we are restoring from an S3 source using an IAM for access
285311
// - https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts-technical-overview.html
286312
if err == nil {
287313
instanceServiceAccount, err = r.reconcileRBACResources(ctx, cluster)
314+
log.V(1).Info("reconciled RBAC resources", "cluster", cluster.Name, "error", err)
288315
}
289316
// First handle reconciling any data source configured for the PostgresCluster. This includes
290317
// reconciling the data source defined to bootstrap a new cluster, as well as a reconciling
@@ -298,88 +325,112 @@ func (r *Reconciler) Reconcile(
298325
// can proceed normally.
299326
returnEarly, err := r.reconcileDataSource(ctx, cluster, instances, clusterVolumes, rootCA)
300327
if err != nil || returnEarly {
328+
log.V(1).Info("waiting for data source initialization", "cluster", cluster.Name, "error", err)
301329
return runtime.ErrorWithBackoff(errors.Join(err, patchClusterStatus()))
302330
}
303331
}
304332
if err == nil {
333+
log.V(1).Info("reconciling cluster config map", "cluster", cluster.Name)
305334
clusterConfigMap, err = r.reconcileClusterConfigMap(ctx, cluster, pgHBAs, pgParameters)
306335
}
307336
if err == nil {
337+
log.V(1).Info("reconciling replication secret", "cluster", cluster.Name)
308338
clusterReplicationSecret, err = r.reconcileReplicationSecret(ctx, cluster, rootCA)
309339
}
310340
if err == nil {
341+
log.V(1).Info("reconciling patroni leader lease", "cluster", cluster.Name)
311342
patroniLeaderService, err = r.reconcilePatroniLeaderLease(ctx, cluster)
312343
}
313344
if err == nil {
345+
log.V(1).Info("reconciling cluster primary service", "cluster", cluster.Name)
314346
primaryService, err = r.reconcileClusterPrimaryService(ctx, cluster, patroniLeaderService)
315347
}
316348
if err == nil {
349+
log.V(1).Info("reconciling cluster replica service", "cluster", cluster.Name)
317350
replicaService, err = r.reconcileClusterReplicaService(ctx, cluster)
318351
}
319352
if err == nil {
353+
log.V(1).Info("reconciling cluster certificate", "cluster", cluster.Name)
320354
primaryCertificate, err = r.reconcileClusterCertificate(ctx, rootCA, cluster, primaryService, replicaService)
321355
}
322356
if err == nil {
357+
log.V(1).Info("reconciling patroni distributed configuration", "cluster", cluster.Name)
323358
err = r.reconcilePatroniDistributedConfiguration(ctx, cluster)
324359
}
325360
if err == nil {
361+
log.V(1).Info("reconciling patroni dynamic configuration", "cluster", cluster.Name)
326362
err = r.reconcilePatroniDynamicConfiguration(ctx, cluster, instances, pgHBAs, pgParameters)
327363
}
328364
if err == nil {
365+
log.V(1).Info("reconciling monitoring secret", "cluster", cluster.Name)
329366
monitoringSecret, err = r.reconcileMonitoringSecret(ctx, cluster)
330367
}
331368
if err == nil {
369+
log.V(1).Info("reconciling exporter queries config", "cluster", cluster.Name)
332370
exporterQueriesConfig, err = r.reconcileExporterQueriesConfig(ctx, cluster)
333371
}
334372
if err == nil {
373+
log.V(1).Info("reconciling exporter web config", "cluster", cluster.Name)
335374
exporterWebConfig, err = r.reconcileExporterWebConfig(ctx, cluster)
336375
}
337376
if err == nil {
377+
log.V(1).Info("reconciling instance sets", "cluster", cluster.Name)
338378
err = r.reconcileInstanceSets(
339379
ctx, cluster, clusterConfigMap, clusterReplicationSecret, rootCA,
340380
clusterPodService, instanceServiceAccount, instances, patroniLeaderService,
341381
primaryCertificate, clusterVolumes, exporterQueriesConfig, exporterWebConfig)
342382
}
343383

344384
if err == nil {
385+
log.V(1).Info("reconciling postgres databases", "cluster", cluster.Name)
345386
err = r.reconcilePostgresDatabases(ctx, cluster, instances)
346387
}
347388
if err == nil {
389+
log.V(1).Info("reconciling postgres users", "cluster", cluster.Name)
348390
err = r.reconcilePostgresUsers(ctx, cluster, instances)
349391
}
350392

351393
if err == nil {
394+
log.V(1).Info("reconciling pgbackrest", "cluster", cluster.Name)
352395
var next reconcile.Result
353396
if next, err = r.reconcilePGBackRest(ctx, cluster, instances, rootCA); err == nil && !next.IsZero() {
354397
result.Requeue = result.Requeue || next.Requeue
355398
if next.RequeueAfter > 0 {
356399
result.RequeueAfter = next.RequeueAfter
400+
log.V(1).Info("requeuing for pgbackrest", "cluster", cluster.Name, "after", next.RequeueAfter)
357401
}
358402
}
359403
}
360404
if err == nil {
405+
log.V(1).Info("reconciling pgbouncer", "cluster", cluster.Name)
361406
err = r.reconcilePGBouncer(ctx, cluster, instances, primaryCertificate, rootCA)
362407
}
363408
if err == nil {
409+
log.V(1).Info("reconciling pgmonitor", "cluster", cluster.Name)
364410
err = r.reconcilePGMonitor(ctx, cluster, instances, monitoringSecret)
365411
}
366412
if err == nil {
413+
log.V(1).Info("reconciling database init sql", "cluster", cluster.Name)
367414
err = r.reconcileDatabaseInitSQL(ctx, cluster, instances)
368415
}
369416
if err == nil {
417+
log.V(1).Info("reconciling pgadmin", "cluster", cluster.Name)
370418
err = r.reconcilePGAdmin(ctx, cluster)
371419
}
372420
if err == nil {
373-
// This is after [Reconciler.rolloutInstances] to ensure that recreating
374-
// Pods takes precedence.
421+
log.V(1).Info("handling patroni restarts", "cluster", cluster.Name)
375422
err = r.handlePatroniRestarts(ctx, cluster, instances)
376423
}
377424

378425
// at this point everything reconciled successfully, and we can update the
379426
// observedGeneration
380427
cluster.Status.ObservedGeneration = cluster.GetGeneration()
381428

382-
log.V(1).Info("reconciled cluster")
429+
log.V(1).Info("reconciliation completed",
430+
"cluster", cluster.Name,
431+
"error", err,
432+
"requeue", result.Requeue,
433+
"requeueAfter", result.RequeueAfter)
383434

384435
return result, errors.Join(err, patchClusterStatus())
385436
}

percona/controller/pgcluster/controller.go

+26-1
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,11 @@ func (r *PGClusterReconciler) Reconcile(ctx context.Context, request reconcile.R
176176
return ctrl.Result{}, errors.Wrap(err, "get PerconaPGCluster")
177177
}
178178

179+
log.V(1).Info("starting reconciliation",
180+
"cluster", cr.Name,
181+
"namespace", cr.Namespace,
182+
"clusterwide", cr.Spec.OpenShift != nil)
183+
179184
cr.Default()
180185

181186
if cr.Spec.OpenShift == nil {
@@ -190,6 +195,7 @@ func (r *PGClusterReconciler) Reconcile(ctx context.Context, request reconcile.R
190195
}
191196

192197
if err := r.ensureFinalizers(ctx, cr); err != nil {
198+
log.Error(err, "failed to ensure finalizers")
193199
return reconcile.Result{}, errors.Wrap(err, "ensure finalizers")
194200
}
195201

@@ -198,6 +204,7 @@ func (r *PGClusterReconciler) Reconcile(ctx context.Context, request reconcile.R
198204

199205
// We're deleting PostgresCluster explicitly to let Crunchy controller run its finalizers and not mess with us.
200206
if err := r.Client.Delete(ctx, postgresCluster); client.IgnoreNotFound(err) != nil {
207+
log.Error(err, "failed to delete PostgresCluster")
201208
return ctrl.Result{RequeueAfter: 5 * time.Second}, errors.Wrap(err, "delete postgres cluster")
202209
}
203210

@@ -207,49 +214,60 @@ func (r *PGClusterReconciler) Reconcile(ctx context.Context, request reconcile.R
207214
}
208215

209216
if err := r.runFinalizers(ctx, cr); err != nil {
217+
log.Error(err, "failed to run finalizers")
210218
return reconcile.Result{RequeueAfter: 5 * time.Second}, errors.Wrap(err, "run finalizers")
211219
}
212220

213221
return reconcile.Result{}, nil
214222
}
215223

216224
if err := r.reconcileTLS(ctx, cr); err != nil {
225+
log.Error(err, "failed to reconcile TLS")
217226
return reconcile.Result{}, errors.Wrap(err, "reconcile TLS")
218227
}
219228

220229
if err := r.reconcileExternalWatchers(ctx, cr); err != nil {
230+
log.Error(err, "failed to reconcile external watchers")
221231
return reconcile.Result{}, errors.Wrap(err, "start external watchers")
222232
}
223233

224234
if err := r.reconcileVersion(ctx, cr); err != nil {
235+
log.Error(err, "failed to reconcile version")
225236
return reconcile.Result{}, errors.Wrap(err, "reconcile version")
226237
}
227238

228239
if err := r.reconcileBackups(ctx, cr); err != nil {
240+
log.Error(err, "failed to reconcile backups")
229241
return reconcile.Result{}, errors.Wrap(err, "reconcile backups")
230242
}
231243

232244
if err := r.createBootstrapRestoreObject(ctx, cr); err != nil {
245+
log.Error(err, "failed to create bootstrap restore object")
233246
return reconcile.Result{}, errors.Wrap(err, "reconcile restore")
234247
}
235248

236249
if err := r.reconcilePMM(ctx, cr); err != nil {
250+
log.Error(err, "failed to reconcile PMM")
237251
return reconcile.Result{}, errors.Wrap(err, "failed to add pmm sidecar")
238252
}
239253

240254
if err := r.handleMonitorUserPassChange(ctx, cr); err != nil {
255+
log.Error(err, "failed to handle monitor user password change")
241256
return reconcile.Result{}, errors.Wrap(err, "failed to handle monitor user password change")
242257
}
243258

259+
log.V(1).Info("reconciling custom extensions", "cluster", cr.Name)
244260
r.reconcileCustomExtensions(cr)
245261

246262
if err := r.reconcileScheduledBackups(ctx, cr); err != nil {
263+
log.Error(err, "failed to reconcile scheduled backups")
247264
return reconcile.Result{}, errors.Wrap(err, "reconcile scheduled backups")
248265
}
249266

250267
if cr.Spec.Pause != nil && *cr.Spec.Pause {
251268
backupRunning, err := isBackupRunning(ctx, r.Client, cr)
252269
if err != nil {
270+
log.Error(err, "failed to check if backup is running")
253271
return reconcile.Result{}, errors.Wrap(err, "is backup running")
254272
}
255273
if backupRunning {
@@ -261,28 +279,35 @@ func (r *PGClusterReconciler) Reconcile(ctx context.Context, request reconcile.R
261279
opRes, err := controllerutil.CreateOrUpdate(ctx, r.Client, postgresCluster, func() error {
262280
var err error
263281
postgresCluster, err = cr.ToCrunchy(ctx, postgresCluster, r.Client.Scheme())
264-
265282
return err
266283
})
267284
if err != nil {
285+
log.Error(err, "failed to create/update PostgresCluster")
268286
return ctrl.Result{}, errors.Wrap(err, "update/create PostgresCluster")
269287
}
270288

271289
// postgresCluster will not be available immediately after creation.
272290
// We should wait some time, it's better to continue on the next reconcile
273291
if opRes == controllerutil.OperationResultCreated {
292+
log.V(1).Info("PostgresCluster created, waiting for next reconcile", "cluster", cr.Name)
274293
return ctrl.Result{RequeueAfter: 1 * time.Second}, nil
275294
}
276295

277296
if err := r.Client.Get(ctx, client.ObjectKeyFromObject(postgresCluster), postgresCluster); err != nil {
297+
log.Error(err, "failed to get PostgresCluster")
278298
return ctrl.Result{}, errors.Wrap(err, "get PostgresCluster")
279299
}
280300

281301
err = r.updateStatus(ctx, cr, &postgresCluster.Status)
282302
if err != nil {
303+
log.Error(err, "failed to update status")
283304
return ctrl.Result{}, errors.Wrap(err, "update status")
284305
}
285306

307+
log.V(1).Info("reconciliation completed",
308+
"cluster", cr.Name,
309+
"namespace", cr.Namespace)
310+
286311
return ctrl.Result{}, nil
287312
}
288313

0 commit comments

Comments
 (0)