Skip to content

Commit

Permalink
Check that snapshot.Status is not nil when checking Status properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsessler7 committed Oct 29, 2024
1 parent 515bc3e commit 0f21106
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
6 changes: 3 additions & 3 deletions internal/controller/postgrescluster/snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (r *Reconciler) reconcileVolumeSnapshots(ctx context.Context,
r.Recorder.Event(postgrescluster, corev1.EventTypeWarning, "VolumeSnapshotError",
*snapshotWithLatestError.Status.Error.Message)
for _, snapshot := range snapshots.Items {
if snapshot.Status.Error != nil &&
if snapshot.Status != nil && snapshot.Status.Error != nil &&
snapshot.Status.Error.Time.Before(snapshotWithLatestError.Status.Error.Time) {
err = r.deleteControlled(ctx, postgrescluster, &snapshot)
if err != nil {
Expand Down Expand Up @@ -537,7 +537,7 @@ func getSnapshotWithLatestError(snapshots *volumesnapshotv1.VolumeSnapshotList)
},
}
for _, snapshot := range snapshots.Items {
if snapshot.Status.Error != nil &&
if snapshot.Status != nil && snapshot.Status.Error != nil &&
snapshotWithLatestError.Status.Error.Time.Before(snapshot.Status.Error.Time) {
snapshotWithLatestError = snapshot
}
Expand Down Expand Up @@ -577,7 +577,7 @@ func getLatestReadySnapshot(snapshots *volumesnapshotv1.VolumeSnapshotList) *vol
},
}
for _, snapshot := range snapshots.Items {
if snapshot.Status.ReadyToUse != nil && *snapshot.Status.ReadyToUse &&
if snapshot.Status != nil && snapshot.Status.ReadyToUse != nil && *snapshot.Status.ReadyToUse &&
latestReadySnapshot.Status.CreationTime.Before(snapshot.Status.CreationTime) {
latestReadySnapshot = snapshot
}
Expand Down
22 changes: 22 additions & 0 deletions internal/controller/postgrescluster/snapshots_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,17 @@ func TestGetSnapshotWithLatestError(t *testing.T) {
assert.Check(t, snapshotWithLatestError == nil)
})

t.Run("NoSnapshotsWithStatus", func(t *testing.T) {
snapshotList := &volumesnapshotv1.VolumeSnapshotList{
Items: []volumesnapshotv1.VolumeSnapshot{
{},
{},
},
}
snapshotWithLatestError := getSnapshotWithLatestError(snapshotList)
assert.Check(t, snapshotWithLatestError == nil)
})

t.Run("NoSnapshotsWithErrors", func(t *testing.T) {
snapshotList := &volumesnapshotv1.VolumeSnapshotList{
Items: []volumesnapshotv1.VolumeSnapshot{
Expand Down Expand Up @@ -1203,6 +1214,17 @@ func TestGetLatestReadySnapshot(t *testing.T) {
assert.Assert(t, latestReadySnapshot == nil)
})

t.Run("NoSnapshotsWithStatus", func(t *testing.T) {
snapshotList := &volumesnapshotv1.VolumeSnapshotList{
Items: []volumesnapshotv1.VolumeSnapshot{
{},
{},
},
}
latestReadySnapshot := getLatestReadySnapshot(snapshotList)
assert.Assert(t, latestReadySnapshot == nil)
})

t.Run("NoReadySnapshots", func(t *testing.T) {
snapshotList := &volumesnapshotv1.VolumeSnapshotList{
Items: []volumesnapshotv1.VolumeSnapshot{
Expand Down

0 comments on commit 0f21106

Please sign in to comment.