@@ -173,7 +173,7 @@ func (ctrl *csiSnapshotCommonController) syncContent(content *crdv1.VolumeSnapsh
173173// created, updated or periodically synced. We do not differentiate between
174174// these events.
175175// For easier readability, it is split into syncUnreadySnapshot and syncReadySnapshot
176- func (ctrl * csiSnapshotCommonController ) syncSnapshot (snapshot * crdv1.VolumeSnapshot ) error {
176+ func (ctrl * csiSnapshotCommonController ) syncSnapshot (ctx context. Context , snapshot * crdv1.VolumeSnapshot ) error {
177177 klog .V (5 ).Infof ("synchronizing VolumeSnapshot[%s]: %s" , utils .SnapshotKey (snapshot ), utils .GetSnapshotStatusForLogging (snapshot ))
178178
179179 klog .V (5 ).Infof ("syncSnapshot [%s]: check if we should remove finalizer on snapshot PVC source and remove it if we can" , utils .SnapshotKey (snapshot ))
@@ -214,7 +214,7 @@ func (ctrl *csiSnapshotCommonController) syncSnapshot(snapshot *crdv1.VolumeSnap
214214 if ! utils .IsSnapshotReady (snapshot ) || ! utils .IsBoundVolumeSnapshotContentNameSet (snapshot ) {
215215 return ctrl .syncUnreadySnapshot (snapshot )
216216 }
217- return ctrl .syncReadySnapshot (snapshot )
217+ return ctrl .syncReadySnapshot (ctx , snapshot )
218218}
219219
220220// processSnapshotWithDeletionTimestamp processes finalizers and deletes the content when appropriate. It has the following steps:
@@ -395,7 +395,7 @@ func (ctrl *csiSnapshotCommonController) checkandAddSnapshotFinalizers(snapshot
395395
396396// syncReadySnapshot checks the snapshot which has been bound to snapshot content successfully before.
397397// If there is any problem with the binding (e.g., snapshot points to a non-existent snapshot content), update the snapshot status and emit event.
398- func (ctrl * csiSnapshotCommonController ) syncReadySnapshot (snapshot * crdv1.VolumeSnapshot ) error {
398+ func (ctrl * csiSnapshotCommonController ) syncReadySnapshot (ctx context. Context , snapshot * crdv1.VolumeSnapshot ) error {
399399 if ! utils .IsBoundVolumeSnapshotContentNameSet (snapshot ) {
400400 return fmt .Errorf ("snapshot %s is not bound to a content" , utils .SnapshotKey (snapshot ))
401401 }
@@ -415,10 +415,56 @@ func (ctrl *csiSnapshotCommonController) syncReadySnapshot(snapshot *crdv1.Volum
415415 return ctrl .updateSnapshotErrorStatusWithEvent (snapshot , true , v1 .EventTypeWarning , "SnapshotMisbound" , "VolumeSnapshotContent is not bound to the VolumeSnapshot correctly" )
416416 }
417417
418+ // If this snapshot is a member of a volume group snapshot, ensure we have
419+ // the correct ownership. This happens when the user
420+ // statically provisioned volume group snapshot members.
421+ if utils .NeedToAddVolumeGroupSnapshotOwnership (snapshot ) {
422+ if _ , err := ctrl .addVolumeGroupSnapshotOwnership (ctx , snapshot ); err != nil {
423+ return err
424+ }
425+ }
426+
418427 // everything is verified, return
419428 return nil
420429}
421430
431+ // addVolumeGroupSnapshotOwnership adds the ownership information to a statically provisioned VolumeSnapshot
432+ // that is a member of a volume group snapshot
433+ func (ctrl * csiSnapshotCommonController ) addVolumeGroupSnapshotOwnership (ctx context.Context , snapshot * crdv1.VolumeSnapshot ) (* crdv1.VolumeSnapshot , error ) {
434+ klog .V (4 ).Infof ("addVolumeGroupSnapshotOwnership[%s]: adding ownership information" , utils .SnapshotKey (snapshot ))
435+ if snapshot .Status == nil || snapshot .Status .VolumeGroupSnapshotName == nil {
436+ klog .V (4 ).Infof ("addVolumeGroupSnapshotOwnership[%s]: no need to add ownership information, empty volumeGroupSnapshotName" , utils .SnapshotKey (snapshot ))
437+ return nil , nil
438+ }
439+ parentObjectName := * snapshot .Status .VolumeGroupSnapshotName
440+
441+ parentGroup , err := ctrl .groupSnapshotLister .VolumeGroupSnapshots (snapshot .Namespace ).Get (parentObjectName )
442+ if err != nil {
443+ klog .V (4 ).Infof ("addVolumeGroupSnapshotOwnership[%s]: error while looking for parent group %v" , utils .SnapshotKey (snapshot ), err )
444+ return nil , err
445+ }
446+ if parentGroup == nil {
447+ klog .V (4 ).Infof ("addVolumeGroupSnapshotOwnership[%s]: parent group not found %v" , utils .SnapshotKey (snapshot ), err )
448+ return nil , fmt .Errorf ("missing parent group for snapshot %v" , utils .SnapshotKey (snapshot ))
449+ }
450+
451+ updatedSnapshot := snapshot .DeepCopy ()
452+ updatedSnapshot .ObjectMeta .OwnerReferences = append (
453+ snapshot .ObjectMeta .OwnerReferences ,
454+ utils .BuildVolumeGroupSnapshotOwnerReference (parentGroup ),
455+ )
456+
457+ newSnapshot , err := ctrl .clientset .SnapshotV1 ().VolumeSnapshots (snapshot .Namespace ).Update (ctx , updatedSnapshot , metav1.UpdateOptions {})
458+ if err != nil {
459+ klog .V (4 ).Infof ("addVolumeGroupSnapshotOwnership[%s]: error when updating VolumeSnapshot %v" , utils .SnapshotKey (snapshot ), err )
460+ return nil , err
461+ }
462+
463+ klog .V (4 ).Infof ("addVolumeGroupSnapshotOwnership[%s]: updated ownership" , utils .SnapshotKey (snapshot ))
464+
465+ return newSnapshot , nil
466+ }
467+
422468// syncUnreadySnapshot is the main controller method to decide what to do with a snapshot which is not set to ready.
423469func (ctrl * csiSnapshotCommonController ) syncUnreadySnapshot (snapshot * crdv1.VolumeSnapshot ) error {
424470 uniqueSnapshotName := utils .SnapshotKey (snapshot )
@@ -483,7 +529,7 @@ func (ctrl *csiSnapshotCommonController) syncUnreadySnapshot(snapshot *crdv1.Vol
483529 }
484530
485531 // member of a dynamically provisioned volume group snapshot
486- if _ , ok := snapshot . Labels [ utils .VolumeGroupSnapshotNameLabel ]; ok {
532+ if utils .IsVolumeGroupSnapshotMember ( snapshot ) {
487533 if snapshot .Status == nil || snapshot .Status .BoundVolumeSnapshotContentName == nil {
488534 klog .V (5 ).Infof (
489535 "syncUnreadySnapshot [%s]: detected group snapshot member with no content, retrying" ,
@@ -1422,7 +1468,7 @@ func (ctrl *csiSnapshotCommonController) SetDefaultSnapshotClass(snapshot *crdv1
14221468 return nil , snapshot , nil
14231469 }
14241470
1425- if _ , ok := snapshot . Labels [ utils .VolumeGroupSnapshotNameLabel ]; ok {
1471+ if utils .IsVolumeGroupSnapshotMember ( snapshot ) {
14261472 // don't return error for volume group snapshot members
14271473 klog .V (5 ).Infof ("Don't need to find SnapshotClass for volume group snapshot member [%s]" , snapshot .Name )
14281474 return nil , snapshot , nil
0 commit comments