Skip to content

Commit 0f21106

Browse files
committed
Check that snapshot.Status is not nil when checking Status properties.
1 parent 515bc3e commit 0f21106

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

internal/controller/postgrescluster/snapshots.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (r *Reconciler) reconcileVolumeSnapshots(ctx context.Context,
103103
r.Recorder.Event(postgrescluster, corev1.EventTypeWarning, "VolumeSnapshotError",
104104
*snapshotWithLatestError.Status.Error.Message)
105105
for _, snapshot := range snapshots.Items {
106-
if snapshot.Status.Error != nil &&
106+
if snapshot.Status != nil && snapshot.Status.Error != nil &&
107107
snapshot.Status.Error.Time.Before(snapshotWithLatestError.Status.Error.Time) {
108108
err = r.deleteControlled(ctx, postgrescluster, &snapshot)
109109
if err != nil {
@@ -537,7 +537,7 @@ func getSnapshotWithLatestError(snapshots *volumesnapshotv1.VolumeSnapshotList)
537537
},
538538
}
539539
for _, snapshot := range snapshots.Items {
540-
if snapshot.Status.Error != nil &&
540+
if snapshot.Status != nil && snapshot.Status.Error != nil &&
541541
snapshotWithLatestError.Status.Error.Time.Before(snapshot.Status.Error.Time) {
542542
snapshotWithLatestError = snapshot
543543
}
@@ -577,7 +577,7 @@ func getLatestReadySnapshot(snapshots *volumesnapshotv1.VolumeSnapshotList) *vol
577577
},
578578
}
579579
for _, snapshot := range snapshots.Items {
580-
if snapshot.Status.ReadyToUse != nil && *snapshot.Status.ReadyToUse &&
580+
if snapshot.Status != nil && snapshot.Status.ReadyToUse != nil && *snapshot.Status.ReadyToUse &&
581581
latestReadySnapshot.Status.CreationTime.Before(snapshot.Status.CreationTime) {
582582
latestReadySnapshot = snapshot
583583
}

internal/controller/postgrescluster/snapshots_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,17 @@ func TestGetSnapshotWithLatestError(t *testing.T) {
978978
assert.Check(t, snapshotWithLatestError == nil)
979979
})
980980

981+
t.Run("NoSnapshotsWithStatus", func(t *testing.T) {
982+
snapshotList := &volumesnapshotv1.VolumeSnapshotList{
983+
Items: []volumesnapshotv1.VolumeSnapshot{
984+
{},
985+
{},
986+
},
987+
}
988+
snapshotWithLatestError := getSnapshotWithLatestError(snapshotList)
989+
assert.Check(t, snapshotWithLatestError == nil)
990+
})
991+
981992
t.Run("NoSnapshotsWithErrors", func(t *testing.T) {
982993
snapshotList := &volumesnapshotv1.VolumeSnapshotList{
983994
Items: []volumesnapshotv1.VolumeSnapshot{
@@ -1203,6 +1214,17 @@ func TestGetLatestReadySnapshot(t *testing.T) {
12031214
assert.Assert(t, latestReadySnapshot == nil)
12041215
})
12051216

1217+
t.Run("NoSnapshotsWithStatus", func(t *testing.T) {
1218+
snapshotList := &volumesnapshotv1.VolumeSnapshotList{
1219+
Items: []volumesnapshotv1.VolumeSnapshot{
1220+
{},
1221+
{},
1222+
},
1223+
}
1224+
latestReadySnapshot := getLatestReadySnapshot(snapshotList)
1225+
assert.Assert(t, latestReadySnapshot == nil)
1226+
})
1227+
12061228
t.Run("NoReadySnapshots", func(t *testing.T) {
12071229
snapshotList := &volumesnapshotv1.VolumeSnapshotList{
12081230
Items: []volumesnapshotv1.VolumeSnapshot{

0 commit comments

Comments
 (0)