@@ -182,11 +182,9 @@ func (r *Reconciler) reconcileDedicatedSnapshotVolume(
182
182
Namespace : cluster .GetNamespace (),
183
183
Name : existingPVCName ,
184
184
}}
185
- pvc .SetGroupVersionKind (corev1 .SchemeGroupVersion .WithKind ("PersistentVolumeClaim" ))
186
185
} else {
187
186
pvc = & corev1.PersistentVolumeClaim {ObjectMeta : naming .ClusterDedicatedSnapshotVolume (cluster )}
188
187
}
189
-
190
188
pvc .SetGroupVersionKind (corev1 .SchemeGroupVersion .WithKind ("PersistentVolumeClaim" ))
191
189
192
190
// If snapshots are disabled, delete the PVC if it exists and return early.
@@ -236,13 +234,13 @@ func (r *Reconciler) reconcileDedicatedSnapshotVolume(
236
234
// job in existence at a time.
237
235
// TODO(dsessler7): Should this function throw an error or something if multiple
238
236
// DSV restores somehow exist?
239
- restoreJobFound , restoreJob , err := r .getDedicatedSnapshotVolumeRestoreJob (ctx , cluster )
237
+ restoreJob , err := r .getDedicatedSnapshotVolumeRestoreJob (ctx , cluster )
240
238
if err != nil {
241
239
return pvc , err
242
240
}
243
241
244
242
// If we don't find a restore job, we run one.
245
- if ! restoreJobFound {
243
+ if restoreJob == nil {
246
244
err = r .dedicatedSnapshotVolumeRestore (ctx , cluster , pvc , instances , clusterVolumes , backupJob )
247
245
return pvc , err
248
246
}
@@ -281,7 +279,7 @@ func (r *Reconciler) reconcileDedicatedSnapshotVolume(
281
279
}
282
280
283
281
// createDedicatedSnapshotVolume creates/updates/gets the dedicated snapshot volume.
284
- // It expects that the volume name has already been set on the pvc that is passed in.
282
+ // It expects that the volume name and GVK has already been set on the pvc that is passed in.
285
283
// TODO(dsessler7): The above fact makes this method feel a little awkward to me.
286
284
// Not sure this code really needs its own method. I'm tempted to just move it back
287
285
// to reconcileDedicatedSnapshotVolume.
@@ -400,19 +398,6 @@ func (r *Reconciler) dedicatedSnapshotVolumeRestore(ctx context.Context,
400
398
restoreJob .Spec .BackoffLimit = initialize .Int32 (0 )
401
399
restoreJob .Spec .Template .Spec .RestartPolicy = corev1 .RestartPolicyNever
402
400
403
- jobs := & batchv1.JobList {}
404
- selectJobs , err := naming .AsSelector (naming .ClusterBackupJobs (cluster .Name ))
405
- if err == nil {
406
- err = errors .WithStack (
407
- r .Client .List (ctx , jobs ,
408
- client .InNamespace (cluster .Namespace ),
409
- client.MatchingLabelsSelector {Selector : selectJobs },
410
- ))
411
- }
412
- if err != nil {
413
- return err
414
- }
415
-
416
401
// Add pgBackRest configs to template.
417
402
pgbackrest .AddConfigToRestorePod (cluster , cluster , & restoreJob .Spec .Template .Spec )
418
403
@@ -433,7 +418,8 @@ func (r *Reconciler) dedicatedSnapshotVolumeRestore(ctx context.Context,
433
418
// provided backup job's UID.
434
419
func (r * Reconciler ) generateSnapshotOfDedicatedSnapshotVolume (
435
420
postgrescluster * v1beta1.PostgresCluster ,
436
- dedicatedSnapshotVolume * corev1.PersistentVolumeClaim ) (* volumesnapshotv1.VolumeSnapshot , error ) {
421
+ dedicatedSnapshotVolume * corev1.PersistentVolumeClaim ,
422
+ ) (* volumesnapshotv1.VolumeSnapshot , error ) {
437
423
438
424
snapshot , err := r .generateVolumeSnapshot (postgrescluster , * dedicatedSnapshotVolume ,
439
425
postgrescluster .Spec .Backups .Snapshots .VolumeSnapshotClassName )
@@ -451,8 +437,8 @@ func (r *Reconciler) generateSnapshotOfDedicatedSnapshotVolume(
451
437
// PersistentVolumeClaim and VolumeSnapshotClassName and will set the provided
452
438
// PostgresCluster as the owner.
453
439
func (r * Reconciler ) generateVolumeSnapshot (postgrescluster * v1beta1.PostgresCluster ,
454
- pvc corev1.PersistentVolumeClaim ,
455
- volumeSnapshotClassName string ) (* volumesnapshotv1.VolumeSnapshot , error ) {
440
+ pvc corev1.PersistentVolumeClaim , volumeSnapshotClassName string ,
441
+ ) (* volumesnapshotv1.VolumeSnapshot , error ) {
456
442
457
443
snapshot := & volumesnapshotv1.VolumeSnapshot {
458
444
TypeMeta : metav1.TypeMeta {
@@ -478,12 +464,11 @@ func (r *Reconciler) generateVolumeSnapshot(postgrescluster *v1beta1.PostgresClu
478
464
// getDedicatedSnapshotVolumeRestoreJob finds a dedicated snapshot volume (DSV)
479
465
// restore job if one exists. Since we delete successful restore jobs and stop
480
466
// creating new restore jobs when one fails, there should only ever be one DSV
481
- // restore job available at a time.
467
+ // restore job present at a time. If a DSV restore cannot be found, we return nil .
482
468
func (r * Reconciler ) getDedicatedSnapshotVolumeRestoreJob (ctx context.Context ,
483
- postgrescluster * v1beta1.PostgresCluster ) (bool , * batchv1.Job , error ) {
469
+ postgrescluster * v1beta1.PostgresCluster ) (* batchv1.Job , error ) {
484
470
485
471
// Get all restore jobs for this cluster
486
- var restoreJobFound bool
487
472
jobs := & batchv1.JobList {}
488
473
selectJobs , err := naming .AsSelector (naming .ClusterRestoreJobs (postgrescluster .Name ))
489
474
if err == nil {
@@ -494,20 +479,18 @@ func (r *Reconciler) getDedicatedSnapshotVolumeRestoreJob(ctx context.Context,
494
479
))
495
480
}
496
481
if err != nil {
497
- return restoreJobFound , nil , err
482
+ return nil , err
498
483
}
499
484
500
485
// Get restore job that has PGBackRestBackupJobCompletion annotation
501
- var restoreJob batchv1.Job
502
486
for _ , job := range jobs .Items {
503
487
_ , annotationExists := job .GetAnnotations ()[naming .PGBackRestBackupJobCompletion ]
504
488
if annotationExists {
505
- restoreJob = job
506
- restoreJobFound = true
489
+ return & job , nil
507
490
}
508
491
}
509
492
510
- return restoreJobFound , & restoreJob , nil
493
+ return nil , nil
511
494
}
512
495
513
496
// getLatestCompleteBackupJob finds the most recently completed
@@ -614,6 +597,8 @@ func getLatestReadySnapshot(snapshots *volumesnapshotv1.VolumeSnapshotList) *vol
614
597
return & latestReadySnapshot
615
598
}
616
599
600
+ // deleteSnapshots takes a postgrescluster and a snapshot list and deletes all snapshots
601
+ // in the list that are controlled by the provided postgrescluster.
617
602
func (r * Reconciler ) deleteSnapshots (ctx context.Context ,
618
603
postgrescluster * v1beta1.PostgresCluster , snapshots * volumesnapshotv1.VolumeSnapshotList ) error {
619
604
0 commit comments