@@ -145,7 +145,15 @@ func (gceCS *GCEControllerServer) CreateVolume(ctx context.Context, req *csi.Cre
145145 return nil , status .Error (codes .AlreadyExists , fmt .Sprintf ("CreateVolume disk already exists with same name and is incompatible: %v" , err ))
146146 }
147147 // If there is no validation error, immediately return success
148- return generateCreateVolumeResponse (existingDisk .GetSelfLink (), capBytes , zones ), nil
148+ return generateCreateVolumeResponse (existingDisk , capBytes , zones ), nil
149+ }
150+
151+ snapshotId := ""
152+ content := req .GetVolumeContentSource ()
153+ if content != nil {
154+ if content .GetSnapshot () != nil {
155+ snapshotId = content .GetSnapshot ().GetId ()
156+ }
149157 }
150158
151159 // Create the disk
@@ -155,23 +163,22 @@ func (gceCS *GCEControllerServer) CreateVolume(ctx context.Context, req *csi.Cre
155163 if len (zones ) != 1 {
156164 return nil , status .Errorf (codes .Internal , fmt .Sprintf ("CreateVolume failed to get a single zone for creating zonal disk, instead got: %v" , zones ))
157165 }
158- disk , err = createSingleZoneDisk (ctx , gceCS .CloudProvider , name , zones , diskType , capacityRange , capBytes )
166+ disk , err = createSingleZoneDisk (ctx , gceCS .CloudProvider , name , zones , diskType , capacityRange , capBytes , snapshotId )
159167 if err != nil {
160168 return nil , status .Error (codes .Internal , fmt .Sprintf ("CreateVolume failed to create single zonal disk %#v: %v" , name , err ))
161169 }
162170 case replicationTypeRegionalPD :
163171 if len (zones ) != 2 {
164172 return nil , status .Errorf (codes .Internal , fmt .Sprintf ("CreateVolume failed to get a 2 zones for creating regional disk, instead got: %v" , zones ))
165173 }
166- disk , err = createRegionalDisk (ctx , gceCS .CloudProvider , name , zones , diskType , capacityRange , capBytes )
174+ disk , err = createRegionalDisk (ctx , gceCS .CloudProvider , name , zones , diskType , capacityRange , capBytes , snapshotId )
167175 if err != nil {
168176 return nil , status .Error (codes .Internal , fmt .Sprintf ("CreateVolume failed to create regional disk %#v: %v" , name , err ))
169177 }
170178 default :
171179 return nil , status .Error (codes .InvalidArgument , fmt .Sprintf ("CreateVolume replication type '%s' is not supported" , replicationType ))
172180 }
173-
174- return generateCreateVolumeResponse (disk .GetSelfLink (), capBytes , zones ), nil
181+ return generateCreateVolumeResponse (disk , capBytes , zones ), nil
175182
176183}
177184
@@ -798,7 +805,7 @@ func getDefaultZonesInRegion(gceCS *GCEControllerServer, existingZones []string,
798805 return ret , nil
799806}
800807
801- func generateCreateVolumeResponse (selfLink string , capBytes int64 , zones []string ) * csi.CreateVolumeResponse {
808+ func generateCreateVolumeResponse (disk * gce. CloudDisk , capBytes int64 , zones []string ) * csi.CreateVolumeResponse {
802809 tops := []* csi.Topology {}
803810 for _ , zone := range zones {
804811 tops = append (tops , & csi.Topology {
@@ -808,11 +815,23 @@ func generateCreateVolumeResponse(selfLink string, capBytes int64, zones []strin
808815 createResp := & csi.CreateVolumeResponse {
809816 Volume : & csi.Volume {
810817 CapacityBytes : capBytes ,
811- Id : cleanSelfLink (selfLink ),
818+ Id : cleanSelfLink (disk . GetSelfLink () ),
812819 Attributes : nil ,
813820 AccessibleTopology : tops ,
814821 },
815822 }
823+ snapshotId := disk .GetSnapshotId ()
824+ if snapshotId != "" {
825+ source := & csi.VolumeContentSource {
826+ Type : & csi.VolumeContentSource_Snapshot {
827+ Snapshot : & csi.VolumeContentSource_SnapshotSource {
828+ Id : snapshotId ,
829+ },
830+ },
831+ }
832+ createResp .Volume .ContentSource = source
833+
834+ }
816835 return createResp
817836}
818837
@@ -821,7 +840,7 @@ func cleanSelfLink(selfLink string) string {
821840 return strings .TrimPrefix (temp , gce .GCEComputeBetaAPIEndpoint )
822841}
823842
824- func createRegionalDisk (ctx context.Context , cloudProvider gce.GCECompute , name string , zones []string , diskType string , capacityRange * csi.CapacityRange , capBytes int64 ) (* gce.CloudDisk , error ) {
843+ func createRegionalDisk (ctx context.Context , cloudProvider gce.GCECompute , name string , zones []string , diskType string , capacityRange * csi.CapacityRange , capBytes int64 , snapshotId string ) (* gce.CloudDisk , error ) {
825844 region , err := common .GetRegionFromZones (zones )
826845 if err != nil {
827846 return nil , fmt .Errorf ("failed to get region from zones: %v" , err )
@@ -833,7 +852,7 @@ func createRegionalDisk(ctx context.Context, cloudProvider gce.GCECompute, name
833852 fullyQualifiedReplicaZones , cloudProvider .GetReplicaZoneURI (replicaZone ))
834853 }
835854
836- err = cloudProvider .InsertDisk (ctx , meta .RegionalKey (name , region ), diskType , capBytes , capacityRange , fullyQualifiedReplicaZones )
855+ err = cloudProvider .InsertDisk (ctx , meta .RegionalKey (name , region ), diskType , capBytes , capacityRange , fullyQualifiedReplicaZones , snapshotId )
837856 if err != nil {
838857 return nil , fmt .Errorf ("failed to insert regional disk: %v" , err )
839858 }
@@ -847,12 +866,12 @@ func createRegionalDisk(ctx context.Context, cloudProvider gce.GCECompute, name
847866 return disk , nil
848867}
849868
850- func createSingleZoneDisk (ctx context.Context , cloudProvider gce.GCECompute , name string , zones []string , diskType string , capacityRange * csi.CapacityRange , capBytes int64 ) (* gce.CloudDisk , error ) {
869+ func createSingleZoneDisk (ctx context.Context , cloudProvider gce.GCECompute , name string , zones []string , diskType string , capacityRange * csi.CapacityRange , capBytes int64 , snapshotId string ) (* gce.CloudDisk , error ) {
851870 if len (zones ) != 1 {
852871 return nil , fmt .Errorf ("got wrong number of zones for zonal create volume: %v" , len (zones ))
853872 }
854873 diskZone := zones [0 ]
855- err := cloudProvider .InsertDisk (ctx , meta .ZonalKey (name , diskZone ), diskType , capBytes , capacityRange , nil )
874+ err := cloudProvider .InsertDisk (ctx , meta .ZonalKey (name , diskZone ), diskType , capBytes , capacityRange , nil , snapshotId )
856875 if err != nil {
857876 return nil , fmt .Errorf ("failed to insert zonal disk: %v" , err )
858877 }
0 commit comments