@@ -35,6 +35,13 @@ import (
35
35
"github.com/kubernetes-csi/external-snapshotter/v7/pkg/utils"
36
36
)
37
37
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
+
38
45
func (ctrl * csiSnapshotSideCarController ) storeGroupSnapshotContentUpdate (groupSnapshotContent interface {}) (bool , error ) {
39
46
return utils .StoreObjectUpdate (ctrl .groupSnapshotContentStore , groupSnapshotContent , "groupsnapshotcontent" )
40
47
}
@@ -430,7 +437,7 @@ func (ctrl *csiSnapshotSideCarController) createGroupSnapshotWrapper(groupSnapsh
430
437
return groupSnapshotContent , fmt .Errorf ("failed to get secret reference for group snapshot content %s: %v" , groupSnapshotContent .Name , err )
431
438
}
432
439
// Create individual snapshots and snapshot contents
433
- var snapshotContentNames []string
440
+ var snapshotContentLinks []snapshotContentNameVolumeHandlePair
434
441
for _ , snapshot := range snapshots {
435
442
volumeSnapshotContentName := GetSnapshotContentNameForVolumeGroupSnapshotContent (string (groupSnapshotContent .UID ), snapshot .SourceVolumeId )
436
443
volumeSnapshotName := GetSnapshotNameForVolumeGroupSnapshotContent (string (groupSnapshotContent .UID ), snapshot .SourceVolumeId )
@@ -484,7 +491,10 @@ func (ctrl *csiSnapshotSideCarController) createGroupSnapshotWrapper(groupSnapsh
484
491
if err != nil {
485
492
return groupSnapshotContent , err
486
493
}
487
- snapshotContentNames = append (snapshotContentNames , vsc .Name )
494
+ snapshotContentLinks = append (snapshotContentLinks , snapshotContentNameVolumeHandlePair {
495
+ snapshotContentName : vsc .Name ,
496
+ volumeHandle : snapshot .SourceVolumeId ,
497
+ })
488
498
489
499
_ , err = ctrl .clientset .SnapshotV1 ().VolumeSnapshots (volumeSnapshotNamespace ).Create (context .TODO (), volumeSnapshot , metav1.CreateOptions {})
490
500
if err != nil {
@@ -497,7 +507,7 @@ func (ctrl *csiSnapshotSideCarController) createGroupSnapshotWrapper(groupSnapsh
497
507
}
498
508
}
499
509
500
- newGroupSnapshotContent , err := ctrl .updateGroupSnapshotContentStatus (groupSnapshotContent , groupSnapshotID , readyToUse , creationTime .UnixNano (), snapshotContentNames )
510
+ newGroupSnapshotContent , err := ctrl .updateGroupSnapshotContentStatus (groupSnapshotContent , groupSnapshotID , readyToUse , creationTime .UnixNano (), snapshotContentLinks )
501
511
if err != nil {
502
512
klog .Errorf ("error updating status for volume group snapshot content %s: %v." , groupSnapshotContent .Name , err )
503
513
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(
633
643
groupSnapshotHandle string ,
634
644
readyToUse bool ,
635
645
createdAt int64 ,
636
- snapshotContentNames []string ) (* crdv1alpha1.VolumeGroupSnapshotContent , error ) {
646
+ snapshotContentLinks []snapshotContentNameVolumeHandlePair ,
647
+ ) (* crdv1alpha1.VolumeGroupSnapshotContent , error ) {
637
648
klog .V (5 ).Infof ("updateGroupSnapshotContentStatus: updating VolumeGroupSnapshotContent [%s], groupSnapshotHandle %s, readyToUse %v, createdAt %v" , groupSnapshotContent .Name , groupSnapshotHandle , readyToUse , createdAt )
638
649
639
650
groupSnapshotContentObj , err := ctrl .clientset .GroupsnapshotV1alpha1 ().VolumeGroupSnapshotContents ().Get (context .TODO (), groupSnapshotContent .Name , metav1.GetOptions {})
640
651
if err != nil {
641
652
return nil , fmt .Errorf ("error get group snapshot content %s from api server: %v" , groupSnapshotContent .Name , err )
642
653
}
643
654
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
+
644
660
var newStatus * crdv1alpha1.VolumeGroupSnapshotContentStatus
645
661
updated := false
646
662
if groupSnapshotContentObj .Status == nil {
@@ -649,10 +665,24 @@ func (ctrl *csiSnapshotSideCarController) updateGroupSnapshotContentStatus(
649
665
ReadyToUse : & readyToUse ,
650
666
CreationTime : & createdAt ,
651
667
}
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
+
653
680
newStatus .PVVolumeSnapshotContentList = append (newStatus .PVVolumeSnapshotContentList , crdv1alpha1.PVVolumeSnapshotContentPair {
654
681
VolumeSnapshotContentRef : v1.LocalObjectReference {
655
- Name : name ,
682
+ Name : snapshotContentLink .snapshotContentName ,
683
+ },
684
+ PersistentVolumeRef : v1.LocalObjectReference {
685
+ Name : pvName ,
656
686
},
657
687
})
658
688
}
@@ -675,10 +705,24 @@ func (ctrl *csiSnapshotSideCarController) updateGroupSnapshotContentStatus(
675
705
updated = true
676
706
}
677
707
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
+
679
720
newStatus .PVVolumeSnapshotContentList = append (newStatus .PVVolumeSnapshotContentList , crdv1alpha1.PVVolumeSnapshotContentPair {
680
721
VolumeSnapshotContentRef : v1.LocalObjectReference {
681
- Name : name ,
722
+ Name : snapshotContentLink .snapshotContentName ,
723
+ },
724
+ PersistentVolumeRef : v1.LocalObjectReference {
725
+ Name : pvName ,
682
726
},
683
727
})
684
728
}
@@ -842,7 +886,7 @@ func (ctrl *csiSnapshotSideCarController) checkandUpdateGroupSnapshotContentStat
842
886
}
843
887
844
888
// 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 {})
846
890
if err != nil {
847
891
return groupSnapshotContent , err
848
892
}
0 commit comments