Skip to content

Commit 3edce23

Browse files
committed
formatting and logging changes
1 parent e30de90 commit 3edce23

File tree

5 files changed

+81
-115
lines changed

5 files changed

+81
-115
lines changed

pkg/common-controller/groupsnapshot_controller_helper.go

Lines changed: 34 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -289,14 +289,6 @@ func (ctrl *csiSnapshotCommonController) syncGroupSnapshot(groupSnapshot *crdv1a
289289

290290
klog.V(5).Infof("syncGroupSnapshot [%s]: check if we should remove finalizer on group snapshot PVC source and remove it if we can", utils.GroupSnapshotKey(groupSnapshot))
291291

292-
/*
293-
TODO:
294-
- Check and remove finalizer if needed.
295-
- Check and set invalid group snapshot label, if needed.
296-
- Process if deletion timestamp is set.
297-
- Check and add group snapshot finalizers.
298-
*/
299-
// START
300292
// Proceed with snapshot deletion and remove finalizers when needed
301293
if groupSnapshot.ObjectMeta.DeletionTimestamp != nil {
302294
return ctrl.processGroupSnapshotWithDeletionTimestamp(groupSnapshot)
@@ -311,15 +303,13 @@ func (ctrl *csiSnapshotCommonController) syncGroupSnapshot(groupSnapshot *crdv1a
311303
return err
312304
}
313305

314-
klog.V(5).Infof("syncGroupSnapshot[%s]: check if we should add finalizers on group snapshot", utils.GroupSnapshotKey(groupSnapshot))
306+
klog.V(5).Infof("syncGroupSnapshot: check if we should add finalizers on group snapshot [%s]", utils.GroupSnapshotKey(groupSnapshot))
315307
if err := ctrl.checkandAddGroupSnapshotFinalizers(groupSnapshot); err != nil {
316-
klog.Errorf("error check and add GroupSnapshot finalizers for group snapshot [%s]: %v", groupSnapshot.Name, err)
308+
klog.Errorf("error ccheckandAddGroupSnapshotFinalizers for group snapshot [%s]: %v", utils.GroupSnapshotKey(groupSnapshot), err)
317309
ctrl.eventRecorder.Event(groupSnapshot, v1.EventTypeWarning, "GroupSnapshotFinalizerError", fmt.Sprintf("Failed to check and update group snapshot: %s", err.Error()))
318310
return err
319311
}
320312

321-
// END
322-
323313
// Need to build or update groupSnapshot.Status in following cases:
324314
// 1) groupSnapshot.Status is nil
325315
// 2) groupSnapshot.Status.ReadyToUse is false
@@ -750,7 +740,7 @@ func (ctrl *csiSnapshotCommonController) createGroupSnapshotContent(groupSnapsho
750740
klog.Infof("createSnapshotContent: Creating group snapshot content for group snapshot %s through the plugin ...", utils.GroupSnapshotKey(groupSnapshot))
751741

752742
/*
753-
TODO: Add finalizer to group snapshot
743+
TODO: Add PVC finalizer
754744
*/
755745

756746
groupSnapshotClass, volumes, contentName, err := ctrl.getCreateGroupSnapshotInput(groupSnapshot)
@@ -872,12 +862,8 @@ func (ctrl *csiSnapshotCommonController) syncGroupSnapshotContent(groupSnapshotC
872862
return nil
873863
}
874864

875-
/*
876-
TODO: Add finalizer to prevent deletion
877-
*/
878-
879865
if utils.NeedToAddGroupSnapshotContentFinalizer(groupSnapshotContent) {
880-
// Content is not being deleted -> it should have the finalizer.
866+
// Group Snapshot Content is not being deleted -> it should have the finalizer.
881867
klog.V(5).Infof("syncGroupSnapshotContent [%s]: Add Finalizer for VolumeGroupSnapshotContent", groupSnapshotContent.Name)
882868
return ctrl.addGroupSnapshotContentFinalizer(groupSnapshotContent)
883869
}
@@ -999,7 +985,7 @@ func (ctrl *csiSnapshotCommonController) addGroupSnapshotContentFinalizer(groupS
999985

1000986
// checkandAddSnapshotFinalizers checks and adds snapshot finailzers when needed
1001987
func (ctrl *csiSnapshotCommonController) checkandAddGroupSnapshotFinalizers(groupSnapshot *crdv1alpha1.VolumeGroupSnapshot) error {
1002-
// get the content for this Snapshot
988+
// get the group snapshot content for this group snapshot
1003989
var (
1004990
groupSnapshotContent *crdv1alpha1.VolumeGroupSnapshotContent
1005991
err error
@@ -1013,29 +999,21 @@ func (ctrl *csiSnapshotCommonController) checkandAddGroupSnapshotFinalizers(grou
1013999
return err
10141000
}
10151001

1016-
/*
1017-
TODO: Do we need to add group snapshot as source finalizer?
1018-
*/
1019-
1020-
// note that content could be nil, in this case bound finalizer is not needed
1021-
addBoundFinalizer := false
1022-
if groupSnapshotContent != nil {
1023-
// A bound finalizer is needed ONLY when all following conditions are satisfied:
1024-
// 1. the VolumeSnapshot is bound to a content
1025-
// 2. the VolumeSnapshot does not have deletion timestamp set
1026-
// 3. the matching content has a deletion policy to be Delete
1027-
// Note that if a matching content is found, it must points back to the snapshot
1028-
addBoundFinalizer = utils.NeedToAddGroupSnapshotBoundFinalizer(groupSnapshot) && (groupSnapshotContent.Spec.DeletionPolicy == crdv1.VolumeSnapshotContentDelete)
1029-
}
1030-
if addBoundFinalizer {
1002+
// A bound finalizer is needed ONLY when all following conditions are satisfied:
1003+
// 1. the VolumeGroupSnapshot is bound to a VolumeGroupSnapshotContent
1004+
// 2. the VolumeGroupSnapshot does not have deletion timestamp set
1005+
// 3. the matching VolumeGroupSnapshotContent has a deletion policy to be Delete
1006+
// Note that if a matching VolumeGroupSnapshotContent is found, it must point back to the VolumeGroupSnapshot
1007+
if groupSnapshotContent != nil && utils.NeedToAddGroupSnapshotBoundFinalizer(groupSnapshot) && (groupSnapshotContent.Spec.DeletionPolicy == crdv1.VolumeSnapshotContentDelete) {
10311008
// Snapshot is not being deleted -> it should have the finalizer.
10321009
klog.V(5).Infof("checkandAddGroupSnapshotFinalizers: Add Finalizer for VolumeGroupSnapshot[%s]", utils.GroupSnapshotKey(groupSnapshot))
1033-
return ctrl.addGroupSnapshotFinalizer(groupSnapshot, addBoundFinalizer)
1010+
return ctrl.addGroupSnapshotFinalizer(groupSnapshot, true)
1011+
10341012
}
10351013
return nil
10361014
}
10371015

1038-
// addGroupSnapshotFinalizer adds a Finalizer for VolumeGroupSnapshot.
1016+
// addGroupSnapshotFinalizer adds a Finalizer to a VolumeGroupSnapshot.
10391017
func (ctrl *csiSnapshotCommonController) addGroupSnapshotFinalizer(groupSnapshot *crdv1alpha1.VolumeGroupSnapshot, addBoundFinalizer bool) error {
10401018
var updatedGroupSnapshot *crdv1alpha1.VolumeGroupSnapshot
10411019
var err error
@@ -1080,7 +1058,7 @@ func (ctrl *csiSnapshotCommonController) addGroupSnapshotFinalizer(groupSnapshot
10801058

10811059
// processGroupSnapshotWithDeletionTimestamp processes finalizers and deletes the
10821060
// group snapshot content when appropriate. It has the following steps:
1083-
// 1. Get the group snapshot content which the to-be-deleted VolumeGroupSnapshot
1061+
// 1. Get the VolumeGroupSnapshotContent which the to-be-deleted VolumeGroupSnapshot
10841062
// points to and verifies bi-directional binding.
10851063
// 2. Call checkandRemoveGroupSnapshotFinalizersAndCheckandDeleteGroupSnapshotContent()
10861064
// with information obtained from step 1. This function name is very long but the
@@ -1101,7 +1079,7 @@ func (ctrl *csiSnapshotCommonController) processGroupSnapshotWithDeletionTimesta
11011079
if groupSnapshotContentName == "" && &groupSnapshot.Spec.Source.VolumeGroupSnapshotContentName == nil {
11021080
groupSnapshotContentName = utils.GetDynamicSnapshotContentNameForGroupSnapshot(groupSnapshot)
11031081
}
1104-
// find a group snapshot content from cache store, note that it's complete legit
1082+
// find a group snapshot content from cache store, note that it's completely legit
11051083
// that no group snapshot content has been found from group snapshot content
11061084
// cache store
11071085
groupSnapshotContent, err := ctrl.getGroupSnapshotContentFromStore(groupSnapshotContentName)
@@ -1139,18 +1117,24 @@ func (ctrl *csiSnapshotCommonController) checkandRemoveGroupSnapshotFinalizersAn
11391117
return nil
11401118
}
11411119

1142-
/* NEEDED?
1143-
// check if the group snapshot is being used for restore a PVC, if yes, do nothing
1144-
// and wait until PVC restoration finishes
1145-
if content != nil && ctrl.isVolumeBeingCreatedFromSnapshot(snapshot) {
1146-
klog.V(4).Infof("checkandRemoveSnapshotFinalizersAndCheckandDeleteContent[%s]: snapshot is being used to restore a PVC", utils.SnapshotKey(snapshot))
1147-
ctrl.eventRecorder.Event(snapshot, v1.EventTypeWarning, "SnapshotDeletePending", "Snapshot is being used to restore a PVC")
1148-
// TODO(@xiangqian): should requeue this?
1149-
return nil
1150-
}
1120+
// TODO: In this needed?
1121+
// check if an individual snapshot belonging to the group snapshot is being
1122+
// used for restore a PVC
1123+
// If yes, do nothing and wait until PVC restoration finishes
1124+
for _, snapshotRef := range groupSnapshot.Status.VolumeSnapshotRefList {
1125+
snapshot, err := ctrl.snapshotLister.VolumeSnapshots(snapshotRef.Namespace).Get(snapshotRef.Name)
1126+
if err != nil {
1127+
return err
1128+
}
1129+
if ctrl.isVolumeBeingCreatedFromSnapshot(snapshot) {
1130+
klog.V(4).Infof("checkandRemoveGroupSnapshotFinalizersAndCheckandDeleteGroupSnapshotContent[%s]: snapshot [%s] belonging to volume group snapshot is being used to restore a PVC", utils.GroupSnapshotKey(groupSnapshot), utils.SnapshotKey(snapshot))
1131+
ctrl.eventRecorder.Event(snapshot, v1.EventTypeWarning, "SnapshotDeletePending", "Snapshot is being used to restore a PVC")
1132+
// TODO(@xiangqian): should requeue this?
1133+
return nil
1134+
}
11511135

1136+
}
11521137

1153-
*/
11541138
// regardless of the deletion policy, set the VolumeSnapshotBeingDeleted on
11551139
// content object, this is to allow snapshotter sidecar controller to conduct
11561140
// a delete operation whenever the content has deletion timestamp set.
@@ -1167,7 +1151,7 @@ func (ctrl *csiSnapshotCommonController) checkandRemoveGroupSnapshotFinalizersAn
11671151
// VolumeGroupSnapshot should be deleted. Check and remove finalizers
11681152
// If group snapshot content exists and has a deletion policy of Delete, set
11691153
// DeletionTimeStamp on the content;
1170-
// group snapshot content won't be deleted immediately due to the VolumeGroupSnapshotContentFinalizer
1154+
// VolumeGroupSnapshotContent won't be deleted immediately due to the VolumeGroupSnapshotContentFinalizer
11711155
if groupSnapshotContent != nil && deleteGroupSnapshotContent {
11721156
klog.V(5).Infof("checkandRemoveGroupSnapshotFinalizersAndCheckandDeleteGroupSnapshotContent: set DeletionTimeStamp on group snapshot content [%s].", groupSnapshotContent.Name)
11731157
err := ctrl.clientset.GroupsnapshotV1alpha1().VolumeGroupSnapshotContents().Delete(context.TODO(), groupSnapshotContent.Name, metav1.DeleteOptions{})
@@ -1228,26 +1212,9 @@ func (ctrl *csiSnapshotCommonController) removeGroupSnapshotFinalizer(groupSnaps
12281212
if !removeBoundFinalizer {
12291213
return nil
12301214
}
1231-
/* NEEDED?
1232-
// NOTE(xyang): We have to make sure PVC finalizer is deleted before
1233-
// the VolumeSnapshot API object is deleted. Once the VolumeSnapshot
1234-
// API object is deleted, there won't be any VolumeSnapshot update
1235-
// event that can trigger the PVC finalizer removal any more.
1236-
// We also can't remove PVC finalizer too early. PVC finalizer should
1237-
// not be removed if a VolumeSnapshot API object is still using it.
1238-
// If we are here, it means we are going to remove finalizers from the
1239-
// VolumeSnapshot API object so that the VolumeSnapshot API object can
1240-
// be deleted. This means we no longer need to keep the PVC finalizer
1241-
// for this particular snapshot.
1242-
if err := ctrl.checkandRemovePVCFinalizer(groupSnapshot, true); err != nil {
1243-
klog.Errorf("removeSnapshotFinalizer: error check and remove PVC finalizer for snapshot [%s]: %v", groupSnapshot.Name, err)
1244-
// Log an event and keep the original error from checkandRemovePVCFinalizer
1245-
ctrl.eventRecorder.Event(groupSnapshot, v1.EventTypeWarning, "ErrorPVCFinalizer", "Error check and remove PVC Finalizer for VolumeSnapshot")
1246-
return newControllerUpdateError(groupSnapshot.Name, err.Error())
1247-
}
12481215

1216+
// TODO: Remove PVC Finalizer
12491217

1250-
*/
12511218
groupSnapshotClone := groupSnapshot.DeepCopy()
12521219
groupSnapshotClone.ObjectMeta.Finalizers = utils.RemoveString(groupSnapshotClone.ObjectMeta.Finalizers, utils.VolumeGroupSnapshotBoundFinalizer)
12531220
newGroupSnapshot, err := ctrl.clientset.GroupsnapshotV1alpha1().VolumeGroupSnapshots(groupSnapshotClone.Namespace).Update(context.TODO(), groupSnapshotClone, metav1.UpdateOptions{})

pkg/common-controller/snapshot_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,7 @@ func (ctrl *csiSnapshotCommonController) isPVCBeingUsed(pvc *v1.PersistentVolume
971971
return false
972972
}
973973

974-
// PVCFinalizer checks if the snapshot source finalizer should be removed
974+
// checkandRemovePVCFinalizer checks if the snapshot source finalizer should be removed
975975
// and removed it if needed. If skipCurrentSnapshot is true, skip checking if the current
976976
// snapshot is using the PVC as source.
977977
func (ctrl *csiSnapshotCommonController) checkandRemovePVCFinalizer(snapshot *crdv1.VolumeSnapshot, skipCurrentSnapshot bool) error {

0 commit comments

Comments
 (0)