@@ -26,6 +26,7 @@ import (
2626 "github.com/pkg/errors"
2727 v1 "k8s.io/api/core/v1"
2828 k8serrors "k8s.io/apimachinery/pkg/api/errors"
29+
2930 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3031 "k8s.io/apimachinery/pkg/util/wait"
3132)
@@ -128,12 +129,22 @@ func (p *Plugin) createPVC(volumeID, snapName string) (*Volume, error) {
128129 return newVol , nil
129130 }
130131
131- p .Log .Infof ("Creating PVC for volumeID:%s snapshot:%s in namespace=%s" , volumeID , snapName , targetedNs )
132+ // restore PV so that it can be bound to PVC
133+ p .Log .Infof ("Creating PV(%s) snapshot:%s" , volumeID , snapName )
134+ if err := p .restorePV (volumeID , snapName ); err != nil {
135+ return nil , errors .Wrapf (err , "failed to restore PV=%s" , pvc .Spec .VolumeName )
136+ }
137+
138+ // pending until the pv is bound to pvc
139+ p .Log .Infof ("Creating PVC with specified volumeID:%s, snapshot:%s in namespace=%s" , volumeID , snapName , targetedNs )
132140
133141 pvc .Annotations = make (map [string ]string )
134142 // Add annotation PVCreatedByKey, with value 'restore' to PVC
135143 // So that Maya-APIServer skip updating target IPAddress in CVR
136144 pvc .Annotations [v1alpha1 .PVCreatedByKey ] = "restore"
145+
146+ // Specify src pvname
147+ pvc .Spec .VolumeName = volumeID
137148 rpvc , err := p .K8sClient .
138149 CoreV1 ().
139150 PersistentVolumeClaims (pvc .Namespace ).
@@ -142,6 +153,10 @@ func (p *Plugin) createPVC(volumeID, snapName string) (*Volume, error) {
142153 return nil , errors .Wrapf (err , "failed to create PVC=%s/%s" , pvc .Namespace , pvc .Name )
143154 }
144155
156+ p .Log .Infof ("Creating CVC(%s) snapshot:%s" , volumeID , snapName )
157+ if err := p .restoreCVC (volumeID , rpvc .Name , rpvc .Namespace , snapName ); err != nil {
158+ return nil , errors .Wrapf (err , "failed to restore CVC=%s" , volumeID )
159+ }
145160 for cnt := 0 ; cnt < PVCWaitCount ; cnt ++ {
146161 pvc , err = p .K8sClient .
147162 CoreV1 ().
@@ -157,7 +172,7 @@ func (p *Plugin) createPVC(volumeID, snapName string) (*Volume, error) {
157172 return nil , errors .Wrapf (err , "failed to create PVC=%s/%s" , rpvc .Namespace , rpvc .Name )
158173 }
159174 if pvc .Status .Phase == v1 .ClaimBound {
160- p .Log .Infof ("PVC(%v) created.. " , pvc .Name )
175+ p .Log .Infof ("PVC(%v) created, PV(%s) bound " , pvc .Name , volumeID )
161176 vol = & Volume {
162177 volname : pvc .Spec .VolumeName ,
163178 snapshotTag : volumeID ,
@@ -168,6 +183,7 @@ func (p *Plugin) createPVC(volumeID, snapName string) (*Volume, error) {
168183 p .volumes [vol .volname ] = vol
169184 break
170185 }
186+ p .Log .Debugf ("PV(%v) is not bound yet.." , volumeID )
171187 time .Sleep (PVCCheckInterval )
172188 }
173189
@@ -182,12 +198,10 @@ func (p *Plugin) createPVC(volumeID, snapName string) (*Volume, error) {
182198 p .Log .Errorf ("Failed to get PV{%s}" , vol .volname )
183199 return nil , errors .Wrapf (err , "failed to get pv=%s" , vol .volname )
184200 }
185-
186201 vol .isCSIVolume = isCSIPv (* pv )
187202 if err = p .waitForAllCVRs (vol ); err != nil {
188203 return nil , err
189204 }
190-
191205 // CVRs are created and updated, now we can remove the annotation 'PVCreatedByKey' from PVC
192206 if err = p .removePVCAnnotationKey (pvc , v1alpha1 .PVCreatedByKey ); err != nil {
193207 p .Log .Warningf ("Failed to remove restore annotation from PVC=%s/%s err=%s" , pvc .Namespace , pvc .Name , err )
@@ -207,6 +221,7 @@ func (p *Plugin) getVolumeFromPVC(pvc v1.PersistentVolumeClaim) (*Volume, error)
207221 Get (context .TODO (), pvc .Name , metav1.GetOptions {})
208222 if err != nil {
209223 if k8serrors .IsNotFound (err ) {
224+ p .Log .Warnf ("PVC{%s} not found" , pvc .Name )
210225 return nil , nil
211226 }
212227 return nil , errors .Wrapf (err , "failed to fetch PVC{%s}" , pvc .Name )
0 commit comments