@@ -504,6 +504,12 @@ func (ctrl *csiSnapshotCommonController) createSnapshotsForGroupSnapshotContent(
504504 return groupSnapshotContent , nil
505505 }
506506
507+ // No volume group snapshot handle is present.
508+ // Let's wait for the snapshotter sidecar to fill it.
509+ if groupSnapshotContent .Status .VolumeGroupSnapshotHandle == nil {
510+ return groupSnapshotContent , nil
511+ }
512+
507513 // The contents of the volume group snapshot class are needed to set the
508514 // metadata containing the secrets to recover the snapshots
509515 if groupSnapshot .Spec .VolumeGroupSnapshotClassName == nil {
@@ -557,6 +563,9 @@ func (ctrl *csiSnapshotCommonController) createSnapshotsForGroupSnapshotContent(
557563 volumeSnapshotContent := & crdv1.VolumeSnapshotContent {
558564 ObjectMeta : metav1.ObjectMeta {
559565 Name : volumeSnapshotContentName ,
566+ Labels : map [string ]string {
567+ utils .VolumeGroupSnapshotHandleLabel : * groupSnapshotContent .Status .VolumeGroupSnapshotHandle ,
568+ },
560569 },
561570 Spec : crdv1.VolumeSnapshotContentSpec {
562571 VolumeSnapshotRef : v1.ObjectReference {
@@ -567,7 +576,7 @@ func (ctrl *csiSnapshotCommonController) createSnapshotsForGroupSnapshotContent(
567576 DeletionPolicy : groupSnapshotContent .Spec .DeletionPolicy ,
568577 Driver : groupSnapshotContent .Spec .Driver ,
569578 Source : crdv1.VolumeSnapshotContentSource {
570- SnapshotHandle : & snapshotHandle ,
579+ VolumeHandle : & volumeHandle ,
571580 },
572581 },
573582 // The status will be set by VolumeSnapshotContent reconciler
@@ -586,24 +595,28 @@ func (ctrl *csiSnapshotCommonController) createSnapshotsForGroupSnapshotContent(
586595 metav1 .SetMetaDataAnnotation (& volumeSnapshotContent .ObjectMeta , utils .AnnDeletionSecretRefNamespace , groupSnapshotSecret .Namespace )
587596 }
588597
589- label := make (map [string ]string )
590- label [utils .VolumeGroupSnapshotNameLabel ] = groupSnapshotContent .Spec .VolumeGroupSnapshotRef .Name
591598 volumeSnapshot := & crdv1.VolumeSnapshot {
592599 ObjectMeta : metav1.ObjectMeta {
593- Name : volumeSnapshotName ,
594- Namespace : volumeSnapshotNamespace ,
595- Labels : label ,
596- Finalizers : []string {utils .VolumeSnapshotInGroupFinalizer },
597- },
598- Spec : crdv1.VolumeSnapshotSpec {
599- Source : crdv1.VolumeSnapshotSource {
600- PersistentVolumeClaimName : & pv .Spec .ClaimRef .Name ,
600+ Name : volumeSnapshotName ,
601+ Namespace : volumeSnapshotNamespace ,
602+ Labels : map [string ]string {
603+ utils .VolumeGroupSnapshotNameLabel : groupSnapshotContent .Spec .VolumeGroupSnapshotRef .Name ,
601604 },
605+ Finalizers : []string {utils .VolumeSnapshotInGroupFinalizer },
602606 },
607+ // The spec stanza is set immediately
603608 // The status will be set by VolumeSnapshot reconciler
604609 }
605610
606- _ , err = ctrl .clientset .SnapshotV1 ().VolumeSnapshotContents ().Create (ctx , volumeSnapshotContent , metav1.CreateOptions {})
611+ if pv != nil {
612+ volumeSnapshot .Spec .Source .PersistentVolumeClaimName = & pv .Spec .ClaimRef .Name
613+ } else {
614+ // If no persistent volume was found, set the PVC name to empty
615+ var emptyString string
616+ volumeSnapshot .Spec .Source .PersistentVolumeClaimName = & emptyString
617+ }
618+
619+ createdVolumeSnapshotContent , err := ctrl .clientset .SnapshotV1 ().VolumeSnapshotContents ().Create (ctx , volumeSnapshotContent , metav1.CreateOptions {})
607620 if err != nil && ! apierrs .IsAlreadyExists (err ) {
608621 return groupSnapshotContent , fmt .Errorf (
609622 "createSnapshotsForGroupSnapshotContent: creating volumesnapshotcontent %w" , err )
@@ -648,6 +661,32 @@ func (ctrl *csiSnapshotCommonController) createSnapshotsForGroupSnapshotContent(
648661 return groupSnapshotContent , fmt .Errorf (
649662 "createSnapshotsForGroupSnapshotContent: binding volumesnapshot to volumesnapshotcontent %w" , err )
650663 }
664+
665+ // set the snapshot handle and the group snapshot handle
666+ // inside the volume snapshot content to allow
667+ // the CSI Snapshotter sidecar to reconcile its status
668+ _ , err = utils .PatchVolumeSnapshotContent (createdVolumeSnapshotContent , []utils.PatchOp {
669+ {
670+ Op : "replace" ,
671+ Path : "/status" ,
672+ Value : & crdv1.VolumeSnapshotContentStatus {},
673+ },
674+ {
675+ Op : "replace" ,
676+ Path : "/status/snapshotHandle" ,
677+ Value : snapshotHandle ,
678+ },
679+ {
680+ Op : "replace" ,
681+ Path : "/status/volumeGroupSnapshotHandle" ,
682+ Value : groupSnapshotContent .Status .VolumeGroupSnapshotHandle ,
683+ },
684+ }, ctrl .clientset , "status" )
685+ if err != nil {
686+ return groupSnapshotContent , fmt .Errorf (
687+ "createSnapshotsForGroupSnapshotContent: setting snapshotHandle in volumesnapshotcontent %w" , err )
688+ }
689+
651690 }
652691
653692 return groupSnapshotContent , nil
0 commit comments