Skip to content

Commit eab9b88

Browse files
dsessler7gkech
authored andcommitted
Check that snapshot.Status is not nil when checking Status properties.
1 parent c19c4ed commit eab9b88

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
@@ -100,7 +100,7 @@ func (r *Reconciler) reconcileVolumeSnapshots(ctx context.Context,
100100
r.Recorder.Event(postgrescluster, corev1.EventTypeWarning, "VolumeSnapshotError",
101101
*snapshotWithLatestError.Status.Error.Message)
102102
for _, snapshot := range snapshots.Items {
103-
if snapshot.Status.Error != nil &&
103+
if snapshot.Status != nil && snapshot.Status.Error != nil &&
104104
snapshot.Status.Error.Time.Before(snapshotWithLatestError.Status.Error.Time) {
105105
err = r.deleteControlled(ctx, postgrescluster, &snapshot)
106106
if err != nil {
@@ -536,7 +536,7 @@ func getSnapshotWithLatestError(snapshots *volumesnapshotv1.VolumeSnapshotList)
536536
},
537537
}
538538
for _, snapshot := range snapshots.Items {
539-
if snapshot.Status.Error != nil &&
539+
if snapshot.Status != nil && snapshot.Status.Error != nil &&
540540
snapshotWithLatestError.Status.Error.Time.Before(snapshot.Status.Error.Time) {
541541
snapshotWithLatestError = snapshot
542542
}
@@ -576,7 +576,7 @@ func getLatestReadySnapshot(snapshots *volumesnapshotv1.VolumeSnapshotList) *vol
576576
},
577577
}
578578
for _, snapshot := range snapshots.Items {
579-
if snapshot.Status.ReadyToUse != nil && *snapshot.Status.ReadyToUse &&
579+
if snapshot.Status != nil && snapshot.Status.ReadyToUse != nil && *snapshot.Status.ReadyToUse &&
580580
latestReadySnapshot.Status.CreationTime.Before(snapshot.Status.CreationTime) {
581581
latestReadySnapshot = snapshot
582582
}

internal/controller/postgrescluster/snapshots_test.go

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

1101+
t.Run("NoSnapshotsWithStatus", func(t *testing.T) {
1102+
snapshotList := &volumesnapshotv1.VolumeSnapshotList{
1103+
Items: []volumesnapshotv1.VolumeSnapshot{
1104+
{},
1105+
{},
1106+
},
1107+
}
1108+
snapshotWithLatestError := getSnapshotWithLatestError(snapshotList)
1109+
assert.Check(t, snapshotWithLatestError == nil)
1110+
})
1111+
11011112
t.Run("NoSnapshotsWithErrors", func(t *testing.T) {
11021113
snapshotList := &volumesnapshotv1.VolumeSnapshotList{
11031114
Items: []volumesnapshotv1.VolumeSnapshot{
@@ -1323,6 +1334,17 @@ func TestGetLatestReadySnapshot(t *testing.T) {
13231334
assert.Assert(t, latestReadySnapshot == nil)
13241335
})
13251336

1337+
t.Run("NoSnapshotsWithStatus", func(t *testing.T) {
1338+
snapshotList := &volumesnapshotv1.VolumeSnapshotList{
1339+
Items: []volumesnapshotv1.VolumeSnapshot{
1340+
{},
1341+
{},
1342+
},
1343+
}
1344+
latestReadySnapshot := getLatestReadySnapshot(snapshotList)
1345+
assert.Assert(t, latestReadySnapshot == nil)
1346+
})
1347+
13261348
t.Run("NoReadySnapshots", func(t *testing.T) {
13271349
snapshotList := &volumesnapshotv1.VolumeSnapshotList{
13281350
Items: []volumesnapshotv1.VolumeSnapshot{

0 commit comments

Comments
 (0)