Skip to content

Commit 8adb575

Browse files
committed
Add go tests for latest iteration of volume snapshots feature. Other minor changes.
1 parent 63c3d1a commit 8adb575

File tree

4 files changed

+919
-60
lines changed

4 files changed

+919
-60
lines changed

internal/controller/postgrescluster/helpers_test.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,17 +141,19 @@ func testCluster() *v1beta1.PostgresCluster {
141141
return cluster.DeepCopy()
142142
}
143143

144-
func testJob(clusterName string) *batchv1.Job {
144+
func testBackupJob(cluster *v1beta1.PostgresCluster) *batchv1.Job {
145145
job := batchv1.Job{
146146
TypeMeta: metav1.TypeMeta{
147147
APIVersion: batchv1.SchemeGroupVersion.String(),
148148
Kind: "Job",
149149
},
150150
ObjectMeta: metav1.ObjectMeta{
151-
Name: "backup-job-1",
151+
Name: "backup-job-1",
152+
Namespace: cluster.Namespace,
152153
Labels: map[string]string{
153-
naming.LabelCluster: clusterName,
154+
naming.LabelCluster: cluster.Name,
154155
naming.LabelPGBackRestBackup: "",
156+
naming.LabelPGBackRestRepo: "repo1",
155157
},
156158
},
157159
Spec: batchv1.JobSpec{
@@ -167,6 +169,30 @@ func testJob(clusterName string) *batchv1.Job {
167169
return job.DeepCopy()
168170
}
169171

172+
func testRestoreJob(cluster *v1beta1.PostgresCluster) *batchv1.Job {
173+
job := batchv1.Job{
174+
TypeMeta: metav1.TypeMeta{
175+
APIVersion: batchv1.SchemeGroupVersion.String(),
176+
Kind: "Job",
177+
},
178+
ObjectMeta: metav1.ObjectMeta{
179+
Name: "restore-job-1",
180+
Namespace: cluster.Namespace,
181+
Labels: naming.PGBackRestRestoreJobLabels(cluster.Name),
182+
},
183+
Spec: batchv1.JobSpec{
184+
Template: corev1.PodTemplateSpec{
185+
Spec: corev1.PodSpec{
186+
Containers: []corev1.Container{{Name: "test", Image: "test"}},
187+
RestartPolicy: corev1.RestartPolicyNever,
188+
},
189+
},
190+
},
191+
}
192+
193+
return job.DeepCopy()
194+
}
195+
170196
// setupManager creates the runtime manager used during controller testing
171197
func setupManager(t *testing.T, cfg *rest.Config,
172198
controllerSetup func(mgr manager.Manager)) (context.Context, context.CancelFunc) {

internal/controller/postgrescluster/snapshots.go

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,9 @@ func (r *Reconciler) reconcileDedicatedSnapshotVolume(
182182
Namespace: cluster.GetNamespace(),
183183
Name: existingPVCName,
184184
}}
185-
pvc.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("PersistentVolumeClaim"))
186185
} else {
187186
pvc = &corev1.PersistentVolumeClaim{ObjectMeta: naming.ClusterDedicatedSnapshotVolume(cluster)}
188187
}
189-
190188
pvc.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("PersistentVolumeClaim"))
191189

192190
// If snapshots are disabled, delete the PVC if it exists and return early.
@@ -236,13 +234,13 @@ func (r *Reconciler) reconcileDedicatedSnapshotVolume(
236234
// job in existence at a time.
237235
// TODO(dsessler7): Should this function throw an error or something if multiple
238236
// DSV restores somehow exist?
239-
restoreJobFound, restoreJob, err := r.getDedicatedSnapshotVolumeRestoreJob(ctx, cluster)
237+
restoreJob, err := r.getDedicatedSnapshotVolumeRestoreJob(ctx, cluster)
240238
if err != nil {
241239
return pvc, err
242240
}
243241

244242
// If we don't find a restore job, we run one.
245-
if !restoreJobFound {
243+
if restoreJob == nil {
246244
err = r.dedicatedSnapshotVolumeRestore(ctx, cluster, pvc, instances, clusterVolumes, backupJob)
247245
return pvc, err
248246
}
@@ -281,7 +279,7 @@ func (r *Reconciler) reconcileDedicatedSnapshotVolume(
281279
}
282280

283281
// 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.
285283
// TODO(dsessler7): The above fact makes this method feel a little awkward to me.
286284
// Not sure this code really needs its own method. I'm tempted to just move it back
287285
// to reconcileDedicatedSnapshotVolume.
@@ -400,19 +398,6 @@ func (r *Reconciler) dedicatedSnapshotVolumeRestore(ctx context.Context,
400398
restoreJob.Spec.BackoffLimit = initialize.Int32(0)
401399
restoreJob.Spec.Template.Spec.RestartPolicy = corev1.RestartPolicyNever
402400

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-
416401
// Add pgBackRest configs to template.
417402
pgbackrest.AddConfigToRestorePod(cluster, cluster, &restoreJob.Spec.Template.Spec)
418403

@@ -433,7 +418,8 @@ func (r *Reconciler) dedicatedSnapshotVolumeRestore(ctx context.Context,
433418
// provided backup job's UID.
434419
func (r *Reconciler) generateSnapshotOfDedicatedSnapshotVolume(
435420
postgrescluster *v1beta1.PostgresCluster,
436-
dedicatedSnapshotVolume *corev1.PersistentVolumeClaim) (*volumesnapshotv1.VolumeSnapshot, error) {
421+
dedicatedSnapshotVolume *corev1.PersistentVolumeClaim,
422+
) (*volumesnapshotv1.VolumeSnapshot, error) {
437423

438424
snapshot, err := r.generateVolumeSnapshot(postgrescluster, *dedicatedSnapshotVolume,
439425
postgrescluster.Spec.Backups.Snapshots.VolumeSnapshotClassName)
@@ -451,8 +437,8 @@ func (r *Reconciler) generateSnapshotOfDedicatedSnapshotVolume(
451437
// PersistentVolumeClaim and VolumeSnapshotClassName and will set the provided
452438
// PostgresCluster as the owner.
453439
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) {
456442

457443
snapshot := &volumesnapshotv1.VolumeSnapshot{
458444
TypeMeta: metav1.TypeMeta{
@@ -478,12 +464,11 @@ func (r *Reconciler) generateVolumeSnapshot(postgrescluster *v1beta1.PostgresClu
478464
// getDedicatedSnapshotVolumeRestoreJob finds a dedicated snapshot volume (DSV)
479465
// restore job if one exists. Since we delete successful restore jobs and stop
480466
// 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.
482468
func (r *Reconciler) getDedicatedSnapshotVolumeRestoreJob(ctx context.Context,
483-
postgrescluster *v1beta1.PostgresCluster) (bool, *batchv1.Job, error) {
469+
postgrescluster *v1beta1.PostgresCluster) (*batchv1.Job, error) {
484470

485471
// Get all restore jobs for this cluster
486-
var restoreJobFound bool
487472
jobs := &batchv1.JobList{}
488473
selectJobs, err := naming.AsSelector(naming.ClusterRestoreJobs(postgrescluster.Name))
489474
if err == nil {
@@ -494,20 +479,18 @@ func (r *Reconciler) getDedicatedSnapshotVolumeRestoreJob(ctx context.Context,
494479
))
495480
}
496481
if err != nil {
497-
return restoreJobFound, nil, err
482+
return nil, err
498483
}
499484

500485
// Get restore job that has PGBackRestBackupJobCompletion annotation
501-
var restoreJob batchv1.Job
502486
for _, job := range jobs.Items {
503487
_, annotationExists := job.GetAnnotations()[naming.PGBackRestBackupJobCompletion]
504488
if annotationExists {
505-
restoreJob = job
506-
restoreJobFound = true
489+
return &job, nil
507490
}
508491
}
509492

510-
return restoreJobFound, &restoreJob, nil
493+
return nil, nil
511494
}
512495

513496
// getLatestCompleteBackupJob finds the most recently completed
@@ -614,6 +597,8 @@ func getLatestReadySnapshot(snapshots *volumesnapshotv1.VolumeSnapshotList) *vol
614597
return &latestReadySnapshot
615598
}
616599

600+
// deleteSnapshots takes a postgrescluster and a snapshot list and deletes all snapshots
601+
// in the list that are controlled by the provided postgrescluster.
617602
func (r *Reconciler) deleteSnapshots(ctx context.Context,
618603
postgrescluster *v1beta1.PostgresCluster, snapshots *volumesnapshotv1.VolumeSnapshotList) error {
619604

0 commit comments

Comments
 (0)