Skip to content

Commit 44ee7ac

Browse files
committed
Add a function needsUpdateSnapshotStatus
1 parent ed0e8ab commit 44ee7ac

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

pkg/common-controller/snapshot_controller.go

+36-3
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ func (ctrl *csiSnapshotCommonController) syncContent(content *crdv1.VolumeSnapsh
116116
// Treat the content as bound to a missing snapshot.
117117
snapshot = nil
118118
} else {
119-
// TODO(xyang): Write a function to check if snapshot.Status is different from content.Status and add snapshot to queue if there is a difference and it is worth triggering an snapshot status update
120-
// Check if content status is set to true and update snapshot status if so
121-
if snapshot != nil && content.Status != nil && content.Status.ReadyToUse != nil && *content.Status.ReadyToUse == true {
119+
// Check if snapshot.Status is different from content.Status and add snapshot to queue
120+
// if there is a difference and it is worth triggering an snapshot status update.
121+
if snapshot != nil && ctrl.needsUpdateSnapshotStatus(snapshot, content) {
122122
klog.V(4).Infof("synchronizing VolumeSnapshotContent for snapshot [%s]: update snapshot status to true if needed.", snapshotName)
123123
// Manually trigger a snapshot status update to happen
124124
// right away so that it is in-sync with the content status
@@ -895,6 +895,39 @@ func (ctrl *csiSnapshotCommonController) bindandUpdateVolumeSnapshot(snapshotCon
895895
return snapshotCopy, nil
896896
}
897897

898+
// needsUpdateSnapshotStatus compares snapshot status with the content status and decide
899+
// if snapshot status needs to be updated based on content status
900+
func (ctrl *csiSnapshotCommonController) needsUpdateSnapshotStatus(snapshot *crdv1.VolumeSnapshot, content *crdv1.VolumeSnapshotContent) bool {
901+
klog.V(5).Infof("needsUpdateSnapshotStatus[%s]", utils.SnapshotKey(snapshot))
902+
903+
if snapshot.Status == nil && content.Status != nil {
904+
return true
905+
}
906+
if snapshot.Status != nil && content.Status == nil {
907+
return false
908+
}
909+
if snapshot.Status == nil && content.Status == nil {
910+
return false
911+
}
912+
if snapshot.Status.BoundVolumeSnapshotContentName == nil {
913+
return true
914+
}
915+
if snapshot.Status.CreationTime == nil && content.Status.CreationTime != nil {
916+
return true
917+
}
918+
if snapshot.Status.ReadyToUse == nil && content.Status.ReadyToUse != nil {
919+
return true
920+
}
921+
if snapshot.Status.ReadyToUse != nil && content.Status.ReadyToUse != nil && snapshot.Status.ReadyToUse != content.Status.ReadyToUse {
922+
return true
923+
}
924+
if (snapshot.Status.RestoreSize == nil && content.Status.RestoreSize != nil) || (snapshot.Status.RestoreSize != nil && snapshot.Status.RestoreSize.IsZero() && content.Status.RestoreSize != nil && *content.Status.RestoreSize > 0) {
925+
return true
926+
}
927+
928+
return false
929+
}
930+
898931
// UpdateSnapshotStatus updates snapshot status based on content status
899932
func (ctrl *csiSnapshotCommonController) updateSnapshotStatus(snapshot *crdv1.VolumeSnapshot, content *crdv1.VolumeSnapshotContent) (*crdv1.VolumeSnapshot, error) {
900933
klog.V(5).Infof("updateSnapshotStatus[%s]", utils.SnapshotKey(snapshot))

0 commit comments

Comments
 (0)