@@ -35,6 +35,13 @@ import (
3535 "github.com/kubernetes-csi/external-snapshotter/v7/pkg/utils"
3636)
3737
38+ // snapshotContentNameVolumeHandlePair represent the link between a VolumeSnapshotContent and
39+ // the handle of the volume that was snapshotted
40+ type snapshotContentNameVolumeHandlePair struct {
41+ snapshotContentName string
42+ volumeHandle string
43+ }
44+
3845func (ctrl * csiSnapshotSideCarController ) storeGroupSnapshotContentUpdate (groupSnapshotContent interface {}) (bool , error ) {
3946 return utils .StoreObjectUpdate (ctrl .groupSnapshotContentStore , groupSnapshotContent , "groupsnapshotcontent" )
4047}
@@ -430,7 +437,7 @@ func (ctrl *csiSnapshotSideCarController) createGroupSnapshotWrapper(groupSnapsh
430437 return groupSnapshotContent , fmt .Errorf ("failed to get secret reference for group snapshot content %s: %v" , groupSnapshotContent .Name , err )
431438 }
432439 // Create individual snapshots and snapshot contents
433- var snapshotContentNames []string
440+ var snapshotContentLinks []snapshotContentNameVolumeHandlePair
434441 for _ , snapshot := range snapshots {
435442 volumeSnapshotContentName := GetSnapshotContentNameForVolumeGroupSnapshotContent (string (groupSnapshotContent .UID ), snapshot .SourceVolumeId )
436443 volumeSnapshotName := GetSnapshotNameForVolumeGroupSnapshotContent (string (groupSnapshotContent .UID ), snapshot .SourceVolumeId )
@@ -484,7 +491,10 @@ func (ctrl *csiSnapshotSideCarController) createGroupSnapshotWrapper(groupSnapsh
484491 if err != nil {
485492 return groupSnapshotContent , err
486493 }
487- snapshotContentNames = append (snapshotContentNames , vsc .Name )
494+ snapshotContentLinks = append (snapshotContentLinks , snapshotContentNameVolumeHandlePair {
495+ snapshotContentName : vsc .Name ,
496+ volumeHandle : snapshot .SourceVolumeId ,
497+ })
488498
489499 _ , err = ctrl .clientset .SnapshotV1 ().VolumeSnapshots (volumeSnapshotNamespace ).Create (context .TODO (), volumeSnapshot , metav1.CreateOptions {})
490500 if err != nil {
@@ -497,7 +507,7 @@ func (ctrl *csiSnapshotSideCarController) createGroupSnapshotWrapper(groupSnapsh
497507 }
498508 }
499509
500- newGroupSnapshotContent , err := ctrl .updateGroupSnapshotContentStatus (groupSnapshotContent , groupSnapshotID , readyToUse , creationTime .UnixNano (), snapshotContentNames )
510+ newGroupSnapshotContent , err := ctrl .updateGroupSnapshotContentStatus (groupSnapshotContent , groupSnapshotID , readyToUse , creationTime .UnixNano (), snapshotContentLinks )
501511 if err != nil {
502512 klog .Errorf ("error updating status for volume group snapshot content %s: %v." , groupSnapshotContent .Name , err )
503513 return groupSnapshotContent , fmt .Errorf ("error updating status for volume group snapshot content %s: %v" , groupSnapshotContent .Name , err )
@@ -633,14 +643,20 @@ func (ctrl *csiSnapshotSideCarController) updateGroupSnapshotContentStatus(
633643 groupSnapshotHandle string ,
634644 readyToUse bool ,
635645 createdAt int64 ,
636- snapshotContentNames []string ) (* crdv1alpha1.VolumeGroupSnapshotContent , error ) {
646+ snapshotContentLinks []snapshotContentNameVolumeHandlePair ,
647+ ) (* crdv1alpha1.VolumeGroupSnapshotContent , error ) {
637648 klog .V (5 ).Infof ("updateGroupSnapshotContentStatus: updating VolumeGroupSnapshotContent [%s], groupSnapshotHandle %s, readyToUse %v, createdAt %v" , groupSnapshotContent .Name , groupSnapshotHandle , readyToUse , createdAt )
638649
639650 groupSnapshotContentObj , err := ctrl .clientset .GroupsnapshotV1alpha1 ().VolumeGroupSnapshotContents ().Get (context .TODO (), groupSnapshotContent .Name , metav1.GetOptions {})
640651 if err != nil {
641652 return nil , fmt .Errorf ("error get group snapshot content %s from api server: %v" , groupSnapshotContent .Name , err )
642653 }
643654
655+ pvs , err := ctrl .client .CoreV1 ().PersistentVolumes ().List (context .TODO (), metav1.ListOptions {})
656+ if err != nil {
657+ return nil , fmt .Errorf ("error get PersistentVolumes list from API server: %v" , err )
658+ }
659+
644660 var newStatus * crdv1alpha1.VolumeGroupSnapshotContentStatus
645661 updated := false
646662 if groupSnapshotContentObj .Status == nil {
@@ -649,10 +665,24 @@ func (ctrl *csiSnapshotSideCarController) updateGroupSnapshotContentStatus(
649665 ReadyToUse : & readyToUse ,
650666 CreationTime : & createdAt ,
651667 }
652- for _ , name := range snapshotContentNames {
668+ for _ , snapshotContentLink := range snapshotContentLinks {
669+ pv := utils .GetPersistentVolumeFromHandle (pvs , groupSnapshotContent .Spec .Driver , snapshotContentLink .volumeHandle )
670+ pvName := ""
671+ if pv != nil {
672+ pvName = pv .Name
673+ } else {
674+ klog .Errorf (
675+ "updateGroupSnapshotContentStatus: unable to find PV for volumeHandle:[%s] and CSI driver:[%s]" ,
676+ snapshotContentLink .volumeHandle ,
677+ groupSnapshotContent .Spec .Driver )
678+ }
679+
653680 newStatus .PVVolumeSnapshotContentList = append (newStatus .PVVolumeSnapshotContentList , crdv1alpha1.PVVolumeSnapshotContentPair {
654681 VolumeSnapshotContentRef : v1.LocalObjectReference {
655- Name : name ,
682+ Name : snapshotContentLink .snapshotContentName ,
683+ },
684+ PersistentVolumeRef : v1.LocalObjectReference {
685+ Name : pvName ,
656686 },
657687 })
658688 }
@@ -675,10 +705,24 @@ func (ctrl *csiSnapshotSideCarController) updateGroupSnapshotContentStatus(
675705 updated = true
676706 }
677707 if len (newStatus .PVVolumeSnapshotContentList ) == 0 {
678- for _ , name := range snapshotContentNames {
708+ for _ , snapshotContentLink := range snapshotContentLinks {
709+ pv := utils .GetPersistentVolumeFromHandle (pvs , groupSnapshotContent .Spec .Driver , snapshotContentLink .volumeHandle )
710+ pvName := ""
711+ if pv != nil {
712+ pvName = pv .Name
713+ } else {
714+ klog .Errorf (
715+ "updateGroupSnapshotContentStatus: unable to find PV for volumeHandle:[%s] and CSI driver:[%s] (existing status)" ,
716+ snapshotContentLink .volumeHandle ,
717+ groupSnapshotContent .Spec .Driver )
718+ }
719+
679720 newStatus .PVVolumeSnapshotContentList = append (newStatus .PVVolumeSnapshotContentList , crdv1alpha1.PVVolumeSnapshotContentPair {
680721 VolumeSnapshotContentRef : v1.LocalObjectReference {
681- Name : name ,
722+ Name : snapshotContentLink .snapshotContentName ,
723+ },
724+ PersistentVolumeRef : v1.LocalObjectReference {
725+ Name : pvName ,
682726 },
683727 })
684728 }
@@ -842,7 +886,7 @@ func (ctrl *csiSnapshotSideCarController) checkandUpdateGroupSnapshotContentStat
842886 }
843887
844888 // TODO: Get a reference to snapshot contents for this volume group snapshot
845- updatedContent , err := ctrl .updateGroupSnapshotContentStatus (groupSnapshotContent , groupSnapshotID , readyToUse , creationTime .UnixNano (), []string {})
889+ updatedContent , err := ctrl .updateGroupSnapshotContentStatus (groupSnapshotContent , groupSnapshotID , readyToUse , creationTime .UnixNano (), []snapshotContentNameVolumeHandlePair {})
846890 if err != nil {
847891 return groupSnapshotContent , err
848892 }
0 commit comments